diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-11-28 01:18:25 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-11-28 01:18:25 +0000 |
| commit | 0970432b72d8f4360b69dc58223ea0725d747d36 (patch) | |
| tree | 7fe9a6a694055785d2c54ca27e3f0bd2fb01d3b6 | |
| parent | c91abc312f61c2d012ff7f33b5bce07ecab3007b (diff) | |
| download | pyramid-0970432b72d8f4360b69dc58223ea0725d747d36.tar.gz pyramid-0970432b72d8f4360b69dc58223ea0725d747d36.tar.bz2 pyramid-0970432b72d8f4360b69dc58223ea0725d747d36.zip | |
- Turn paths into resource specs in ZCML directives that use
context.path.
| -rw-r--r-- | TODO.txt | 2 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zcml.py | 47 | ||||
| -rw-r--r-- | repoze/bfg/zcml.py | 27 |
3 files changed, 70 insertions, 6 deletions
@@ -9,5 +9,3 @@ - Decide if we *really* make the threadlocal stuff into APIs. -- Turn paths into resource specs in ZCML directives that use - context.path. diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index 1954adf64..1f6703d5d 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -896,6 +896,49 @@ class TestRolledUpFactory(unittest.TestCase): result = factory(True) self.assertEqual(result, True) +class Test_path_spec(unittest.TestCase): + def _callFUT(self, context, path): + from repoze.bfg.zcml import path_spec + return path_spec(context, path) + + def test_no_package_attr(self): + context = DummyContext() + path = '/thepath' + result = self._callFUT(context, path) + self.assertEqual(result, path) + + def test_package_attr_None(self): + context = DummyContext() + context.package = None + path = '/thepath' + result = self._callFUT(context, path) + self.assertEqual(result, path) + + def test_package_path_doesnt_start_with_abspath(self): + context = DummyContext() + context.package = DummyPackage('repoze.bfg.tests') + path = '/thepath' + result = self._callFUT(context, path) + self.assertEqual(result, path) + + def test_package_path_starts_with_abspath(self): + import pkg_resources + import os + context = DummyContext() + package = DummyPackage('repoze.bfg.tests') + package_path = pkg_resources.resource_filename('repoze.bfg.tests', '') + template_path = os.path.join(package_path, 'templates/foo.pt') + context.package = package + result = self._callFUT(context, template_path) + self.assertEqual(result, 'repoze.bfg.tests:templates/foo.pt') + + def test_package_name_is___main__(self): + context = DummyContext() + package = DummyPackage('__main__') + context.package = package + result = self._callFUT(context, '/foo.pt') + self.assertEqual(result, '/foo.pt') + class IDummy(Interface): pass @@ -952,3 +995,7 @@ class DummyRequest: def copy(self): return self +class DummyPackage(object): + def __init__(self, name): + self.__name__ = name + diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index c81c22d65..e2ea50ab4 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -27,6 +27,7 @@ from repoze.bfg.authentication import RemoteUserAuthenticationPolicy from repoze.bfg.authentication import RepozeWho1AuthenticationPolicy from repoze.bfg.authorization import ACLAuthorizationPolicy from repoze.bfg.configuration import Configurator +from repoze.bfg.path import package_path from repoze.bfg.request import route_request_iface from repoze.bfg.static import StaticRootFactory from repoze.bfg.threadlocal import get_current_registry @@ -163,7 +164,7 @@ def view( request_type = _context.resolve(request_type) if renderer and '.' in renderer: - renderer = _context.path(renderer) + renderer = path_spec(_context, renderer) def register(): config = Configurator(reg) @@ -242,7 +243,7 @@ def route(_context, name, path, view=None, view_for=None, view_permission = view_permission or permission view_renderer = view_renderer or renderer if view_renderer and '.' in view_renderer: - view_renderer = _context.path(view_renderer) + view_renderer = path_spec(_context, view_renderer) def register(): config = Configurator(reg) @@ -319,7 +320,7 @@ class SystemViewHandler(object): def __call__(self, _context, view=None, attr=None, renderer=None, wrapper=None): if renderer and '.' in renderer: - renderer = _context.path(renderer) + renderer = path_spec(_context, renderer) def register(iface=self.iface): reg = get_current_registry() @@ -480,7 +481,7 @@ class IStaticDirective(Interface): def static(_context, name, path, cache_max_age=3600): """ Handle ``static`` ZCML directives """ - path = _context.path(path) + path = path_spec(_context, path) reg = get_current_registry() config = Configurator(reg) @@ -752,3 +753,21 @@ def _rolledUpFactory(factories): # Store the original factory for documentation factory.factory = factories[0] return factory + +def path_spec(context, path): + # Convert an absolute path to a resource in a package to a + # resource specification if possible; otherwise return the + # absolute path; we prefer registering resource specifications + # over absolute paths because these can be overridden by the + # resource directive. + abspath = context.path(path) + if hasattr(context, 'package') and context.package: + package = context.package + if getattr(package, '__name__', None) == '__main__': + return abspath + pp = package_path(package) +'/' + if abspath.startswith(pp): + relpath = abspath[len(pp):] + return '%s:%s' % (package.__name__, relpath) + return abspath + |
