edbob/edbob/db/__init__.py
Lance Edgar 8ceb98baf4 save point (see note)
Added initial database schema and ability to install it, added init-db command
to pyramid scaffold.
2012-03-24 12:15:11 -05:00

159 lines
4.3 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.db`` -- Database Framework
"""
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
import edbob
__all__ = ['engines', 'engine', 'Session', 'metadata',
'get_setting', 'save_setting']
inited = False
engines = None
engine = None
Session = sessionmaker()
metadata = None
def init(config):
"""
Initializes the database connection(s); called by :func:`edbob.init()` if
config includes something like::
.. highlight:: ini
[edbob]
init = ['edbob.db']
[edbob.db]
sqlalchemy.urls = {
'default': 'postgresql://user:pass@localhost/edbob,
}
This function reads connection info from ``config`` and builds a dictionary
or :class:`sqlalchemy.Engine` instances accordingly. It also extends the
root ``edbob`` namespace with the ORM classes (:class:`edbob.Person`,
:class:`edbob.User`, etc.), as well as a few other things
(e.g. :attr:`edbob.engine`, :attr:`edbob.Session`, :attr:`edbob.metadata`).
"""
import edbob.db
from edbob.db import classes
from edbob.db import enum
from edbob.db.model import get_metadata
from edbob.db.mappers import make_mappers
from edbob.db.extensions import extend_framework
global inited, engines, engine, metadata
keys = config.get('edbob.db', 'sqlalchemy.keys')
if keys:
keys = keys.split()
else:
keys = ['default']
engines = {}
cfg = config.get_dict('edbob.db')
for key in keys:
try:
engines[key] = engine_from_config(cfg, 'sqlalchemy.%s.' % key)
except KeyError:
if key == 'default':
try:
engines[key] = engine_from_config(cfg)
except KeyError:
pass
engine = engines.get('default')
if engine:
Session.configure(bind=engine)
metadata = get_metadata(bind=engine)
make_mappers(metadata)
extend_framework()
edbob.graft(edbob, edbob.db)
edbob.graft(edbob, classes)
edbob.graft(edbob, enum)
inited = True
def get_setting(name, session=None):
"""
Returns a setting from the database.
"""
_session = session
if not session:
session = Session()
setting = session.query(edbob.Setting).get(name)
if setting:
setting = setting.value
if not _session:
session.close()
return setting
def save_setting(name, value, session=None):
"""
Saves a setting to the database.
"""
_session = session
if not session:
session = Session()
setting = session.query(edbob.Setting).get(name)
if not setting:
setting = edbob.Setting(name=name)
session.add(setting)
setting.value = value
if not _session:
session.commit()
session.close()
def needs_session(func):
"""
Decorator which adds helpful session handling.
"""
def wrapped(*args, **kwargs):
session = kwargs.get('session')
_session = session
if not session:
session = Session()
kwargs['session'] = session
res = func(session, *args, **kwargs)
if not _session:
session.commit()
session.close()
return res
return wrapped