Commits

Olemis Lang committed 094ecab

Trac #11148 : ListenerNotifier.notify(*args) => ListenerNotifier.notify(**kwargs)

Comments (0)

Files changed (1)

t11148/t11148_r11782_IEntityChangeListener_extends_with_prefix.diff

 
 diff -r 8d0c3223a818 trac/core.py
 --- a/trac/core.py	Thu Apr 18 14:30:21 2013 +0000
-+++ b/trac/core.py	Tue Apr 23 09:03:10 2013 -0500
++++ b/trac/core.py	Tue Apr 23 09:56:25 2013 -0500
 @@ -165,8 +165,30 @@
  
          locals_.setdefault('_implements', []).extend(interfaces)
  
  
  class ComponentManager(object):
-@@ -236,3 +258,57 @@
+@@ -236,3 +258,61 @@
          with the given class will not be available.
          """
          return True
 +    etc.
 +    * changeinfo: is instance of NotificationChangeInfo. The changeinfo
 +    parameter content depends on an entity type and may be None.
++    * kwargs: receives custom arguments supported by resource-specific
++    interfaces in the form of excess keyword arguments 
 +    """
 +
-+    def entity_created(entity, changeinfo = None):
++    def entity_created(entity, changeinfo = None, **kwargs):
 +        """
 +        Called when an entity is created.
 +        """
 +
-+    def entity_changed(entity, old_values, changeinfo = None):
++    def entity_changed(entity, old_values, changeinfo = None, **kwargs):
 +        """Called when an entity is modified.
 +
 +        `old_values` is a dictionary containing the previous values of the
 +        type.
 +        """
 +
-+    def entity_deleted(entity, changeinfo = None):
++    def entity_deleted(entity, changeinfo = None, **kwargs):
 +        """Called when an entity is deleted."""
 +
-+    def entity_reparented(entity, changeinfo = None):
++    def entity_reparented(entity, changeinfo = None, **kwargs):
 +        """Called when an entity has been re-parented."""
 +
 +class ListenerNotifier(Component):
 +    required = True
 +
-+    def notify(self, method, *args):
++    def notify(self, method, **kwargs):
 +        interface = method.im_class
 +        method_name = method.__name__
 +        if interface is None:
 +        xp = ExtensionPoint(interface)
 +        # FIXME : Sender component rather than self. Change method signature
 +        for listener in xp.extensions(self):
-+            getattr(listener, method_name)(*args)
++            getattr(listener, method_name)(**kwargs)
 +
-+        #TBD: we can also call listeners implemented IEntityChangeListener here
-+        #if community will agree that generic event listeners support is needed
-+
++        xp = ExtensionPoint(IEntityChangeListener)
++        parts = method_name.split('_', 1)
++        method_name = 'entity_' + parts[-1]
++        for listener in xp.extensions(self):
++            getattr(listener, method_name)(**kwargs)
 diff -r 8d0c3223a818 trac/ticket/model.py
 --- a/trac/ticket/model.py	Thu Apr 18 14:30:21 2013 +0000
-+++ b/trac/ticket/model.py	Tue Apr 23 09:03:10 2013 -0500
++++ b/trac/ticket/model.py	Tue Apr 23 09:56:25 2013 -0500
 @@ -25,7 +25,8 @@
  from trac.attachment import Attachment
  from trac import core
 +        interface = self.change_listener_interface
 +        ListenerNotifier(self.env).notify(getattr(interface, 
 +                    interface._extends_with_prefix + '_deleted'),
-+                    self)
++                    entity=self)
          self.value = self._old_value = None
          self.name = self._old_name = None
  
 +        interface = self.change_listener_interface
 +        ListenerNotifier(self.env).notify(getattr(interface, 
 +                    interface._extends_with_prefix + '_created'),
-+                    self)
++                    entity=self)
  
      def update(self, db=None):
          """Update the enum value.
 +        interface = self.change_listener_interface
 +        ListenerNotifier(self.env).notify(getattr(interface, 
 +                    interface._extends_with_prefix + '_changed'),
-+                    self, old_values)
++                    entity=self, old_values=old_values)
  
      @classmethod
      def select(cls, env, db=None):
              TicketSystem(self.env).reset_ticket_fields()
  
 +        ListenerNotifier(self.env).notify(
-+                    IComponentChangeListener.component_deleted, self)
++                    IComponentChangeListener.component_deleted, entity=self)
 +
 +        self.name = self._old_name = None
 +
              TicketSystem(self.env).reset_ticket_fields()
  
 +        ListenerNotifier(self.env).notify(
-+                    IComponentChangeListener.component_created, self)
++                    IComponentChangeListener.component_created, entity=self)
 +
      def update(self, db=None):
          """Update the component.
          with self.env.db_transaction as db:
              self.env.log.info("Updating component '%s'", self.name)
              db("""UPDATE component SET name=%s,owner=%s, description=%s
-@@ -939,6 +996,13 @@
+@@ -939,6 +996,14 @@
                  self._old_name = self.name
              TicketSystem(self.env).reset_ticket_fields()
  
 +        if self.name != old_name:
 +            old_values["name"] = old_name
 +        ListenerNotifier(self.env).notify(
-+                    IComponentChangeListener.component_changed, self, old_values)
++                    IComponentChangeListener.component_changed, 
++                    entity=self, old_values=old_values)
 +
      @classmethod
      def select(cls, env, db=None):
          """
-@@ -1170,6 +1234,11 @@
+@@ -1170,6 +1235,11 @@
      return groups
  
  
  class Version(object):
      def __init__(self, env, name=None, db=None):
          self.env = env
-@@ -1199,9 +1268,12 @@
+@@ -1199,9 +1269,12 @@
          with self.env.db_transaction as db:
              self.env.log.info("Deleting version %s", self.name)
              db("DELETE FROM version WHERE name=%s", (self.name,))
              TicketSystem(self.env).reset_ticket_fields()
  
 +        ListenerNotifier(self.env).notify(
-+            IVersionChangeListener.version_deleted, self)
++            IVersionChangeListener.version_deleted, entity=self)
 +        self.name = self._old_name = None
 +
      def insert(self, db=None):
          """Insert a new version.
  
-@@ -1220,6 +1292,9 @@
+@@ -1220,6 +1293,9 @@
              self._old_name = self.name
              TicketSystem(self.env).reset_ticket_fields()
  
 +        ListenerNotifier(self.env).notify(
-+            IVersionChangeListener.version_created, self)
++            IVersionChangeListener.version_created, entity=self)
 +
      def update(self, db=None):
          """Update the version.
  
