view backend/sqlalchemy/Database.py @ 242:03e3ebb1d52f

Disable the use of a proxy when updating feeds - https traffic does not seem to work currently over a proxy.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 08 Jun 2015 19:20:46 +0200
parents bb3c851b18b1
children
line wrap: on
line source

# -*- coding: utf-8 -*-
from sqlalchemy.engine import create_engine
from sqlalchemy.orm import sessionmaker
import Mapping
import argparse
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=None):
    if databaseUrl is None:
        databaseUrl = _getDatabaseUrl()
    initEngine(databaseUrl)
    Mapping.createMapping(engine)
    initSessionMaker()
    return SessionMaker()

def _getDatabaseUrl():
    parser = argparse.ArgumentParser()
    parser.add_argument("--dburl", nargs="?", required=True, help="Database URL for the sqlalchemy backend")
    args = parser.parse_known_args()
    return args[0].dburl

def initEngine(databaseUrl):
    global engine
    if engine is None:
        verbose = util.databaseLoggingEnabled()
        engine = create_engine(databaseUrl, echo=verbose)

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