convert db module to unix format
This commit is contained in:
parent
b939bc42aa
commit
c371a2d997
2 changed files with 230 additions and 230 deletions
|
@ -1,184 +1,184 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
# edbob -- Pythonic Software Framework
|
# edbob -- Pythonic Software Framework
|
||||||
# Copyright © 2010-2012 Lance Edgar
|
# Copyright © 2010-2012 Lance Edgar
|
||||||
#
|
#
|
||||||
# This file is part of edbob.
|
# This file is part of edbob.
|
||||||
#
|
#
|
||||||
# edbob is free software: you can redistribute it and/or modify it under the
|
# 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
|
# 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)
|
# Software Foundation, either version 3 of the License, or (at your option)
|
||||||
# any later version.
|
# any later version.
|
||||||
#
|
#
|
||||||
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
|
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
||||||
# more details.
|
# more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with edbob. If not, see <http://www.gnu.org/licenses/>.
|
# along with edbob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
"""
|
"""
|
||||||
``edbob.db`` -- Database Framework
|
``edbob.db`` -- Database Framework
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config, MetaData
|
from sqlalchemy import engine_from_config, MetaData
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
import edbob
|
import edbob
|
||||||
|
|
||||||
|
|
||||||
# __all__ = ['engines', 'engine', 'Session', 'metadata',
|
# __all__ = ['engines', 'engine', 'Session', 'metadata',
|
||||||
# 'get_setting', 'save_setting']
|
# 'get_setting', 'save_setting']
|
||||||
|
|
||||||
__all__ = ['engines', 'engine', 'Session', 'get_setting', 'save_setting']
|
__all__ = ['engines', 'engine', 'Session', 'get_setting', 'save_setting']
|
||||||
|
|
||||||
inited = False
|
inited = False
|
||||||
engines = None
|
engines = None
|
||||||
engine = None
|
engine = None
|
||||||
Session = sessionmaker()
|
Session = sessionmaker()
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
# metadata = None
|
# metadata = None
|
||||||
|
|
||||||
|
|
||||||
def init(config):
|
def init(config):
|
||||||
"""
|
"""
|
||||||
Initializes the database connection(s); called by :func:`edbob.init()` if
|
Initializes the database connection(s); called by :func:`edbob.init()` if
|
||||||
config includes something like::
|
config includes something like::
|
||||||
|
|
||||||
.. highlight:: ini
|
.. highlight:: ini
|
||||||
|
|
||||||
[edbob]
|
[edbob]
|
||||||
init = ['edbob.db']
|
init = ['edbob.db']
|
||||||
|
|
||||||
[edbob.db]
|
[edbob.db]
|
||||||
sqlalchemy.urls = {
|
sqlalchemy.urls = {
|
||||||
'default': 'postgresql://user:pass@localhost/edbob,
|
'default': 'postgresql://user:pass@localhost/edbob,
|
||||||
}
|
}
|
||||||
|
|
||||||
This function reads connection info from ``config`` and builds a dictionary
|
This function reads connection info from ``config`` and builds a dictionary
|
||||||
or :class:`sqlalchemy.Engine` instances accordingly. It also extends the
|
or :class:`sqlalchemy.Engine` instances accordingly. It also extends the
|
||||||
root ``edbob`` namespace with the ORM classes (:class:`edbob.Person`,
|
root ``edbob`` namespace with the ORM classes (:class:`edbob.Person`,
|
||||||
:class:`edbob.User`, etc.), as well as a few other things
|
:class:`edbob.User`, etc.), as well as a few other things
|
||||||
(e.g. :attr:`edbob.engine`, :attr:`edbob.Session`, :attr:`edbob.metadata`).
|
(e.g. :attr:`edbob.engine`, :attr:`edbob.Session`, :attr:`edbob.metadata`).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import edbob.db
|
import edbob.db
|
||||||
# from edbob.db import classes
|
# from edbob.db import classes
|
||||||
from edbob.db import model
|
from edbob.db import model
|
||||||
from edbob.db import enum
|
from edbob.db import enum
|
||||||
# from edbob.db.model import get_metadata
|
# from edbob.db.model import get_metadata
|
||||||
# from edbob.db.mappers import make_mappers
|
# from edbob.db.mappers import make_mappers
|
||||||
from edbob.db.extensions import extend_framework
|
from edbob.db.extensions import extend_framework
|
||||||
|
|
||||||
# global inited, engines, engine, metadata
|
# global inited, engines, engine, metadata
|
||||||
global inited, engines, engine
|
global inited, engines, engine
|
||||||
|
|
||||||
keys = config.get('edbob.db', 'sqlalchemy.keys')
|
keys = config.get('edbob.db', 'sqlalchemy.keys')
|
||||||
if keys:
|
if keys:
|
||||||
keys = keys.split()
|
keys = keys.split()
|
||||||
else:
|
else:
|
||||||
keys = ['default']
|
keys = ['default']
|
||||||
|
|
||||||
engines = {}
|
engines = {}
|
||||||
cfg = config.get_dict('edbob.db')
|
cfg = config.get_dict('edbob.db')
|
||||||
for key in keys:
|
for key in keys:
|
||||||
try:
|
try:
|
||||||
engines[key] = engine_from_config(cfg, 'sqlalchemy.%s.' % key)
|
engines[key] = engine_from_config(cfg, 'sqlalchemy.%s.' % key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if key == 'default':
|
if key == 'default':
|
||||||
try:
|
try:
|
||||||
engines[key] = engine_from_config(cfg)
|
engines[key] = engine_from_config(cfg)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
engine = engines.get('default')
|
engine = engines.get('default')
|
||||||
if engine:
|
if engine:
|
||||||
Base.metadata.bind = engine
|
Base.metadata.bind = engine
|
||||||
|
|
||||||
# metadata = get_metadata(bind=engine)
|
# metadata = get_metadata(bind=engine)
|
||||||
# make_mappers(metadata)
|
# make_mappers(metadata)
|
||||||
extend_framework()
|
extend_framework()
|
||||||
|
|
||||||
edbob.graft(edbob, edbob.db)
|
edbob.graft(edbob, edbob.db)
|
||||||
# edbob.graft(edbob, classes)
|
# edbob.graft(edbob, classes)
|
||||||
edbob.graft(edbob, model)
|
edbob.graft(edbob, model)
|
||||||
edbob.graft(edbob, enum)
|
edbob.graft(edbob, enum)
|
||||||
inited = True
|
inited = True
|
||||||
|
|
||||||
|
|
||||||
def get_setting(name, session=None):
|
def get_setting(name, session=None):
|
||||||
"""
|
"""
|
||||||
Returns a setting from the database.
|
Returns a setting from the database.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_session = session
|
_session = session
|
||||||
if not session:
|
if not session:
|
||||||
session = Session()
|
session = Session()
|
||||||
setting = session.query(edbob.Setting).get(name)
|
setting = session.query(edbob.Setting).get(name)
|
||||||
if setting:
|
if setting:
|
||||||
setting = setting.value
|
setting = setting.value
|
||||||
if not _session:
|
if not _session:
|
||||||
session.close()
|
session.close()
|
||||||
return setting
|
return setting
|
||||||
|
|
||||||
|
|
||||||
def save_setting(name, value, session=None):
|
def save_setting(name, value, session=None):
|
||||||
"""
|
"""
|
||||||
Saves a setting to the database.
|
Saves a setting to the database.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_session = session
|
_session = session
|
||||||
if not session:
|
if not session:
|
||||||
session = Session()
|
session = Session()
|
||||||
setting = session.query(edbob.Setting).get(name)
|
setting = session.query(edbob.Setting).get(name)
|
||||||
if not setting:
|
if not setting:
|
||||||
setting = edbob.Setting(name=name)
|
setting = edbob.Setting(name=name)
|
||||||
session.add(setting)
|
session.add(setting)
|
||||||
setting.value = value
|
setting.value = value
|
||||||
if not _session:
|
if not _session:
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|
||||||
def get_core_metadata():
|
def get_core_metadata():
|
||||||
"""
|
"""
|
||||||
Returns a :class:`sqlalchemy.MetaData` instance containing only those
|
Returns a :class:`sqlalchemy.MetaData` instance containing only those
|
||||||
:class:`sqlalchemy.Table`s which are part of the core ``edbob`` schema.
|
:class:`sqlalchemy.Table`s which are part of the core ``edbob`` schema.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from edbob.db import model
|
from edbob.db import model
|
||||||
|
|
||||||
meta = MetaData()
|
meta = MetaData()
|
||||||
for name in model.__all__:
|
for name in model.__all__:
|
||||||
if name != 'Base':
|
if name != 'Base':
|
||||||
obj = getattr(model, name)
|
obj = getattr(model, name)
|
||||||
if isinstance(obj, type) and issubclass(obj, model.Base):
|
if isinstance(obj, type) and issubclass(obj, model.Base):
|
||||||
obj.__table__.tometadata(meta)
|
obj.__table__.tometadata(meta)
|
||||||
return meta
|
return meta
|
||||||
|
|
||||||
|
|
||||||
def needs_session(func):
|
def needs_session(func):
|
||||||
"""
|
"""
|
||||||
Decorator which adds helpful session handling.
|
Decorator which adds helpful session handling.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrapped(*args, **kwargs):
|
def wrapped(*args, **kwargs):
|
||||||
session = kwargs.pop('session', None)
|
session = kwargs.pop('session', None)
|
||||||
_orig_session = session
|
_orig_session = session
|
||||||
if not session:
|
if not session:
|
||||||
session = Session()
|
session = Session()
|
||||||
res = func(session, *args, **kwargs)
|
res = func(session, *args, **kwargs)
|
||||||
if not _orig_session:
|
if not _orig_session:
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
|
@ -1,46 +1,46 @@
|
||||||
# A generic, single database configuration.
|
# A generic, single database configuration.
|
||||||
|
|
||||||
[alembic]
|
[alembic]
|
||||||
# path to migration scripts
|
# path to migration scripts
|
||||||
script_location = schema
|
script_location = schema
|
||||||
|
|
||||||
# template used to generate migration files
|
# template used to generate migration files
|
||||||
# file_template = %%(rev)s_%%(slug)s
|
# file_template = %%(rev)s_%%(slug)s
|
||||||
|
|
||||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||||
|
|
||||||
|
|
||||||
# Logging configuration
|
# Logging configuration
|
||||||
[loggers]
|
[loggers]
|
||||||
keys = root,sqlalchemy,alembic
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
[handlers]
|
[handlers]
|
||||||
keys = console
|
keys = console
|
||||||
|
|
||||||
[formatters]
|
[formatters]
|
||||||
keys = generic
|
keys = generic
|
||||||
|
|
||||||
[logger_root]
|
[logger_root]
|
||||||
level = WARN
|
level = WARN
|
||||||
handlers = console
|
handlers = console
|
||||||
qualname =
|
qualname =
|
||||||
|
|
||||||
[logger_sqlalchemy]
|
[logger_sqlalchemy]
|
||||||
level = WARN
|
level = WARN
|
||||||
handlers =
|
handlers =
|
||||||
qualname = sqlalchemy.engine
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
[logger_alembic]
|
[logger_alembic]
|
||||||
level = INFO
|
level = INFO
|
||||||
handlers =
|
handlers =
|
||||||
qualname = alembic
|
qualname = alembic
|
||||||
|
|
||||||
[handler_console]
|
[handler_console]
|
||||||
class = StreamHandler
|
class = StreamHandler
|
||||||
args = (sys.stderr,)
|
args = (sys.stderr,)
|
||||||
level = NOTSET
|
level = NOTSET
|
||||||
formatter = generic
|
formatter = generic
|
||||||
|
|
||||||
[formatter_generic]
|
[formatter_generic]
|
||||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
datefmt = %H:%M:%S
|
datefmt = %H:%M:%S
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue