]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- factor out the dependency on the "on_" name
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 30 Dec 2010 19:07:15 +0000 (14:07 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 30 Dec 2010 19:07:15 +0000 (14:07 -0500)
lib/sqlalchemy/event.py
lib/sqlalchemy/orm/attributes.py
lib/sqlalchemy/orm/events.py
lib/sqlalchemy/orm/mapper.py
lib/sqlalchemy/pool.py
test/base/test_events.py
test/orm/test_cascade.py
test/orm/test_instrumentation.py

index ad46a0c60e0225f685e3993f7b3eaa2c6eec27c7..ae49517b4f67ac6fee6c951d43cf9dd5df75967e 100644 (file)
@@ -33,13 +33,16 @@ def remove(target, identifier, fn):
 
 _registrars = util.defaultdict(list)
 
+def _is_event_name(name):
+    return not name.startswith('_') and name != 'dispatch'
+    
 class _UnpickleDispatch(object):
     """Serializable callable that re-generates an instance of :class:`_Dispatch`
     given a particular :class:`.Events` subclass.
     
     """
-    def __call__(self, parent_cls):
-        return parent_cls.__dict__['dispatch'].dispatch_cls(parent_cls)
+    def __call__(self, _parent_cls):
+        return _parent_cls.__dict__['dispatch'].dispatch_cls(_parent_cls)
         
 class _Dispatch(object):
     """Mirror the event listening definitions of an Events class with 
@@ -61,23 +64,23 @@ class _Dispatch(object):
     
     """
     
-    def __init__(self, parent_cls):
-        self.parent_cls = parent_cls
+    def __init__(self, _parent_cls):
+        self._parent_cls = _parent_cls
     
     def __reduce__(self):
         
-        return _UnpickleDispatch(), (self.parent_cls, )
+        return _UnpickleDispatch(), (self._parent_cls, )
     
     @property
-    def descriptors(self):
-        return (getattr(self, k) for k in dir(self) if k.startswith("on_"))
+    def _descriptors(self):
+        return (getattr(self, k) for k in dir(self) if _is_event_name(k))
 
-    def update(self, other, only_propagate=True):
+    def _update(self, other, only_propagate=True):
         """Populate from the listeners in another :class:`_Dispatch`
             object."""
 
-        for ls in other.descriptors:
-            getattr(self, ls.name).update(ls, only_propagate=only_propagate)
+        for ls in other._descriptors:
+            getattr(self, ls.name)._update(ls, only_propagate=only_propagate)
             
             
 class _EventMeta(type):
@@ -102,13 +105,13 @@ def _create_dispatcher_class(cls, classname, bases, dict_):
     dispatch_cls._clear = cls._clear
     
     for k in dict_:
-        if k.startswith('on_'):
+        if _is_event_name(k):
             setattr(dispatch_cls, k, _DispatchDescriptor(dict_[k]))
             _registrars[k].append(cls)
 
 def _remove_dispatcher(cls):
     for k in dir(cls):
-        if k.startswith('on_'):
+        if _is_event_name(k):
             _registrars[k].remove(cls)
             if not _registrars[k]:
                 del _registrars[k]
@@ -143,7 +146,7 @@ class Events(object):
     @classmethod
     def _clear(cls):
         for attr in dir(cls.dispatch):
-            if attr.startswith("on_"):
+            if _is_event_name(attr):
                 getattr(cls.dispatch, attr).clear()
                 
 class _DispatchDescriptor(object):
@@ -175,7 +178,7 @@ class _DispatchDescriptor(object):
         if obj is None:
             return self
         obj.__dict__[self.__name__] = result = \
-                            _ListenerCollection(self, obj.parent_cls)
+                            _ListenerCollection(self, obj._parent_cls)
         return result
 
 class _ListenerCollection(object):
@@ -231,7 +234,7 @@ class _ListenerCollection(object):
     def __nonzero__(self):
         return bool(self.listeners or self.parent_listeners)
     
-    def update(self, other, only_propagate=True):
+    def _update(self, other, only_propagate=True):
         """Populate from the listeners in another :class:`_Dispatch`
             object."""
         
index d6dc10ad47bb11b15abf0df2d849c8527ea09e39..d32d4f1b17030ad3c2cc34acf90cd7db86ab843f 100644 (file)
@@ -68,10 +68,10 @@ class QueryableAttribute(interfaces.PropComparator):
             # immediate superclass
             for base in manager._bases:
                 if key in base:
-                    self.dispatch.update(base[key].dispatch)
+                    self.dispatch._update(base[key].dispatch)
 
     dispatch = event.dispatcher(events.AttributeEvents)
-    dispatch.dispatch_cls.active_history = False
+    dispatch.dispatch_cls._active_history = False
     
     @util.memoized_property
     def _supports_population(self):
@@ -276,17 +276,17 @@ class AttributeImpl(object):
             ext._adapt_listener(attr, ext)
             
         if active_history:
-            self.dispatch.active_history = True
+            self.dispatch._active_history = True
 
         self.expire_missing = expire_missing
         
     def _get_active_history(self):
         """Backwards compat for impl.active_history"""
         
-        return self.dispatch.active_history
+        return self.dispatch._active_history
     
     def _set_active_history(self, value):
-        self.dispatch.active_history = value
+        self.dispatch._active_history = value
     
     active_history = property(_get_active_history, _set_active_history)
     
@@ -442,7 +442,7 @@ class ScalarAttributeImpl(AttributeImpl):
     def delete(self, state, dict_):
 
         # TODO: catch key errors, convert to attributeerror?
-        if self.dispatch.active_history:
+        if self.dispatch._active_history:
             old = self.get(state, dict_)
         else:
             old = dict_.get(self.key, NO_VALUE)
@@ -460,7 +460,7 @@ class ScalarAttributeImpl(AttributeImpl):
         if initiator and initiator.parent_token is self.parent_token:
             return
 
-        if self.dispatch.active_history:
+        if self.dispatch._active_history:
             old = self.get(state, dict_)
         else:
             old = dict_.get(self.key, NO_VALUE)
@@ -606,7 +606,7 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl):
         if initiator and initiator.parent_token is self.parent_token:
             return
 
