save point (see note)

Changed license to AGPLv3, "finished" console command framework (for now?), and
added initial db/lib/pyramid/wx subpackages - though those are not yet well tested.
This commit is contained in:
Lance Edgar 2012-03-20 09:11:01 -05:00
parent 3d75732d36
commit a6decbb313
36 changed files with 2910 additions and 172 deletions

134
edbob/db/__init__.py Normal file
View file

@ -0,0 +1,134 @@
#!/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
Session = sessionmaker()
def init():
"""
Called whenever ``'edbob.db'`` is configured to be auto-initialized.
This function is responsible for establishing the primary database engine
(a ``sqlalchemy.Engine`` instance, read from config), and extending the
root ``edbob`` namespace with the ORM classes (``Person``, ``User``, etc.),
as well as a few other things, e.g. ``engine``, ``Session`` and
``metadata``.
In addition to this, if a connection to the primary database can be
obtained, it will be consulted to see which extensions are active within
it. If any are found, edbob's ORM will be extended in-place accordingly.
"""
config = edbob.config.get_dict('edbob.db')
engine = engine_from_config(config)
edbob.graft(edbob, locals(), 'engine')
Session.configure(bind=engine)
edbob.graft(edbob, globals(), 'Session')
from edbob.db.model import get_metadata
metadata = get_metadata(bind=engine)
edbob.graft(edbob, locals(), 'metadata')
from edbob.db.mappers import make_mappers
make_mappers(metadata)
from edbob.db.ext import extend_framework
extend_framework()
# Note that we extend the framework before we graft the 'classes' module
# contents, since extensions may graft things to that module.
import edbob.db.classes as classes
edbob.graft(edbob, classes)
# Same goes for the enum module.
import edbob.db.enum as enum
edbob.graft(edbob, enum)
# Add settings functions.
edbob.graft(edbob, globals(), ('get_setting', 'save_setting'))
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