annotate Database.py @ 108:e50d446f9942 python3

updates fro python3
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 29 Mar 2011 03:17:30 +0200
parents 842727971796
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
1
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
2 import Mapping
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
3 import sqlalchemy
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
4 import sqlalchemy.orm
37
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
5 import sys
57
254d5b89a6ca make sqlalchemy logging configurable through the --databaseLogging commandline parameter
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 37
diff changeset
6 import util
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
7
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
8 # Keep the connection to the database only once. The feed updater and the GUI app will
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
9 # operate on a single engine/session but this comes in handy for interactive use
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
10 engine = None
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11 SessionMaker = None
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12
70
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
13 def createSession(databaseUrl=None):
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
14 if databaseUrl is None:
842727971796 have the DB URL as parameter when creating a session and fall back to commandline arguments if no DB URL was passed in
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 57
diff changeset
15 databaseUrl = _getDatabaseUrl()
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
16 initEngine(databaseUrl)
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
17 Mapping.createMapping(engine)
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
18 initSessionMaker()
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
19 return SessionMaker()
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
20
37
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
21 def _getDatabaseUrl():
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
22 if len(sys.argv) < 2:
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
23 print("Usage: %s <database url>" % (sys.argv[0]))
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
24 sys.exit(1)
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
25 return sys.argv[1]
22214d79ed41 database URL must be given as commandline argument now, no need for creating complicated config files. Add a menu entry for opening the selected article in browser.
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 34
diff changeset
26
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
27 def initEngine(databaseUrl):
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
28 global engine
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
29 if engine is None:
57
254d5b89a6ca make sqlalchemy logging configurable through the --databaseLogging commandline parameter
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 37
diff changeset
30 verbose = util.databaseLoggingEnabled()
254d5b89a6ca make sqlalchemy logging configurable through the --databaseLogging commandline parameter
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 37
diff changeset
31 engine = sqlalchemy.create_engine(databaseUrl, echo=verbose)
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
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
32
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
33 def initSessionMaker():
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
34 global SessionMaker
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
35 if SessionMaker is None:
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
36 SessionMaker = sqlalchemy.orm.sessionmaker(bind=engine)
5813e3c10f14 move the database logic out into its own module. Make everything reload safe so that multiple sessions can be created from interactive sessions
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
37