Skip to content

Commit

Permalink
support pyramid configuration refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
aodag committed May 26, 2012
1 parent 43218c2 commit 3748cca
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

tests_require = [
"transaction",
"pyramid",
"nose",
"coverage",
]
Expand Down
55 changes: 55 additions & 0 deletions sqlahelper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import sqlalchemy as sa
import sqlalchemy.ext.declarative as declarative
import sqlalchemy.orm as orm
Expand All @@ -6,6 +7,60 @@
# Import only version 1 API with "import *"
__all__ = ["add_engine", "get_base", "get_session", "get_engine"]

# pyramid configuration

truthy = frozenset(['true', 'yes', 'on', 'y', 't', '1'])
falsy = frozenset(['false', 'no', 'off', 'n', 'f', '0'])

def asbool(obj):
if isinstance(obj, basestring):
obj = obj.strip().lower()
if obj in truthy:
return True
elif obj in falsy:
return False
else:
raise ValueError("String is not true/false: %r" % obj)
return bool(obj)

engine_url_pattern = re.compile(r'sqlahelper\.(?P<engine_name>\w+)\.url')
engine_echo_pattern = re.compile(r'sqlahelper\.(?P<engine_name>\w+)\.echo')

def includeme(config):
""" set up engines from config.
usege in :term:`Pyramid`::
config.include('sqlahelper')
config.ini::
sqlalchemy.url = sqlite:///%(here)s/myapp.db
sqlahelper.otherengine.url = sqlite:///%(here)s/myapp_other.db
``sqlalchemy.url`` is set to default engine.
``sqlahelper.otherengine.url`` is set to engine named "otherengine".
"""

settings = config.registry.settings
if 'sqlalchemy.url' in settings:
engine = sa.engine_from_config(settings)
set_default_engine(engine)

for k, v in settings.items():
url_match = engine_url_pattern.match(k)
if url_match:
engine_name = url_match.groupdict()['engine_name']
engine = sa.create_engine(v)
setattr(engines, engine_name, engine)
echo_match = engine_echo_pattern.match(k)
if echo_match:
engine_name = echo_match.groupdict()['engine_name']
if not hasattr(engines, engine_name):
continue
setattr(getattr(engines, engine_name), "echo", asbool(v))

# VERSION 2 API

class AttributeContainer(object):
Expand Down
42 changes: 42 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ def test1(self):
self.assertNotEqual(base2, base)
self.assertEqual(base2, my_base)

class IncludeMeTests(unittest.TestCase):
def setUp(self):
from pyramid import testing
self.config = testing.setUp()

def tearDown(self):
from pyramid import testing
testing.tearDown()

def _callFUT(self, *args, **kwargs):
from sqlahelper import includeme
return includeme(*args, **kwargs)

def test_default_engine(self):
engine_settings = {
'sqlalchemy.url': 'sqlite:///',
'sqlalchemy.echo': 'true',
}
self.config.registry.settings.update(engine_settings)

self._callFUT(self.config)

import sqlahelper

self.assertIsNotNone(sqlahelper.engines.default)
self.assertEqual(str(sqlahelper.engines.default.url), 'sqlite:///')
self.assertTrue(sqlahelper.engines.default.echo)

def test_another_engines(self):
engine_settings = {
'sqlahelper.other.url': 'sqlite:///',
'sqlahelper.other.echo': 'true',
}
self.config.registry.settings.update(engine_settings)

self._callFUT(self.config)

import sqlahelper

self.assertIsNotNone(sqlahelper.engines.other)
self.assertEqual(str(sqlahelper.engines.other.url), 'sqlite:///')
self.assertTrue(sqlahelper.engines.other.echo)

if __name__ == "__main__":
unittest.main()

0 comments on commit 3748cca

Please sign in to comment.