changeset 12:9ede118b93ef

move session creation into its own module
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 28 Apr 2010 02:09:48 +0200
parents e87c54b3a216
children 591ecc2a99bd
files database.py feedupdate-main.py
diffstat 2 files changed, 24 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/database.py	Wed Apr 28 02:09:48 2010 +0200
@@ -0,0 +1,22 @@
+
+from ConfigParser import ConfigParser
+import Mapping
+import socket
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+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
+
+    parser = ConfigParser();
+    parser.read(filename)
+    return parser.get("database", "url")
--- a/feedupdate-main.py	Tue Apr 27 20:18:22 2010 +0200
+++ b/feedupdate-main.py	Wed Apr 28 02:09:48 2010 +0200
@@ -1,24 +1,12 @@
 #!/usr/bin/env python
 
-from ConfigParser import ConfigParser
+from database import createSession
 from Feed import Feed
 from FeedUpdater import updateAllFeeds
-import Mapping
 import logging
-import socket
-from sqlalchemy import create_engine
-from sqlalchemy.orm import sessionmaker
 
 logger = logging.getLogger("feedupdater")
 
-def loadDatabaseProperties():
-    hostname = socket.gethostname()
-    filename = "database-%s.ini" % hostname
-
-    parser = ConfigParser();
-    parser.read(filename)
-    return parser.get("database", "url")
-
 def createFeed(session):
     if len(session.query(Feed).all()) == 0:
         newFeed = Feed("Joel on Software", "http://www.joelonsoftware.com/rss.xml")
@@ -33,11 +21,7 @@
             print entry.title
 
 if __name__ == "__main__":
-    databaseUrl = loadDatabaseProperties()
-    engine = create_engine(databaseUrl,echo=True)
-    Mapping.createMapping(engine)
-    SessionMaker = sessionmaker(bind = engine)
-    session = SessionMaker()
+    session = createSession()
     
     createFeed(session)
     updateAllFeeds(session)