view Database.py @ 47:a8442c3487b5

add an option to Feed that allows loading an entry's link right away instead of displaying a feed's summary
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 16 May 2010 08:13:07 +0200
parents 22214d79ed41
children 254d5b89a6ca
line wrap: on
line source


import Mapping
import sqlalchemy
import sqlalchemy.orm
import sys

# 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 = _getDatabaseUrl()
    initEngine(databaseUrl)
    Mapping.createMapping(engine)
    initSessionMaker()
    return SessionMaker()

def _getDatabaseUrl():
    if len(sys.argv) < 2:
        print("Usage: %s <database url>" % (sys.argv[0]))
        sys.exit(1)
    return sys.argv[1]

def initEngine(databaseUrl):
    global engine
    if engine is None:
        engine = sqlalchemy.create_engine(databaseUrl, echo=True)

def initSessionMaker():
    global SessionMaker
    if SessionMaker is None:
        SessionMaker = sqlalchemy.orm.sessionmaker(bind=engine)