diff util.py @ 13:591ecc2a99bd

move logging configuration to the util module, configure logging for sqlalchemy
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 28 Apr 2010 02:23:00 +0200
parents database.py@9ede118b93ef
children 72dfae865899
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util.py	Wed Apr 28 02:23:00 2010 +0200
@@ -0,0 +1,31 @@
+
+from ConfigParser import ConfigParser
+import logging
+import Mapping
+import socket
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+logger = logging.getLogger("database")
+
+def configureLogging():
+    logging.basicConfig(level=logging.DEBUG)
+    logging.getLogger("sqlalchemy").setLevel(logging.INFO)
+    logging.getLogger("sqlalchemy.orm").setLevel(logging.WARN)
+
+def createSession():
+    databaseUrl = loadDatabaseUrl()
+    engine = create_engine(databaseUrl,echo=True)
+    Mapping.createMapping(engine)
+    SessionMaker = sessionmaker(bind = engine)
+    session = SessionMaker()
+    return session
+
+def loadDatabaseUrl():
+    hostname = socket.gethostname()
+    filename = "database-%s.ini" % hostname
+    logger.debug("loading database configuration from " + filename)
+
+    parser = ConfigParser();
+    parser.read(filename)
+    return parser.get("database", "url")