diff options
author | Fabian Affolter <fabian@bernewireless.net> | 2010-12-08 23:27:15 +0100 |
---|---|---|
committer | Fabian Affolter <fabian@bernewireless.net> | 2010-12-08 23:27:15 +0100 |
commit | e833a7a76b231cd346f09c9a422ecb855d1cc6b4 (patch) | |
tree | e2762fc31e1162a6859dbd6cd7b5c28208296d9d /build/lib/Bcfg2/Server/Snapshots/__init__.py | |
parent | baf5854f414c2a35887e3fbbcfd094e12e7c6ee7 (diff) | |
download | bcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.tar.gz bcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.tar.bz2 bcfg2-e833a7a76b231cd346f09c9a422ecb855d1cc6b4.zip |
Merge with upstream
Diffstat (limited to 'build/lib/Bcfg2/Server/Snapshots/__init__.py')
-rw-r--r-- | build/lib/Bcfg2/Server/Snapshots/__init__.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/build/lib/Bcfg2/Server/Snapshots/__init__.py b/build/lib/Bcfg2/Server/Snapshots/__init__.py new file mode 100644 index 000000000..6018377cb --- /dev/null +++ b/build/lib/Bcfg2/Server/Snapshots/__init__.py @@ -0,0 +1,30 @@ +__all__ = ['models', 'db_from_config', 'setup_session'] + +import sqlalchemy +import sqlalchemy.orm +import ConfigParser + + +def db_from_config(cfile): + cp = ConfigParser.ConfigParser() + cp.read([cfile]) + driver = cp.get('snapshots', 'driver') + if driver == 'sqlite': + path = cp.get('snapshots', 'database') + return 'sqlite:///%s' % path + elif driver in ['mysql', 'postgres']: + user = cp.get('snapshots', 'user') + password = cp.get('snapshots', 'password') + host = cp.get('snapshots', 'host') + db = cp.get('snapshots', 'database') + return '%s://%s:%s@%s/%s' % (driver, user, password, host, db) + else: + raise Exception, "unsupported db driver %s" % driver + + +def setup_session(cfile, debug=False): + engine = sqlalchemy.create_engine(db_from_config(cfile), + echo=debug) + Session = sqlalchemy.orm.sessionmaker() + Session.configure(bind=engine) + return Session() |