annotate database.py @ 12:9ede118b93ef

move session creation into its own module
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 28 Apr 2010 02:09:48 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
3 import Mapping
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
4 import socket
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
5 from sqlalchemy import create_engine
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
6 from sqlalchemy.orm import sessionmaker
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
7
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
8 def createSession():
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
9 databaseUrl = loadDatabaseUrl()
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
10 engine = create_engine(databaseUrl,echo=True)
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11 Mapping.createMapping(engine)
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12 SessionMaker = sessionmaker(bind = engine)
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
13 session = SessionMaker()
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
14 return session
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
15
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
16 def loadDatabaseUrl():
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
17 hostname = socket.gethostname()
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
18 filename = "database-%s.ini" % hostname
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
19
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
20 parser = ConfigParser();
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
21 parser.read(filename)
9ede118b93ef move session creation into its own module
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
22 return parser.get("database", "url")