Refactoring: rename eventsource module to emitter.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/docs/emitter.rst Fri Dec 14 10:32:43 2012 +0100
@@ -0,0 +1,13 @@
+Emitter
+=======
+
+.. toctree::
+ :maxdepth: 3
+ :titlesonly:
+
+ widget
+
+.. automodule:: tuikit.emitter
+ :members:
+ :show-inheritance:
+
--- a/docs/eventsource.rst Fri Dec 14 10:20:14 2012 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-EventSource
-===========
-
-.. toctree::
- :maxdepth: 3
- :titlesonly:
-
- widget
-
-.. automodule:: tuikit.eventsource
- :members:
- :show-inheritance:
-
--- a/docs/index.rst Fri Dec 14 10:20:14 2012 +0100
+++ b/docs/index.rst Fri Dec 14 10:32:43 2012 +0100
@@ -12,7 +12,7 @@
:maxdepth: 3
application
- eventsource
+ emitter
events
focus
redraw
@@ -20,7 +20,7 @@
tableview
.. inheritance-diagram:: tuikit.application
- tuikit.eventsource
+ tuikit.emitter
tuikit.widget
tuikit.container
tuikit.window
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tuikit/emitter.py Fri Dec 14 10:32:43 2012 +0100
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+
+class Emitter:
+
+ """Event emitter mixin class."""
+
+ def add_events(self, *event_names):
+ """Add event names which may be registered by user.
+
+ This should be called only by subclasses.
+ This serves also as initializer, other methods of Emitter
+ will not work if add_events was not called.
+
+ """
+ if not hasattr(self, '_event_handlers'):
+ self._event_handlers = dict()
+ for event_name in event_names:
+ self._event_handlers[event_name] = []
+
+ def connect(self, event_name, handler):
+ """Connect event handler to event name.
+
+ Add handler to the end of handler list.
+
+ """
+ if event_name in self._event_handlers:
+ self._event_handlers[event_name].append(handler)
+ else:
+ raise KeyError('Unknown event: %s', event_name)
+
+ def disconnect(self, event_name, handler=None):
+ """Remove event handler from the list.
+
+ If no handler is given, remove all handlers.
+
+ """
+ if event_name in self._event_handlers:
+ if handler:
+ self._event_handlers[event_name].remove(handler)
+ else:
+ self._event_handlers[event_name][:] = []
+ else:
+ raise KeyError('Unknown event: %s', event_name)
+
+ def is_connected(self, event_name):
+ """Test if any handlers are connected to event name.
+
+ Return True if event handler list is not empty,
+ False otherwise.
+
+ """
+ if event_name in self._event_handlers:
+ return bool(self._event_handlers[event_name])
+ else:
+ raise KeyError('Unknown event: %s', event_name)
+
+ def handle(self, event_name, *args, **kwargs):
+ """Emit the event.
+
+ Call all handlers from event's handler list,
+ starting from first added handler.
+
+ Return True when one of the handlers returns True,
+ False otherwise.
+
+ """
+ handled = False
+ for handler in self._event_handlers[event_name]:
+ res = handler(*args, **kwargs)
+ if res:
+ handled = True
+ return handled
+
+ def emit(self, event, *args, **kwargs):
+ """Emit event.
+
+ This is used by original event source when the event is detected.
+
+ """
+ try:
+ getattr(self, event)(*args, **kwargs)
+ except AttributeError:
+ self.handle(event, *args, **kwargs)
+
--- a/tuikit/eventsource.py Fri Dec 14 10:20:14 2012 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-# -*- coding: utf-8 -*-
-
-class Emitter:
-
- '''Event emitter mixin class.'''
-
- def add_events(self, *event_names):
- '''Add event names which may be registered by user.
-
- This should be called only by subclasses.
- This serves also as initializer, other methods of Emitter
- will not work if add_events was not called.
-
- '''
- if not hasattr(self, '_event_handlers'):
- self._event_handlers = dict()
- for event_name in event_names:
- self._event_handlers[event_name] = []
-
- def connect(self, event_name, handler):
- '''Connect event handler to event name.
-
- Add handler to the end of handler list.
-
- '''
- if event_name in list(self._event_handlers.keys()):
- self._event_handlers[event_name].append(handler)
- else:
- raise KeyError('Unknown event: %s', event_name)
-
- def disconnect(self, event_name, handler=None):
- '''Remove event handler from the list.
-
- If no handler is given, remove all handlers.
-
- '''
- if event_name in list(self._event_handlers.keys()):
- if handler:
- self._event_handlers[event_name].remove(handler)
- else:
- self._event_handlers[event_name][:] = []
- else:
- raise KeyError('Unknown event: %s', event_name)
-
- def handle(self, event_name, *args, **kwargs):
- '''Emit the event.
-
- Call all handlers from event's handler list,
- starting from first added handler.
-
- Return True when one of the handlers returns True,
- False otherwise.
-
- '''
- handled = False
- for handler in self._event_handlers[event_name]:
- res = handler(*args, **kwargs)
- if res:
- handled = True
- return handled
-
- def emit(self, event, *args, **kwargs):
- '''Emit event.
-
- This is used by original event source when the event is detected.
-
- '''
- try:
- getattr(self, event)(*args, **kwargs)
- except AttributeError:
- self.handle(event, *args, **kwargs)
-
--- a/tuikit/tableview.py Fri Dec 14 10:20:14 2012 +0100
+++ b/tuikit/tableview.py Fri Dec 14 10:32:43 2012 +0100
@@ -3,7 +3,7 @@
import math
import logging
-from tuikit.eventsource import Emitter
+from tuikit.emitter import Emitter
from tuikit.widget import Widget
from tuikit.common import Coords
--- a/tuikit/treeview.py Fri Dec 14 10:20:14 2012 +0100
+++ b/tuikit/treeview.py Fri Dec 14 10:32:43 2012 +0100
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-from tuikit.eventsource import Emitter
+from tuikit.emitter import Emitter
from tuikit.widget import Widget
--- a/tuikit/widget.py Fri Dec 14 10:20:14 2012 +0100
+++ b/tuikit/widget.py Fri Dec 14 10:32:43 2012 +0100
@@ -2,7 +2,7 @@
import logging
-from tuikit.eventsource import Emitter
+from tuikit.emitter import Emitter
from tuikit.common import Coords, Size
@@ -162,7 +162,7 @@
def canfocus(self):
- return bool(self._event_handlers['keypress'])
+ return self.is_connected('keypress')
def hasfocus(self):