diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-08-28 17:15:00 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-08-28 17:15:00 +0000 |
| commit | 50df953770409dc9c9558c77bd5c0bbb17ac54f6 (patch) | |
| tree | 327045a06d056ae3c734fe4368bef30c1b449f83 | |
| parent | 399d18a10e14a9748fbdf23763dbddd18bdda812 (diff) | |
| download | pyramid-50df953770409dc9c9558c77bd5c0bbb17ac54f6.tar.gz pyramid-50df953770409dc9c9558c77bd5c0bbb17ac54f6.tar.bz2 pyramid-50df953770409dc9c9558c77bd5c0bbb17ac54f6.zip | |
Coverage.
| -rw-r--r-- | repoze/bfg/static.py | 21 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zcml.py | 107 | ||||
| -rw-r--r-- | repoze/bfg/zcml.py | 33 |
3 files changed, 81 insertions, 80 deletions
diff --git a/repoze/bfg/static.py b/repoze/bfg/static.py index be33ebee1..01ce98a30 100644 --- a/repoze/bfg/static.py +++ b/repoze/bfg/static.py @@ -1,5 +1,4 @@ import os -import sys import pkg_resources from paste.httpheaders import ETAG @@ -7,26 +6,6 @@ from paste.urlparser import StaticURLParser from paste import httpexceptions from paste import request -def find_package(path): - """ - Determine the Python-package where path is located. If the path - is not located within the Python sys-path, return ``None``. - """ - - syspaths = list(sys.path) - syspaths.sort(key=lambda syspath: path.startswith('path') and 'path', reverse=True) - syspath = syspaths[0] - - path = os.path.normpath(path) - if not path.startswith(syspath): - return - - path = path[len(syspath):] - if path.startswith(os.path.sep): - path = path[1:] - - return path - class PackageURLParser(StaticURLParser): """ This probably won't work with zipimported resources """ def __init__(self, package_name, resource_name, root_resource=None, diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index b19f20148..49baa3a34 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -1040,9 +1040,6 @@ class TestRouteDirective(unittest.TestCase): class TestStaticDirective(unittest.TestCase): def setUp(self): cleanUp() - import os - here = os.path.dirname(__file__) - self.static_path = os.path.join(here, 'fixtures', 'static') def tearDown(self): cleanUp() @@ -1052,31 +1049,33 @@ class TestStaticDirective(unittest.TestCase): return static(*arg, **kw) def test_absolute(self): + import os + here = os.path.dirname(__file__) + static_path = os.path.join(here, 'fixtures', 'static') from repoze.bfg.zcml import handler from repoze.bfg.zcml import connect_route from repoze.bfg.interfaces import IView context = DummyContext() - self._callFUT(context, 'name', self.static_path) + self._callFUT(context, 'name', static_path) actions = context.actions self.assertEqual(len(actions), 2) - route_action = actions[0] - route_callable = route_action['callable'] - route_discriminator = route_action['discriminator'] - route_args = route_action['args'] - self.assertEqual(route_callable, handler) - self.assertEqual(route_discriminator[:3], ( - 'view', None, '')) - self.assertEqual(route_discriminator[4], IView) - - route_action = actions[1] - route_callable = route_action['callable'] - route_discriminator = route_action['discriminator'] - route_args = route_action['args'] - self.assertEqual(route_callable, connect_route) - self.assertEqual(route_discriminator, ( - 'route', 'name', None, None)) - self.assertEqual(route_args[0], 'name*subpath') + action = actions[0] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, handler) + self.assertEqual(discriminator[:3], ('view', None, '')) + self.assertEqual(discriminator[4], IView) + self.assertEqual(args[1].app.directory, static_path) + + action = actions[1] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, connect_route) + self.assertEqual(discriminator, ('route', 'name', None, None)) + self.assertEqual(args[0], 'name*subpath') def test_package_relative(self): from repoze.bfg.zcml import handler @@ -1087,23 +1086,51 @@ class TestStaticDirective(unittest.TestCase): actions = context.actions self.assertEqual(len(actions), 2) - route_action = actions[0] - route_callable = route_action['callable'] - route_discriminator = route_action['discriminator'] - route_args = route_action['args'] - self.assertEqual(route_callable, handler) - self.assertEqual(route_discriminator[:3], ( - 'view', None, '')) - self.assertEqual(route_discriminator[4], IView) + action = actions[0] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, handler) + self.assertEqual(discriminator[:3], ('view', None, '')) + self.assertEqual(discriminator[4], IView) + self.assertEqual(args[1].app.package_name, 'repoze.bfg.tests') + self.assertEqual(args[1].app.resource_name, 'fixtures/static') + + action = actions[1] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, connect_route) + self.assertEqual(discriminator, ('route', 'name', None, None)) + self.assertEqual(args[0], 'name*subpath') + + def test_here_relative(self): + from repoze.bfg.zcml import handler + from repoze.bfg.zcml import connect_route + from repoze.bfg.interfaces import IView + import repoze.bfg.tests + context = DummyContext(repoze.bfg.tests) + self._callFUT(context, 'name', 'fixtures/static') + actions = context.actions + self.assertEqual(len(actions), 2) - route_action = actions[1] - route_callable = route_action['callable'] - route_discriminator = route_action['discriminator'] - route_args = route_action['args'] - self.assertEqual(route_callable, connect_route) - self.assertEqual(route_discriminator, ( - 'route', 'name', None, None)) - self.assertEqual(route_args[0], 'name*subpath') + action = actions[0] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, handler) + self.assertEqual(discriminator[:3], ('view', None, '')) + self.assertEqual(discriminator[4], IView) + self.assertEqual(args[1].app.package_name, 'repoze.bfg.tests') + self.assertEqual(args[1].app.resource_name, 'fixtures/static') + + action = actions[1] + callable = action['callable'] + discriminator = action['discriminator'] + args = action['args'] + self.assertEqual(callable, connect_route) + self.assertEqual(discriminator, ('route', 'name', None, None)) + self.assertEqual(args[0], 'name*subpath') class TestResourceDirective(unittest.TestCase): def setUp(self): @@ -1559,6 +1586,7 @@ class TestRequestOnly(unittest.TestCase): class DummyModule: __path__ = "foo" __name__ = "dummy" + __file__ = '' class DummyModuleGrokker: def __init__(self): @@ -1579,9 +1607,10 @@ class DummyMartianModule: return self.module_grokker class DummyContext: - def __init__(self): + def __init__(self, resolved=DummyModule): self.actions = [] self.info = None + self.resolved = resolved def action(self, discriminator, callable, args): self.actions.append( @@ -1591,7 +1620,7 @@ class DummyContext: ) def resolve(self, dottedname): - return DummyModule + return self.resolved class Dummy: pass diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index dbc145149..5a41babd1 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -1,5 +1,4 @@ import os -import sys import inspect import types @@ -39,6 +38,8 @@ from repoze.bfg.interfaces import ILogger from repoze.bfg.interfaces import IRequestFactories from repoze.bfg.interfaces import IPackageOverrides +from repoze.bfg.path import package_name + from repoze.bfg.resource import PackageOverrides from repoze.bfg.request import DEFAULT_REQUEST_FACTORIES @@ -48,8 +49,6 @@ from repoze.bfg.security import ViewPermissionFactory from repoze.bfg.secpols import registerBBBAuthn -from repoze.bfg.static import find_package - from repoze.bfg.view import static as static_view import martian @@ -189,13 +188,13 @@ def _override(package, path, override_package, override_prefix, PackageOverrides=PackageOverrides): # PackageOverrides kw arg for tests sm = getSiteManager() - package_name = package.__name__ - override_package_name = override_package.__name__ - override = queryUtility(IPackageOverrides, name=package_name) + pkg_name = package.__name__ + override_pkg_name = override_package.__name__ + override = queryUtility(IPackageOverrides, name=pkg_name) if override is None: override = PackageOverrides(package) - sm.registerUtility(override, IPackageOverrides, name=package_name) - override.insert(path, override_package_name, override_prefix) + sm.registerUtility(override, IPackageOverrides, name=pkg_name) + override.insert(path, override_pkg_name, override_prefix) def resource(context, to_override, override_with): if to_override == override_with: @@ -365,19 +364,13 @@ class IStaticDirective(Interface): def static(_context, name, path, cache_max_age=3600): """ Handle ``static`` ZCML directives """ + if (not ':' in path) and (not os.path.isabs(path)): + # if it's not a package:relative/name and it's not an + # /absolute/path it's a relative/path; this means its relative + # to the package in which the ZCML file is defined. + path = '%s:%s' % (package_name(_context.resolve('.')), path) - if ':' in path: - package_name, path = path.split(':') - else: - package_path = _context.resolve('.').__path__[0] - package_name = find_package(package_path) - if package_name is not None: - path = os.path.join(package_path, path) - path = path[len(sys.modules[package_name].__path__[0])+1:] - - view = static_view( - path, cache_max_age=cache_max_age, package_name=package_name) - + view = static_view(path, cache_max_age=cache_max_age) route(_context, name, "%s*subpath" % name, view=view) class IViewDirective(Interface): |