-@@ -1231,6 +1306,7 @@
+@@ -1231,6 +1307,7 @@
          if not self.name:
              raise TracError(_("Invalid version name."))
  
          with self.env.db_transaction as db:
              self.env.log.info("Updating version '%s'", self.name)
              db("""UPDATE version
-@@ -1244,6 +1320,14 @@
+@@ -1244,6 +1321,15 @@
                  self._old_name = self.name
              TicketSystem(self.env).reset_ticket_fields()
  
 +            old_values["name"] = old_name
 +
 +        ListenerNotifier(self.env).notify(
-+            IVersionChangeListener.version_changed, self, old_values)
++            IVersionChangeListener.version_changed, 
++            entity=self, old_values=old_values)
 +
      @classmethod
      def select(cls, env, db=None):
          """
 diff -r 8d0c3223a818 trac/ticket/tests/model.py
 --- a/trac/ticket/tests/model.py	Thu Apr 18 14:30:21 2013 +0000
-+++ b/trac/ticket/tests/model.py	Tue Apr 23 09:03:10 2013 -0500
++++ b/trac/ticket/tests/model.py	Tue Apr 23 09:56:25 2013 -0500
 @@ -12,8 +12,8 @@
  from trac.core import TracError, implements
  from trac.resource import ResourceNotFound
  from trac.ticket.api import (
      IMilestoneChangeListener, ITicketChangeListener, TicketSystem
  )
-@@ -1097,6 +1097,228 @@
+@@ -1097,6 +1097,232 @@
          self.assertEqual([('Test', 0, 'Some text')], self.env.db_query(
              "SELECT name, time, description FROM version WHERE name='Test'"))
  
 +class EntityChangeListenerMock(core.Component):
 + 
-+    def callback(self, action, entity, changeinfo, old_values = None):
++    def callback(self, action, entity, changeinfo, old_values = None, kwargs=None):
 +        pass
 +
-+    def _entity_created(self, entity, changeinfo = None):
++    def _entity_created(self, entity, changeinfo = None, **kwargs):
 +        self.action = "created"
 +        self.entity = entity
 +        self.changeinfo = changeinfo
-+        self.callback(self.action, entity, changeinfo)
++        self.kwargs = kwargs
++        self.callback(self.action, entity, changeinfo, kwargs)
 +
-+    def _entity_changed(self, entity, old_values, changeinfo = None):
++    def _entity_changed(self, entity, old_values, changeinfo = None, **kwargs):
 +        self.action = "changed"
 +        self.entity = entity
 +        self.old_values = old_values
 +        self.changeinfo = changeinfo
++        self.kwargs = kwargs
 +        self.callback(
-+            self.action, entity, changeinfo, old_values=self.old_values)
++            self.action, entity, changeinfo, old_values, kwargs)
 +
-+    def _entity_deleted(self, entity, changeinfo = None):
++    def _entity_deleted(self, entity, changeinfo = None, **kwargs):
 +        self.action = "deleted"
 +        self.entity = entity
 +        self.changeinfo = changeinfo
-+        self.callback(self.action, entity, changeinfo)
++        self.kwargs = kwargs
++        self.callback(self.action, entity, changeinfo, kwargs)
 +
-+    def _entity_reparented(self, entity, changeinfo = None):
++    def _entity_reparented(self, entity, changeinfo = None, **kwargs):
 +        self.action = "reparented"
 +        self.entity = entity
 +        self.changeinfo = changeinfo
-+        self.callback(self.action, entity, changeinfo)
++        self.kwargs = kwargs
++        self.callback(self.action, entity, changeinfo, kwargs)
 +
 +class BaseEntityChangeListenerTestCase(unittest.TestCase):
 +    DUMMY_ENTITY_NAME = "Entity 1"
 +    def _get_entity_name(self, enity):
 +        return enity.name
 +
-+    def listener_callback(self, action, entity, changeinfo, old_values = None):
++    def listener_callback(self, action, entity, changeinfo, old_values=None, kwargs=None):
 +        self.entity_name = self._get_entity_name(entity)
 +
 +
  
  def suite():
      suite = unittest.TestSuite()
-@@ -1107,6 +1329,14 @@
+@@ -1107,6 +1333,14 @@
      suite.addTest(unittest.makeSuite(MilestoneTestCase, 'test'))
      suite.addTest(unittest.makeSuite(ComponentTestCase, 'test'))
      suite.addTest(unittest.makeSuite(VersionTestCase, 'test'))