[mousetrap/gnome3-wip: 106/240] Rename `Part` to `Plugin`.
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 106/240] Rename `Part` to `Plugin`.
- Date: Mon, 8 Sep 2014 15:21:28 +0000 (UTC)
commit 6be358381e78e84f026933752f9d028fa0d8ff0d
Author: Stoney Jackson <dr stoney gmail com>
Date: Fri Jun 20 13:22:26 2014 -0400
Rename `Part` to `Plugin`.
src/mousetrap/main.py | 32 ++++----
src/mousetrap/main.py.1 | 101 +++++++++++++++++++++
src/mousetrap/parts/camera.py | 6 --
src/mousetrap/{parts => plugins}/__init__.py | 0
src/mousetrap/plugins/camera.py | 6 ++
src/mousetrap/{parts => plugins}/display.py | 4 +-
src/mousetrap/{parts => plugins}/eyes.py | 4 +-
src/mousetrap/{parts => plugins}/interface.py | 2 +-
src/mousetrap/{parts => plugins}/nose.py | 4 +-
src/mousetrap/{parts => plugins}/nose_joystick.py | 6 +-
10 files changed, 133 insertions(+), 32 deletions(-)
---
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index cb13053..7010368 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -13,10 +13,10 @@ LOGGER = log.getLogger('mousetrap.main')
#TODO: Should be a configuration file.
DEFAULT_PARTS = [
- ('camera', 'mousetrap.parts.camera.CameraPart'),
- ('display', 'mousetrap.parts.display.DisplayPart'),
- ('nose_joystick', 'mousetrap.parts.nose_joystick.NoseJoystickPart'),
- ('eye_click', 'mousetrap.parts.eyes.EyesPart'),
+ ('camera', 'mousetrap.plugins.camera.CameraPlugin'),
+ ('display', 'mousetrap.plugins.display.DisplayPlugin'),
+ ('nose_joystick', 'mousetrap.plugins.nose_joystick.NoseJoystickPlugin'),
+ ('eye_click', 'mousetrap.plugins.eyes.EyesPlugin'),
]
DEFAULT_LOOPS_PER_SECOND = 10
@@ -28,27 +28,27 @@ class App(object):
self.gui = Gui()
self.camera = Camera()
self.pointer = Pointer()
- self.parts = []
- self._assemble_parts()
+ self.plugins = []
+ self._assemble_plugins()
- def _assemble_parts(self):
- self._load_parts(DEFAULT_PARTS)
- self._register_parts_with_loop()
+ def _assemble_plugins(self):
+ self._load_plugins(DEFAULT_PARTS)
+ self._register_plugins_with_loop()
- def _load_parts(self, part_descriptors):
- for name, class_ in part_descriptors:
- self.parts.append(self._load_part(class_))
+ def _load_plugins(self, plugin_descriptors):
+ for name, class_ in plugin_descriptors:
+ self.plugins.append(self._load_plugin(class_))
@staticmethod
- def _load_part(class_):
+ def _load_plugin(class_):
LOGGER.debug('loading %s', class_)
class_path = class_.split('.')
module = __import__('.'.join(class_path[:-1]), {}, {}, class_path[-1])
return getattr(module, class_path[-1])()
- def _register_parts_with_loop(self):
- for part in self.parts:
- self.loop.subscribe(part)
+ def _register_plugins_with_loop(self):
+ for plugin in self.plugins:
+ self.loop.subscribe(plugin)
def run(self, app=None):
self.loop.start()
diff --git a/src/mousetrap/main.py.1 b/src/mousetrap/main.py.1
new file mode 100644
index 0000000..eb8cdad
--- /dev/null
+++ b/src/mousetrap/main.py.1
@@ -0,0 +1,101 @@
+'''
+Where it all begins.
+'''
+
+import mousetrap.log as log
+from gi.repository import GObject, Gdk, Gtk
+from mousetrap.gui import Gui, Pointer
+from mousetrap.vision import Camera
+
+
+LOGGER = log.getLogger('mousetrap.main')
+
+
+#TODO: Should be a configuration file.
+DEFAULT_PARTS = [
+ ('camera', 'mousetrap.plugins.camera.CameraPart'),
+ ('display', 'mousetrap.plugins.display.DisplayPart'),
+ ('nose_joystick', 'mousetrap.plugins.nose_joystick.NoseJoystickPart'),
+ ('eye_click', 'mousetrap.plugins.eyes.EyesPart'),
+ ]
+DEFAULT_LOOPS_PER_SECOND = 10
+
+
+class App(object):
+ def __init__(self):
+ self.image = None
+ self.loop = Loop(self)
+ self.gui = Gui()
+ self.camera = Camera()
+ self.pointer = Pointer()
+ self.plugins = []
+ self._assemble_plugins()
+
+ def _assemble_plugins(self):
+ self._load_plugins(DEFAULT_PARTS)
+ self._register_plugins_with_loop()
+
+ def _load_plugins(self, plugin_descriptors):
+ for name, class_ in plugin_descriptors:
+ self.plugins.append(self._load_plugin(class_))
+
+ @staticmethod
+ def _load_plugin(class_):
+ LOGGER.debug('loading %s', class_)
+ class_path = class_.split('.')
+ module = __import__('.'.join(class_path[:-1]), {}, {}, class_path[-1])
+ return getattr(module, class_path[-1])()
+
+ def _register_plugins_with_loop(self):
+ for plugin in self.plugins:
+ self.loop.subscribe(plugin)
+
+ def run(self, app=None):
+ self.loop.start()
+ self.gui.start()
+
+
+class Observable(object):
+ def __init__(self):
+ self.__observers = []
+ self.__arguments = {}
+
+ def subscribe(self, observer):
+ self.__observers.append(observer)
+
+ def _add_argument(self, key, value):
+ self.__arguments[key] = value
+
+ def _fire(self, callback_name):
+ for observer in self.__observers:
+ callback = getattr(observer, callback_name)
+ callback(**self.__arguments)
+
+
+class Loop(Observable):
+ MILLISECONDS_PER_SECOND = 1000.0
+ CALLBACK_RUN = 'run'
+
+ def __init__(self, app):
+ super(Loop, self).__init__()
+ self.set_loops_per_second(DEFAULT_LOOPS_PER_SECOND)
+ self._timeout_id = None
+ self._add_argument('app', app)
+
+ def set_loops_per_second(self, loops_per_second):
+ self._loops_per_second = loops_per_second
+ self._interval = int(round(
+ self.MILLISECONDS_PER_SECOND / self._loops_per_second))
+
+ def start(self):
+ self.timeout_id = GObject.timeout_add(self._interval, self.run)
+
+ def run(self):
+ CONTINUE = True
+ PAUSE = False
+ self._fire(self.CALLBACK_RUN)
+ return CONTINUE
+
+
+if __name__ == '__main__':
+ App().run()
diff --git a/src/mousetrap/parts/__init__.py b/src/mousetrap/plugins/__init__.py
similarity index 100%
rename from src/mousetrap/parts/__init__.py
rename to src/mousetrap/plugins/__init__.py
diff --git a/src/mousetrap/plugins/camera.py b/src/mousetrap/plugins/camera.py
new file mode 100644
index 0000000..1e79d8f
--- /dev/null
+++ b/src/mousetrap/plugins/camera.py
@@ -0,0 +1,6 @@
+import mousetrap.plugins.interface as interface
+
+
+class CameraPlugin(interface.Plugin):
+ def run(self, app):
+ app.image = app.camera.read_image()
diff --git a/src/mousetrap/parts/display.py b/src/mousetrap/plugins/display.py
similarity index 51%
rename from src/mousetrap/parts/display.py
rename to src/mousetrap/plugins/display.py
index 15fa0d1..7bc7aae 100644
--- a/src/mousetrap/parts/display.py
+++ b/src/mousetrap/plugins/display.py
@@ -1,7 +1,7 @@
-import mousetrap.parts.interface as interface
+import mousetrap.plugins.interface as interface
import logging
-class DisplayPart(interface.Part):
+class DisplayPlugin(interface.Plugin):
def run(self, app):
app.gui.show_image('MouseTrap', app.image)
diff --git a/src/mousetrap/parts/eyes.py b/src/mousetrap/plugins/eyes.py
similarity index 94%
rename from src/mousetrap/parts/eyes.py
rename to src/mousetrap/plugins/eyes.py
index c061685..1e52fae 100644
--- a/src/mousetrap/parts/eyes.py
+++ b/src/mousetrap/plugins/eyes.py
@@ -1,4 +1,4 @@
-import mousetrap.parts.interface as interface
+import mousetrap.plugins.interface as interface
from mousetrap.vision import FeatureDetector, FeatureNotFoundException
import mousetrap.log as log
@@ -6,7 +6,7 @@ import mousetrap.log as log
LOGGER = log.getLogger(__name__)
-class EyesPart(interface.Part):
+class EyesPlugin(interface.Plugin):
def __init__(self):
self._left_locator = LeftEyeLocator()
self._history = []
diff --git a/src/mousetrap/parts/interface.py b/src/mousetrap/plugins/interface.py
similarity index 84%
rename from src/mousetrap/parts/interface.py
rename to src/mousetrap/plugins/interface.py
index c55cf48..15c3176 100644
--- a/src/mousetrap/parts/interface.py
+++ b/src/mousetrap/plugins/interface.py
@@ -1,4 +1,4 @@
-class Part(object):
+class Plugin(object):
def run(self, app):
'''Called each pass of the loop.'''
raise NotImplementedError('Must implement.')
diff --git a/src/mousetrap/parts/nose.py b/src/mousetrap/plugins/nose.py
similarity index 95%
rename from src/mousetrap/parts/nose.py
rename to src/mousetrap/plugins/nose.py
index 4bf1c61..284494b 100644
--- a/src/mousetrap/parts/nose.py
+++ b/src/mousetrap/plugins/nose.py
@@ -1,9 +1,9 @@
-import mousetrap.parts.interface as interface
+import mousetrap.plugins.interface as interface
from mousetrap.vision import FeatureDetector, FeatureNotFoundException
from mousetrap.gui import Gui
-class NosePart(interface.Part):
+class NosePlugin(interface.Plugin):
def __init__(self):
self._nose_locator = NoseLocator()
self._gui = Gui()
diff --git a/src/mousetrap/parts/nose_joystick.py b/src/mousetrap/plugins/nose_joystick.py
similarity index 92%
rename from src/mousetrap/parts/nose_joystick.py
rename to src/mousetrap/plugins/nose_joystick.py
index 4200309..a735815 100644
--- a/src/mousetrap/parts/nose_joystick.py
+++ b/src/mousetrap/plugins/nose_joystick.py
@@ -1,9 +1,9 @@
-import mousetrap.parts.interface as interface
+import mousetrap.plugins.interface as interface
from mousetrap.vision import FeatureDetector, FeatureNotFoundException
-from mousetrap.parts.nose import NoseLocator
+from mousetrap.plugins.nose import NoseLocator
-class NoseJoystickPart(interface.Part):
+class NoseJoystickPlugin(interface.Plugin):
THRESHOLD = 5
def __init__(self):
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]