[Checkins] SVN: Sandbox/faassen/rdbintegration/trunk/src/rdbintegration/app.py Update according to latest discussions.

Martijn Faassen faassen at infrae.com
Tue Jun 17 13:06:04 EDT 2008


Log message for revision 87463:
  Update according to latest discussions.
  

Changed:
  U   Sandbox/faassen/rdbintegration/trunk/src/rdbintegration/app.py

-=-
Modified: Sandbox/faassen/rdbintegration/trunk/src/rdbintegration/app.py
===================================================================
--- Sandbox/faassen/rdbintegration/trunk/src/rdbintegration/app.py	2008-06-17 16:56:03 UTC (rev 87462)
+++ Sandbox/faassen/rdbintegration/trunk/src/rdbintegration/app.py	2008-06-17 17:06:04 UTC (rev 87463)
@@ -41,7 +41,6 @@
 from zope.interface import Interface
 import thread
 from sqlalchemy.orm import mapper
-from sqlalchemy.orm.session import Session as _Session
 from zope.sqlalchemy import ZopeTransactionExtension
 from sqlalchemy import create_engine
 import sqlalchemy as sa
@@ -49,29 +48,35 @@
 class IDatabase(Interface):
     """A utility that specifies the database.
     """
-    
-    def engine():
-        """Get the engine in use for this database session.
 
-        The engine is not created on the fly. Each time the engine is
-        looked up for a site (application), the same engine should be found.
-        """
+    def session_factory():
+        """Create the session.
 
-    def configuration():
-        """Get all configuration parameters for Session.
+        The session needs to be configured with the right engine and
+        parameters.
 
-        This should give the configuration parameters to be used for
-        a session in this site.
+        The session is only created one per thread/application combination.
         """
 
-    def id():
-        """Get unique id for this database configuration.
-
-        This should be unique per site (application).
+    def scopefunc():
+        """Determine the scope of the session.
+        
+        This should at the very least be unique per thread.
         """
 
 class Database(grok.LocalUtility):
     grok.implements(IDatabase)
+
+    def session_factory(self):
+        print "creating new session"
+        return sa.orm.create_session(**self.configuration())
+
+    def scopefunc(self):
+        # we use the application name as the unique per application id.
+        # Can we use something more clever and universally working?
+        result = (thread.get_ident(), self.__parent__.__name__)
+        print "scopefunc:", result
+        return result
     
     def engine(self):
         # we look up the engine with the name defined in the application
@@ -86,49 +91,37 @@
             autoflush=True,
             extension=ZopeTransactionExtension())
 
-    def id(self):
-        # we use the application name as the unique id. Can we use
-        # something more clever and universally working?
-        return self.__parent__.__name__
-
-class IEngine(Interface):
-    """The database engine.
-    """
-
-# we register the available engines as global utilities.
-# we want to be able to configure the engines, preferably also through
-# the UI. This might mean we need to register the engine as a local,
-# non-persistent utility that is somehow recreated on each restart.
-engine1 = create_engine('postgres:///experiment', convert_unicode=True)
-grok.global_utility(engine1, provides=IEngine, direct=True, name='engine1')
-
-engine2 = create_engine('postgres:///experiment2', convert_unicode=True)
-grok.global_utility(engine2, provides=IEngine, direct=True, name='engine2')
-        
 def session_factory():
     """This is used by scoped session to create a new Session object.
     """
     utility = component.getUtility(IDatabase)
-    print "Creating new session"
-    return _Session(**utility.configuration())
+    return utility.session_factory()
 
 def scopefunc():
     """This is used by scoped session to distinguish between sessions.
-
-    We distinguish between sessions by thread id and IDatabase unique
-    application id.
     """
     utility = component.getUtility(IDatabase)
-    result = (thread.get_ident(), utility.id())
-    print "Scope: ", result
-    return result
+    return utility.scopefunc()
 
 # this is a frame-work central configuration, we only need to do this
 # once in our integration framework, after which we can just import
 # Session
-Session = scoped_session(session_factory,
-                         scopefunc)
+Session = scoped_session(session_factory, scopefunc)
 
+class IEngine(Interface):
+    """The database engine.
+    """
+    
+# we register the available engines as global utilities.
+# we want to be able to configure the engines, preferably also through
+# the UI. This might mean we need to register the engine as a local,
+# non-persistent utility that is somehow recreated on each restart.
+engine1 = create_engine('postgres:///experiment', convert_unicode=True)
+grok.global_utility(engine1, provides=IEngine, direct=True, name='engine1')
+
+engine2 = create_engine('postgres:///experiment2', convert_unicode=True)
+grok.global_utility(engine2, provides=IEngine, direct=True, name='engine2')
+
 # an application that allows the configuration of the engine name
 class IForm(Interface):
     engine_name = schema.TextLine(title=u"Engine name")



More information about the Checkins mailing list