Mercurial > hg > Feedworm
changeset 34:5813e3c10f14
move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Wed, 05 May 2010 02:14:05 +0200 |
parents | f371d02fa09d |
children | aaec263f07ca |
files | Database.py Mapping.py feeds.txt feedupdate-main.py util.py |
diffstat | 5 files changed, 60 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Database.py Wed May 05 02:14:05 2010 +0200 @@ -0,0 +1,28 @@ + +import Mapping +import sqlalchemy +import sqlalchemy.orm +import util + +# Keep the connection to the database only once. The feed updater and the GUI app will +# operate on a single engine/session but this comes in handy for interactive use +engine = None +SessionMaker = None + +def createSession(): + databaseUrl = util.loadDatabaseUrl() + initEngine(databaseUrl) + Mapping.createMapping(engine) + initSessionMaker() + return SessionMaker() + +def initEngine(databaseUrl): + global engine + if engine is None: + engine = sqlalchemy.create_engine(databaseUrl, echo=False) + +def initSessionMaker(): + global SessionMaker + if SessionMaker is None: + SessionMaker = sqlalchemy.orm.sessionmaker(bind=engine) + \ No newline at end of file
--- a/Mapping.py Fri Apr 30 06:22:33 2010 +0200 +++ b/Mapping.py Wed May 05 02:14:05 2010 +0200 @@ -12,7 +12,17 @@ from sqlalchemy.orm import mapper from sqlalchemy.orm import relation +mappingDefined = False + def createMapping(engine): + """ Make sure the mapping is defined only once. This is not really needed for the feed updater + or the GUI app but comes in handy when working interactively with the system. """ + global mappingDefined + if not mappingDefined: + _createMapping(engine) + mappingDefined = True + +def _createMapping(engine): metadata = MetaData(engine) feedTable = Table("feed", metadata,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/feeds.txt Wed May 05 02:14:05 2010 +0200 @@ -0,0 +1,3 @@ +Joel on Software|http://www.joelonsoftware.com/rss.xml +Ruhrbarone|http://www.ruhrbarone.de/feed/ +TheServerSide : Thread List - News|http://www.theserverside.com/rss/theserverside-rss2.xml
--- a/feedupdate-main.py Fri Apr 30 06:22:33 2010 +0200 +++ b/feedupdate-main.py Wed May 05 02:14:05 2010 +0200 @@ -1,9 +1,10 @@ #!/usr/bin/env python +import Database from Feed import Feed from FeedUpdater import updateAllFeeds import logging -from util import configureLogging, createSession +import util logger = logging.getLogger("feedupdater") @@ -21,9 +22,10 @@ print entry.title if __name__ == "__main__": - configureLogging() - session = createSession() + util.configureLogging() + session = Database.createSession() - createFeed(session) - updateAllFeeds(session) + #createFeed(session) + #updateAllFeeds(session) #listFeeds(session) + #session.close()
--- a/util.py Fri Apr 30 06:22:33 2010 +0200 +++ b/util.py Wed May 05 02:14:05 2010 +0200 @@ -1,10 +1,8 @@ from ConfigParser import ConfigParser +from Feed import Feed import logging -import Mapping import socket -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker logger = logging.getLogger("database") @@ -13,14 +11,6 @@ logging.getLogger("sqlalchemy").setLevel(logging.INFO) logging.getLogger("sqlalchemy.orm").setLevel(logging.WARN) -def createSession(): - databaseUrl = loadDatabaseUrl() - engine = create_engine(databaseUrl,echo=False) - Mapping.createMapping(engine) - SessionMaker = sessionmaker(bind = engine) - session = SessionMaker() - return session - def loadDatabaseUrl(): hostname = socket.gethostname() filename = "database-%s.ini" % hostname @@ -29,3 +19,14 @@ parser = ConfigParser(); parser.read(filename) return parser.get("database", "url") + +def loadFeeds(session=None, filename="feeds.txt"): + file = open(filename) + for line in file: + (title, rss_url) = line.split("|") + # remove the newline + rss_url = rss_url.rstrip() + feed = Feed(title, rss_url) + session.add(feed) + file.close() + session.commit()