Mercurial > hg > Feedworm
annotate util.py @ 13:591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Wed, 28 Apr 2010 02:23:00 +0200 |
parents | database.py@9ede118b93ef |
children | 72dfae865899 |
rev | line source |
---|---|
12
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
1 |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
2 from ConfigParser import ConfigParser |
13
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
3 import logging |
12
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
4 import Mapping |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
5 import socket |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
6 from sqlalchemy import create_engine |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
7 from sqlalchemy.orm import sessionmaker |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
8 |
13
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
9 logger = logging.getLogger("database") |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
10 |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
11 def configureLogging(): |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
12 logging.basicConfig(level=logging.DEBUG) |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
13 logging.getLogger("sqlalchemy").setLevel(logging.INFO) |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
14 logging.getLogger("sqlalchemy.orm").setLevel(logging.WARN) |
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
15 |
12
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
16 def createSession(): |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
17 databaseUrl = loadDatabaseUrl() |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
18 engine = create_engine(databaseUrl,echo=True) |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
19 Mapping.createMapping(engine) |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
20 SessionMaker = sessionmaker(bind = engine) |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
21 session = SessionMaker() |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
22 return session |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
23 |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
24 def loadDatabaseUrl(): |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
25 hostname = socket.gethostname() |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
26 filename = "database-%s.ini" % hostname |
13
591ecc2a99bd
move logging configuration to the util module, configure logging for sqlalchemy
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
12
diff
changeset
|
27 logger.debug("loading database configuration from " + filename) |
12
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
28 |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
29 parser = ConfigParser(); |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
30 parser.read(filename) |
9ede118b93ef
move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
31 return parser.get("database", "url") |