Rename emitter module to events.
authorRadek Brich <radek.brich@devl.cz>
Wed, 02 Jan 2013 00:16:12 +0100
changeset 43 369c8ef5070a
parent 42 0224ce40792f
child 44 d77f1ae3786c
Rename emitter module to events.
tuikit/button.py
tuikit/common.py
tuikit/editbox.py
tuikit/emitter.py
tuikit/events.py
tuikit/menu.py
tuikit/scrollbar.py
tuikit/tableview.py
tuikit/treeview.py
tuikit/widget.py
--- a/tuikit/button.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/button.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from tuikit.widget import Widget
-from tuikit.emitter import Event
+from tuikit.events import Event
 
 
 class Button(Widget):
--- a/tuikit/common.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/common.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from tuikit.emitter import Event, Emitter
+from tuikit.events import Event, Emitter
 
 
 class Coords:
--- a/tuikit/editbox.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/editbox.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from tuikit.widget import Widget
-from tuikit.emitter import Event
+from tuikit.events import Event
 
 
 class EditBox(Widget):
--- a/tuikit/emitter.py	Sun Dec 30 13:03:29 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,189 +0,0 @@
-# -*- coding: utf-8 -*-
-"""Event emitter.
-
-This is simple implementation of signals/slots paradigm.
-We deliberately do not use signal or slot words, as they
-could be misleading (there are __slots__ in Python and
-signals in Unix OS).
-
-"""
-
-import logging
-
-
-class Event:
-    def __init__(self):
-        self.originator = None
-
-    def __getitem__(self, key):
-        return self.__dict__[key]
-
-
-class DrawEvent(Event):
-    def __init__(self, driver, x, y):
-        super().__init__()
-        self.driver = driver
-        self.x = x
-        self.y = y
-
-    def __repr__(self):
-        return 'DrawEvent(x={0.x},y={0.y})'.format(self)
-
-
-class FocusEvent(Event):
-    def __init__(self, old=None, new=None):
-        super().__init__()
-        #: Former focused widget.
-        self.old = old
-        #: Current focused widget.
-        self.new = new
-
-    def __repr__(self):
-        return 'FocusEvent(old={0.old},new={0.new})'.format(self)
-
-
-class KeyboardEvent(Event):
-    def __init__(self, keyname, char):
-        super().__init__()
-        self.keyname = keyname
-        self.char = char
-
-    def __repr__(self):
-        return 'KeyboardEvent(keyname={0.keyname},char={0.char})'.format(self)
-
-
-class MouseEvent(Event):
-    def __init__(self, x=0, y=0, button=0):
-        super().__init__()
-        self.x = x   # global coordinates
-        self.y = y
-        self.wx = x  # local widget coordinates
-        self.wy = y
-        self.px = 0  # parent coordinates
-        self.py = 0
-        self.button = button
-
-    def childevent(self, child):
-        ev = MouseEvent(self.x, self.y, self.button)
-        # original local coordinates are new parent coordinates
-        ev.px = self.wx
-        ev.py = self.wy
-        # update local coordinates
-        ev.wx = self.wx - child.x
-        ev.wy = self.wy - child.y
-        return ev
-
-    def __repr__(self):
-        return 'MouseEvent(x={0.x},y={0.y},button={0.button})'.format(self)
-
-
-class GenericEvent(Event):
-    """Generic event which carries an object with additional data."""
-    def __init__(self, data):
-        self.data = data
-
-
-class Emitter:
-
-    """Event emitter mixin class."""
-
-    def add_events(self, *events):
-        """Add events 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.
-
-        *events -- Arguments must be given in pairs.
-
-        Each pair consists of event_name, event_class:
-            event_name -- a string used in connect(), emit()
-            event_class -- class of event payload
-
-        """
-        if not hasattr(self, '_event_handlers'):
-            self._event_handlers = dict()
-            self._event_class = dict()
-        for event_name, event_class in zip(events[::2], events[1::2]):
-            self._event_handlers[event_name] = []
-            self._event_class[event_name] = event_class
-            # add default dummy handler if no handler exists for this event
-            handler_name = '_handle_' + event_name
-            if not hasattr(Emitter, handler_name):
-                setattr(Emitter, handler_name, lambda self, ev: False)
-
-    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 emit(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.
-        
-        This creates new instance of event_class given to
-        add_events() and passes all arguments after event_name
-        to its __init__ method.
-        
-        Unless first of these arguments is Event instance
-        in which case no object is created and the instance
-        is passed to handlers.
-        
-        """
-        logging.getLogger('tuikit').debug('Emit "%s" on %s %s',
-            event_name,
-            self.__class__.__name__,
-            getattr(self, 'name', None) or id(self))
-        # create event from specified event class, or use first argument
-        if len(args) and isinstance(args[0], Event):
-            event = args[0]
-        else:
-            event = self._event_class[event_name](*args, **kwargs)
-        event.originator = self
-        # try default handler, stop if satisfied
-        handled = getattr(self, '_handle_' + event_name)(event)
-        if handled:
-            return True
-        # try custom handlers, stop if satisfied
-        for handler in self._event_handlers[event_name]:
-            handled = handler(event)
-            if handled:
-                return True
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tuikit/events.py	Wed Jan 02 00:16:12 2013 +0100
@@ -0,0 +1,189 @@
+# -*- coding: utf-8 -*-
+"""Event emitter.
+
+There are two sorts of events - those which are emitted by user or system
+and others which are emitted by Widgets. We will not distinguish them.
+
+This module implements universal mechanism for event emitting and handling.
+
+"""
+
+import logging
+
+
+class Event:
+    def __init__(self):
+        self.originator = None
+
+    def __getitem__(self, key):
+        return self.__dict__[key]
+
+
+class DrawEvent(Event):
+    def __init__(self, driver, x, y):
+        super().__init__()
+        self.driver = driver
+        self.x = x
+        self.y = y
+
+    def __repr__(self):
+        return 'DrawEvent(x={0.x},y={0.y})'.format(self)
+
+
+class FocusEvent(Event):
+    def __init__(self, old=None, new=None):
+        super().__init__()
+        #: Former focused widget.
+        self.old = old
+        #: Current focused widget.
+        self.new = new
+
+    def __repr__(self):
+        return 'FocusEvent(old={0.old},new={0.new})'.format(self)
+
+
+class KeyboardEvent(Event):
+    def __init__(self, keyname, char):
+        super().__init__()
+        self.keyname = keyname
+        self.char = char
+
+    def __repr__(self):
+        return 'KeyboardEvent(keyname={0.keyname},char={0.char})'.format(self)
+
+
+class MouseEvent(Event):
+    def __init__(self, x=0, y=0, button=0):
+        super().__init__()
+        self.x = x   # global coordinates
+        self.y = y
+        self.wx = x  # local widget coordinates
+        self.wy = y
+        self.px = 0  # parent coordinates
+        self.py = 0
+        self.button = button
+
+    def childevent(self, child):
+        ev = MouseEvent(self.x, self.y, self.button)
+        # original local coordinates are new parent coordinates
+        ev.px = self.wx
+        ev.py = self.wy
+        # update local coordinates
+        ev.wx = self.wx - child.x
+        ev.wy = self.wy - child.y
+        return ev
+
+    def __repr__(self):
+        return 'MouseEvent(x={0.x},y={0.y},button={0.button})'.format(self)
+
+
+class GenericEvent(Event):
+    """Generic event which carries an object with additional data."""
+    def __init__(self, data):
+        self.data = data
+
+
+class Emitter:
+
+    """Event emitter mixin class."""
+
+    def add_events(self, *events):
+        """Add events 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.
+
+        *events -- Arguments must be given in pairs.
+
+        Each pair consists of event_name, event_class:
+            event_name -- a string used in connect(), emit()
+            event_class -- class of event payload
+
+        """
+        if not hasattr(self, '_event_handlers'):
+            self._event_handlers = dict()
+            self._event_class = dict()
+        for event_name, event_class in zip(events[::2], events[1::2]):
+            self._event_handlers[event_name] = []
+            self._event_class[event_name] = event_class
+            # add default dummy handler if no handler exists for this event
+            handler_name = '_handle_' + event_name
+            if not hasattr(Emitter, handler_name):
+                setattr(Emitter, handler_name, lambda self, ev: False)
+
+    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 emit(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.
+
+        This creates new instance of event_class given to
+        add_events() and passes all arguments after event_name
+        to its __init__ method.
+
+        Unless first of these arguments is Event instance
+        in which case no object is created and the instance
+        is passed to handlers.
+
+        """
+        logging.getLogger('tuikit').debug('Emit "%s" on %s %s',
+            event_name,
+            self.__class__.__name__,
+            getattr(self, 'name', None) or id(self))
+        # create event from specified event class, or use first argument
+        if len(args) and isinstance(args[0], Event):
+            event = args[0]
+        else:
+            event = self._event_class[event_name](*args, **kwargs)
+        event.originator = self
+        # try default handler, stop if satisfied
+        handled = getattr(self, '_handle_' + event_name)(event)
+        if handled:
+            return True
+        # try custom handlers, stop if satisfied
+        for handler in self._event_handlers[event_name]:
+            handled = handler(event)
+            if handled:
+                return True
+
--- a/tuikit/menu.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/menu.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from tuikit.widget import Widget
-from tuikit.emitter import GenericEvent
+from tuikit.events import GenericEvent
 
 
 class Menu(Widget):
--- a/tuikit/scrollbar.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/scrollbar.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 from tuikit.widget import Widget
-from tuikit.emitter import Event
+from tuikit.events import Event
 
 
 class VScrollbar(Widget):
--- a/tuikit/tableview.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/tableview.py	Wed Jan 02 00:16:12 2013 +0100
@@ -3,7 +3,7 @@
 import math
 import logging
 
-from tuikit.emitter import Event, Emitter
+from tuikit.events import Event, Emitter
 from tuikit.widget import Widget
 from tuikit.common import Coords
 
--- a/tuikit/treeview.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/treeview.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from tuikit.emitter import Event, Emitter
+from tuikit.events import Event, Emitter
 from tuikit.widget import Widget
 
 
--- a/tuikit/widget.py	Sun Dec 30 13:03:29 2012 +0100
+++ b/tuikit/widget.py	Wed Jan 02 00:16:12 2013 +0100
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from tuikit.emitter import Emitter, Event, DrawEvent, FocusEvent, KeyboardEvent, MouseEvent
+from tuikit.events import Emitter, Event, DrawEvent, FocusEvent, KeyboardEvent, MouseEvent
 from tuikit.common import Coords, Size