svv/application.py
changeset 6 72c73df76db2
child 7 bbac4b0f4320
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/svv/application.py	Wed Dec 22 02:52:25 2010 +0200
@@ -0,0 +1,52 @@
+"""
+    Application runtime state
+"""
+
+from sqlalchemy import create_engine
+
+import logging
+
+from svv import database as db
+
+log = logging.getLogger('svv.app')
+
+class Application (object) :
+    """
+        Our core run-time state, which e.g. WSGIAoo acts as a frontend to
+    """
+
+    def __init__ (self, db_url) :
+        """
+            Initialize app, connecting to database.
+
+                db_url      - sqlalchemy-style URL to database
+        """
+        
+        if db_url :
+            # connect
+            self.engine = create_engine(db_url)
+
+            log.info("Connected to database %s", self.engine)
+
+        else :
+            self.engine = None
+
+    def create_tables (self) :
+        """
+            Initialize the database if new.
+        """
+
+        log.warn("Creating database tables...")
+
+        db.schema.create_all(self.engine)
+
+    def get_connection (self) :
+        """
+            Return an active sqlalchemy Connection for our database.
+        """
+        
+        if not self.engine :
+            raise RuntimeError("No database configured")
+
+        return self.engine.connect()
+