From bb672fe3c2e2a90fa149ba575e36d45c7a4271f7 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 17 Jan 2009 18:59:53 +0000 Subject: - Allow the ``repoze.bfg.view.static`` helper to be passed a relative ``root_path`` name; it will be considered relative to the file in which it was called. --- repoze/bfg/tests/test_view.py | 30 ++++++++++++++++++++---------- repoze/bfg/view.py | 22 ++++++++++++++-------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/repoze/bfg/tests/test_view.py b/repoze/bfg/tests/test_view.py index 53c0248ed..ca1037a67 100644 --- a/repoze/bfg/tests/test_view.py +++ b/repoze/bfg/tests/test_view.py @@ -432,24 +432,34 @@ class TestStaticView(unittest.TestCase, BaseTest): from repoze.bfg.view import static return static - def _getHere(self): + def _makeOne(self, path): + return self._getTargetClass()(path) + + def test_abspath(self): import os - return os.path.dirname(__file__) + path = os.path.dirname(__file__) + view = self._makeOne(path) + context = DummyContext() + request = DummyRequest() + request.subpath = ['__init__.py'] + request.environ = self._makeEnviron() + response = view(context, request) + self.assertEqual(request.copied, True) + self.assertEqual(response.directory, path) - def _makeOne(self): - here = self._getHere() - return self._getTargetClass()(here) - - def test_it(self): - view = self._makeOne() + def test_relpath(self): + import os + path = 'fixtures' + view = self._makeOne(path) context = DummyContext() request = DummyRequest() request.subpath = ['__init__.py'] request.environ = self._makeEnviron() response = view(context, request) self.assertEqual(request.copied, True) - here = self._getHere() - self.assertEqual(response.directory, here) + here = os.path.abspath(os.path.dirname(__file__)) + abspath = os.path.join(here, 'fixtures') + self.assertEqual(response.directory, abspath) class TestBFGViewDecorator(unittest.TestCase): def setUp(self): diff --git a/repoze/bfg/view.py b/repoze/bfg/view.py index 3b27a8b94..67c664855 100644 --- a/repoze/bfg/view.py +++ b/repoze/bfg/view.py @@ -6,6 +6,7 @@ from repoze.bfg.interfaces import ISecurityPolicy from repoze.bfg.interfaces import IViewPermission from repoze.bfg.interfaces import IView +from repoze.bfg.path import caller_path from repoze.bfg.security import Unauthorized from repoze.bfg.security import Allowed @@ -132,15 +133,20 @@ class static(object): directory may contain subdirectories (recursively); the static view implementation will descend into these directories as necessary based on the components of the URL in order to resolve a - path into a response - - Pass the absolute filesystem path to the directory containing - static files directory to the constructor as the ``root_dir`` - argument. ``cache_max_age`` influences the Expires and Max-Age - response headers returned by the view (default is 3600 seconds or - five minutes). + path into a response. + + You may pass an absolute or relative filesystem path to the + directory containing static files directory to the constructor as + the ``root_dir`` argument. If the path is relative, it will be + considered relative to the directory in which the Python file + which calls ``static`` resides. ``cache_max_age`` influences the + Expires and Max-Age response headers returned by the view (default + is 3600 seconds or five minutes). ``level`` influences how + relative directories are resolved (the number of hops in the call + stack), not used very often. """ - def __init__(self, root_dir, cache_max_age=3600): + def __init__(self, root_dir, cache_max_age=3600, level=2): + root_dir = caller_path(root_dir, level=level) self.app = StaticURLParser(root_dir, cache_max_age=cache_max_age) def __call__(self, context, request): -- cgit v1.2.3