| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | import cherrypyfrom cherrypy.process import wspbus, pluginsfrom sqlalchemy import create_enginefrom sqlalchemy.orm import scoped_session, sessionmaker__all__ = ['SAEnginePlugin']        class SAEnginePlugin(plugins.SimplePlugin):    def __init__(self, bus, connection_string=None):        """        The plugin is registered to the CherryPy engine and therefore        is part of the bus (the engine *is* a bus) registery.         We use this plugin to create the SA engine. At the same time,        when the plugin starts we create the tables into the database        using the mapped class of the global metadata.        """        plugins.SimplePlugin.__init__(self, bus)        self.sa_engine = None        self.connection_string = connection_string        self.session = scoped_session(sessionmaker(autoflush=True,                                                   autocommit=False))     def start(self):        self.bus.log('Starting up DB access')        self.sa_engine = create_engine(self.connection_string, echo=False)        self.bus.subscribe("bind-session", self.bind)        self.bus.subscribe("commit-session", self.commit)     def stop(self):        self.bus.log('Stopping down DB access')        self.bus.unsubscribe("bind-session", self.bind)        self.bus.unsubscribe("commit-session", self.commit)        if self.sa_engine:            self.sa_engine.dispose()            self.sa_engine = None     def bind(self):        """        Whenever this plugin receives the 'bind-session' command, it applies        this method and to bind the current session to the engine.        It then returns the session to the caller.        """        self.session.configure(bind=self.sa_engine)        return self.session    def commit(self):        """        Commits the current transaction or rollbacks if an error occurs.        In all cases, the current session is unbound and therefore        not usable any longer.        """        try:            self.session.commit()        except:            self.session.rollback()              raise        finally:            self.session.remove()
 |