]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
a little cleanup, but we probably need a generalized "propagate" mechanism
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Nov 2010 21:19:08 +0000 (17:19 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Nov 2010 21:19:08 +0000 (17:19 -0400)
lib/sqlalchemy/event.py
lib/sqlalchemy/orm/events.py
lib/sqlalchemy/orm/instrumentation.py
lib/sqlalchemy/orm/mapper.py

index 379c3f1dd022d065982e1b58adadb0512d27284e..5f46a115377ac725abb8ab8af16393181877dcaf 100644 (file)
@@ -69,11 +69,6 @@ class _Dispatch(object):
 
         for ls in other.descriptors:
             getattr(self, ls.name).update(ls)
-            #existing_listeners = getattr(self, ls.name).listeners
-            #existing_listener_set = set(existing_listeners)
-            #existing_listeners.extend([l for l 
-            #                        in ls.listeners 
-            #                        if l not in existing_listener_set])
 
 class _EventMeta(type):
     """Intercept new Event subclasses and create 
index 5765b9ea8bafcc69c2e8ea24dc9c126c6d5a12b8..36c12cf8aea2786c3b6849d20768d27430841766 100644 (file)
@@ -59,7 +59,12 @@ class InstrumentationEvents(event.Events):
         """Called when an attribute is instrumented."""
 
 class InstanceEvents(event.Events):
+    """Define events specific to object lifecycle.
     
+    Instance-level don't automatically propagate their associations
+    to subclasses.
+    
+    """
     @classmethod
     def accept_with(cls, target):
         from sqlalchemy.orm.instrumentation import ClassManager, manager_of_class
@@ -75,9 +80,12 @@ class InstanceEvents(event.Events):
     @classmethod
     def listen(cls, fn, identifier, target, raw=False):
         if not raw:
-            fn = _to_instance(fn)
+            orig_fn = fn
+            def wrap(state, *arg, **kw):
+                return orig_fn(state.obj(), *arg, **kw)
+            fn = wrap
         event.Events.listen(fn, identifier, target)
-
+        
     @classmethod
     def remove(cls, fn, identifier, target):
         raise NotImplementedError("Removal of instance events not yet implemented")
@@ -443,6 +451,9 @@ class AttributeEvents(event.Events):
         event.Events.listen(fn, identifier, target)
         
         if propagate:
+
+            raise NotImplementedError()
+
             # TODO: for removal, need to implement
             # packaging this info for operation in reverse.
 
@@ -452,9 +463,6 @@ class AttributeEvents(event.Events):
                 if impl is not target:
                     event.Events.listen(fn, identifier, impl)
             
-            def configure_listener(class_, key, inst):
-                event.Events.listen(fn, identifier, inst)
-            event.listen(configure_listener, 'on_attribute_instrument', class_)
         
     @classmethod
     def remove(cls, fn, identifier, target):
@@ -512,9 +520,3 @@ class AttributeEvents(event.Events):
 
         """
 
-@util.decorator
-def _to_instance(fn, state, *arg, **kw):
-    """Marshall the :class:`.InstanceState` argument to an instance."""
-    
-    return fn(state.obj(), *arg, **kw)
-    
index 52c1c7213c061c8621156ffe4cfc9de533dc72ee..6e357e15795c5f77dd66dd3b73d626dab75e07cc 100644 (file)
@@ -211,8 +211,9 @@ class ClassManager(dict):
         if key in self.mutable_attributes:
             self.mutable_attributes.remove(key)
         for cls in self.class_.__subclasses__():
-            manager = self._subclass_manager(cls)
-            manager.uninstrument_attribute(key, True)
+            manager = manager_of_class(cls) 
+            if manager:
+                manager.uninstrument_attribute(key, True)
 
     def unregister(self):
         """remove all instrumentation established by this ClassManager."""
index 03e313685e04c416dfa268883bb6333a8fefe0d9..7fdf21c6cbe7d9190443f92c5cfb98efe20837fc 100644 (file)
@@ -405,7 +405,6 @@ class Mapper(object):
             return
 
         event.listen(_event_on_init, 'on_init', manager, raw=True)
-        event.listen(_event_on_init_failure, 'on_init_failure', manager, raw=True)
         event.listen(_event_on_resurrect, 'on_resurrect', manager, raw=True)
         
         for key, method in util.iterate_attributes(self.class_):
@@ -2409,22 +2408,19 @@ def _event_on_load(state):
 def _event_on_init(state, args, kwargs):
     """Trigger mapper compilation and run init_instance hooks."""
 
-    instrumenting_mapper = state.manager.info[_INSTRUMENTOR]
-    # compile() always compiles all mappers
-    instrumenting_mapper.compile()
-
-def _event_on_init_failure(state, args, kwargs):
-    """Run init_failed hooks."""
-
-    instrumenting_mapper = state.manager.info[_INSTRUMENTOR]
+    instrumenting_mapper = state.manager.info.get(_INSTRUMENTOR)
+    if instrumenting_mapper:
+        # compile() always compiles all mappers
+        instrumenting_mapper.compile()
 
 def _event_on_resurrect(state):
     # re-populate the primary key elements
     # of the dict based on the mapping.
-    instrumenting_mapper = state.manager.info[_INSTRUMENTOR]
-    for col, val in zip(instrumenting_mapper.primary_key, state.key[1]):
-        instrumenting_mapper._set_state_attr_by_column(
-                                        state, state.dict, col, val)
+    instrumenting_mapper = state.manager.info.get(_INSTRUMENTOR)
+    if instrumenting_mapper:
+        for col, val in zip(instrumenting_mapper.primary_key, state.key[1]):
+            instrumenting_mapper._set_state_attr_by_column(
+                                            state, state.dict, col, val)
     
     
 def _sort_states(states):