def _adapt_listener_methods(cls, self, listener, methods):

        for meth in methods:
            me_meth = getattr(MapperExtension, meth)
            ls_meth = getattr(listener, meth)

            if not util.methods_equivalent(me_meth, ls_meth):
                if meth == 'reconstruct_instance':

                    def go(ls_meth):
                        def reconstruct(instance, ctx):
                            ls_meth(self, instance)

                        return reconstruct

                    event.listen(self.class_manager,
                                 'load',
                                 go(ls_meth),
                                 raw=False,
                                 propagate=True)
                elif meth == 'init_instance':

                    def go(ls_meth):
                        def init_instance(instance, args, kwargs):
                            ls_meth(self, self.class_,
                                    self.class_manager.original_init, instance,
                                    args, kwargs)

                        return init_instance

                    event.listen(self.class_manager,
                                 'init',
                                 go(ls_meth),
                                 raw=False,
                                 propagate=True)
                elif meth == 'init_failed':

                    def go(ls_meth):
                        def init_failed(instance, args, kwargs):
                            util.warn_exception(
                                ls_meth, self, self.class_,
                                self.class_manager.original_init, instance,
                                args, kwargs)

                        return init_failed

                    event.listen(self.class_manager,
                                 'init_failure',
                                 go(ls_meth),
                                 raw=False,
                                 propagate=True)
                else:
                    event.listen(self,
                                 "%s" % meth,
                                 ls_meth,
                                 raw=False,
                                 retval=True,
                                 propagate=True)
Esempio n. 2
0
    def _adapt_listener(cls, self, listener):
        for meth in [
            'before_commit',
            'after_commit',
            'after_rollback',
            'before_flush',
            'after_flush',
            'after_flush_postexec',
            'after_begin',
            'after_attach',
            'after_bulk_update',
            'after_bulk_delete',
        ]:
            me_meth = getattr(SessionExtension, meth)
            ls_meth = getattr(listener, meth)

            if not util.methods_equivalent(me_meth, ls_meth):
                event.listen(self, meth, getattr(listener, meth))
    def _adapt_listener_methods(cls, self, listener, methods):

        for meth in methods:
            me_meth = getattr(MapperExtension, meth)
            ls_meth = getattr(listener, meth)

            if not util.methods_equivalent(me_meth, ls_meth):
                if meth == 'reconstruct_instance':
                    def go(ls_meth):
                        def reconstruct(instance, ctx):
                            ls_meth(self, instance)
                        return reconstruct
                    event.listen(self.class_manager, 'load',
                                        go(ls_meth), raw=False, propagate=True)
                elif meth == 'init_instance':
                    def go(ls_meth):
                        def init_instance(instance, args, kwargs):
                            ls_meth(self, self.class_,
                                        self.class_manager.original_init,
                                        instance, args, kwargs)
                        return init_instance
                    event.listen(self.class_manager, 'init',
                                        go(ls_meth), raw=False, propagate=True)
                elif meth == 'init_failed':
                    def go(ls_meth):
                        def init_failed(instance, args, kwargs):
                            util.warn_exception(ls_meth, self, self.class_,
                                            self.class_manager.original_init,
                                            instance, args, kwargs)

                        return init_failed
                    event.listen(self.class_manager, 'init_failure',
                                        go(ls_meth), raw=False, propagate=True)
                else:
                    event.listen(self, "%s" % meth, ls_meth,
                                        raw=False, retval=True, propagate=True)