diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-05-27 14:03:07 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-05-27 14:03:07 +0000 |
| commit | 711b60c05b9573f688994233ec1baac3f89bc45a (patch) | |
| tree | c58595bc181ab52189b0cc096de099cf8e3bf167 | |
| parent | a6ead8805b4de79edd0d980942894c0518104d5e (diff) | |
| download | pyramid-711b60c05b9573f688994233ec1baac3f89bc45a.tar.gz pyramid-711b60c05b9573f688994233ec1baac3f89bc45a.tar.bz2 pyramid-711b60c05b9573f688994233ec1baac3f89bc45a.zip | |
Provide b/c for scripts which used ``registry_manager``
| -rw-r--r-- | CHANGES.txt | 26 | ||||
| -rw-r--r-- | repoze/bfg/registry.py | 29 | ||||
| -rw-r--r-- | repoze/bfg/scripting.py | 18 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_scripting.py | 31 |
4 files changed, 104 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 34b59d144..cb2b7d7ae 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,29 @@ +0.9dev (unreleased) +=================== + +Features +-------- + +- A new API function has been added for scripts which need to obtain + the traversal root: ``repoze.bfg.scripting.get_root``. Given a + ``repoze.bfg`` Router application instance as its ``router`` + argument, this callable returns the traversal root of graph as + defined by the application's root factory. It also has the effect + of pushing a new registry and request on to the internal thread + local stack managed by BFG so that registry lookups work properly. + + .. warning:: This function should never be called from *within* a + BFG model or view, only from top-level scripts which wish to + get the root of a graph to do offline processing. + +Deprecations +------------ + +- The name ``repoze.bfg.registry.registry_manager`` was never an API, + but scripts in the wild were using it to set up an environment for + use under a debug shell. A backwards compatibility shim has been + added for this purpose, but the feature is deprecated. + 0.9a1 (2009-5-27) ================= diff --git a/repoze/bfg/registry.py b/repoze/bfg/registry.py index cacf9806c..b9397e7da 100644 --- a/repoze/bfg/registry.py +++ b/repoze/bfg/registry.py @@ -89,3 +89,32 @@ def getSiteManager(context=None): except TypeError, error: raise ComponentLookupError(*error.args) +class FakeRegistryManager(object): + def push(self, registry): + return manager.push({'registry':registry, 'request':None}) + + set = push # b/c + + def pop(self): + result = manager.pop() + if result: + return result['registry'] + + def get(self): + return manager.get()['registry'] + + def clear(self): + manager.clear() + +# for use in scripts for backwards compatibility *only*! +registry_manager = FakeRegistryManager() + +deprecated('registry_manager', + 'As of repoze.bfg 0.9, any import of registry_manager from' + '``repoze.bfg.registry`` is ' + 'deprecated. Instead, if you are trying to push a BFG ' + 'application registry into a registry_manager within a "debug" ' + 'script, call ``app.get_root(environ)``, which has the side ' + 'effect of pushing the current registry into a thread local ' + 'stack. ``registry_manager`` will disappear in a later ' + 'release of repoze.bfg') diff --git a/repoze/bfg/scripting.py b/repoze/bfg/scripting.py new file mode 100644 index 000000000..9e83e2fe8 --- /dev/null +++ b/repoze/bfg/scripting.py @@ -0,0 +1,18 @@ +_GET_ROOT_ENVIRON = {} + +def get_root(router): + """ Given a :mod:`repoze.bfg` Router application instance as its + ``router`` argument, this callable returns the traversal root of + graph as defined by the application's root factory. It also has + the effect of pushing a new registry and request on to the + internal thread local stack managed by BFG so that registry + lookups work properly. + + .. warning:: This function should never be called from *within* a + BFG model or view, only from top-level scripts which wish to + get the root of a graph to do offline processing.""" + registry = router.registry + threadlocals = {'registry':registry, 'request':None} + router.threadlocal_manager.push(threadlocals) + return router.root_factory(_GET_ROOT_ENVIRON) + diff --git a/repoze/bfg/tests/test_scripting.py b/repoze/bfg/tests/test_scripting.py new file mode 100644 index 000000000..a54b4b7d9 --- /dev/null +++ b/repoze/bfg/tests/test_scripting.py @@ -0,0 +1,31 @@ +import unittest + +class TestGetRoot(unittest.TestCase): + def _callFUT(self, router): + from repoze.bfg.scripting import get_root + return get_root(router) + + def test_it(self): + router = DummyRouter() + result = self._callFUT(router) + self.assertEqual(result, router) + self.assertEqual(len(router.threadlocal_manager.pushed), 1) + self.assertEqual(router.threadlocal_manager.pushed[0], + {'registry':None, 'request':None}) + + +class DummyThreadLocalManager: + def __init__(self): + self.pushed = [] + + def push(self, val): + self.pushed.append(val) + +class DummyRouter: + def __init__(self): + self.registry = None + self.threadlocal_manager = DummyThreadLocalManager() + + def root_factory(self, environ): + return self + |