-        if self.dispatch.active_history:
+        if self.dispatch._active_history:
             old = self.get(state, dict_, passive=PASSIVE_ONLY_PERSISTENT)
         else:
             old = self.get(state, dict_, passive=PASSIVE_NO_FETCH)
index db7623bd22760f30fffcf7aac3dfdddcd21a1228..e8dd07142842e2c085e2e08970aa5416884dd23e 100644 (file)
@@ -858,7 +858,7 @@ class AttributeEvents(event.Events):
                                         raw=False, retval=False,
                                         propagate=False):
         if active_history:
-            target.dispatch.active_history = True
+            target.dispatch._active_history = True
         
         # TODO: for removal, need to package the identity
         # of the wrapper with the original function.
index cfd17500866fd18c289c4325fe983ff5ed96e0cc..563de116a5b6c72ecea7e87591f72ad77b981f16 100644 (file)
@@ -331,7 +331,7 @@ class Mapper(object):
     def _configure_legacy_instrument_class(self):
 
         if self.inherits:
-            self.dispatch.update(self.inherits.dispatch)
+            self.dispatch._update(self.inherits.dispatch)
             super_extensions = set(chain(*[m._deprecated_extensions 
                                     for m in self.inherits.iterate_to_root()]))
         else:
@@ -353,7 +353,7 @@ class Mapper(object):
                 ext._adapt_listener(self, ext)
         
         if self.inherits:
-            self.class_manager.dispatch.update(
+            self.class_manager.dispatch._update(
                         self.inherits.class_manager.dispatch)
 
     def _configure_class_instrumentation(self):
index 4ae6ec022f0bb30ce56c572039f87d62a9f20788..572087217e22e9534821dc30e33d010a2d021d62 100644 (file)
@@ -135,7 +135,7 @@ class Pool(log.Identified):
         self._reset_on_return = reset_on_return
         self.echo = echo
         if _dispatch:
-            self.dispatch.update(_dispatch, only_propagate=False)
+            self.dispatch._update(_dispatch, only_propagate=False)
         if events:
             for fn, target in events:
                 event.listen(self, target, fn)
index 664decbf1efb181eecbfa8b9d703af34e43057be..a7b10946b60f9328f05ee1336dddf00868930bbf 100644 (file)
@@ -257,7 +257,7 @@ class TestPropagate(TestBase):
 
         t2 = Target()
         
-        t2.dispatch.update(t1.dispatch)
+        t2.dispatch._update(t1.dispatch)
         
         t2.dispatch.on_event_one(t2, 1)
         t2.dispatch.on_event_two(t2, 2)
index c26223ad40d632089a743532592d44d37cb2842d..b36f52ac26c72f8ff6a81135f52976ce5c652e78 100644 (file)
@@ -970,7 +970,7 @@ class M2OCascadeDeleteOrphanTestOne(_base.MappedTest):
 
         # the error condition relies upon
         # these things being true
-        assert User.foo.dispatch.active_history is False
+        assert User.foo.dispatch._active_history is False
         eq_(
             attributes.get_history(u1, 'foo'),
             ([None], (), [attributes.PASSIVE_NO_RESULT])
index f7ba025e06fef267ffcb038ad1a85f9a5848b787..d624772d04b7b740067fa1358d95f597c1ecb4e9 100644 (file)
@@ -581,7 +581,7 @@ class ExtendedEventsTest(_base.ORMTest):
 
         instrumentation.register_class(A)
         manager = instrumentation.manager_of_class(A)
-        assert issubclass(manager.dispatch.parent_cls.__dict__['dispatch'].events, MyEvents)
+        assert issubclass(manager.dispatch._parent_cls.__dict__['dispatch'].events, MyEvents)
 
 
 class NativeInstrumentationTest(_base.ORMTest):