diff options
Diffstat (limited to 'repoze')
| -rw-r--r-- | repoze/__init__.py | 1 | ||||
| -rw-r--r-- | repoze/bfg/__init__.py | 1 | ||||
| -rw-r--r-- | repoze/bfg/interfaces.py | 10 | ||||
| -rw-r--r-- | repoze/bfg/router.py | 14 | ||||
| -rw-r--r-- | repoze/bfg/tests/__init__.py | 1 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zodb.py | 29 | ||||
| -rw-r--r-- | repoze/bfg/zodb.py | 37 |
7 files changed, 93 insertions, 0 deletions
diff --git a/repoze/__init__.py b/repoze/__init__.py new file mode 100644 index 000000000..de40ea7ca --- /dev/null +++ b/repoze/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/repoze/bfg/__init__.py b/repoze/bfg/__init__.py new file mode 100644 index 000000000..de40ea7ca --- /dev/null +++ b/repoze/bfg/__init__.py @@ -0,0 +1 @@ +__import__('pkg_resources').declare_namespace(__name__) diff --git a/repoze/bfg/interfaces.py b/repoze/bfg/interfaces.py new file mode 100644 index 000000000..5e1a605ec --- /dev/null +++ b/repoze/bfg/interfaces.py @@ -0,0 +1,10 @@ +from zope.interface import Interface + +class IWSGIApplication(Interface): + def __call__(environ, start_response): + """ Represent a WSGI (PEP 333) application """ + +class IPolicy(Interface): + def __call__(environ): + """ Return a tuple in the form (context, name, subpath) """ + diff --git a/repoze/bfg/router.py b/repoze/bfg/router.py new file mode 100644 index 000000000..049430723 --- /dev/null +++ b/repoze/bfg/router.py @@ -0,0 +1,14 @@ +from zope.component import getAdapter +from repoze.bfg.interfaces import IWSGIApplication + +class Router: + def __init__(self, app, policy): + self.app = app + self.policy = policy + + def __call__(self, environ, start_response): + context, name, subpath = self.policy(environ) + app = getAdapter(context, IWSGIApplication, name) + environ['repoze.bfg.context'] = context + environ['repoze.bfg.subpath'] = subpath + return app(environ, start_response) diff --git a/repoze/bfg/tests/__init__.py b/repoze/bfg/tests/__init__.py new file mode 100644 index 000000000..5bb534f79 --- /dev/null +++ b/repoze/bfg/tests/__init__.py @@ -0,0 +1 @@ +# package diff --git a/repoze/bfg/tests/test_zodb.py b/repoze/bfg/tests/test_zodb.py new file mode 100644 index 000000000..3e08968b4 --- /dev/null +++ b/repoze/bfg/tests/test_zodb.py @@ -0,0 +1,29 @@ +import unittest + +class ZODBGetitemPolicyTests(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.zodb import ZODBGetitemPolicy + return ZODBGetitemPolicy + + def _makeOne(self, *arg, **kw): + klass = self._getTargetClass() + return klass(*arg, **kw) + + def test_class_conforms_to_IPolicy(self): + from zope.interface.verify import verifyClass + from repoze.bfg.interfaces import IPolicy + verifyClass(IPolicy, self._getTargetClass()) + + def test_instance_conforms_to_IPolicy(self): + from zope.interface.verify import verifyObject + from repoze.bfg.interfaces import IPolicy + verifyObject(IPolicy, self._makeOne('dbname')) + + def test_call_noconn(self): + mw = self._makeOne('dbname') + environ = {} + self.assertRaises(ValueError, mw, environ) + + + + diff --git a/repoze/bfg/zodb.py b/repoze/bfg/zodb.py new file mode 100644 index 000000000..976c687fb --- /dev/null +++ b/repoze/bfg/zodb.py @@ -0,0 +1,37 @@ +from zope.interface import implements + +from repoze.zodbconn.middleware import get_conn + +from repoze.bfg.interfaces import IPolicy + +class ZODBGetitemPolicy: + + implements(IPolicy) + + def __init__(self, dbname, prefix=()): + self.dbname = dbname + self.prefix = prefix + self.get_conn = get_conn + + def __call__(self, environ): + conn = self.get_conn(environ, self.dbname) + if conn is None: + raise ValueError('No such connection %s' % self.dbname) + + path = environ['PATH_INFO'].split('/') + path = list(self.prefix) + path + + ob = conn.open() + + name = '' + while path: + element = path.pop(0) + try: + ob = ob[element] + except KeyError: + if path: + name = path.pop(0) + break + + return ob, name, path + |
