[Checkins] SVN: zope.fanstatic/trunk/s implement get() and __getitem__() to mimic browserresources. this allows for computing URLs to resources outside of page templates
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Wed Jan 5 05:16:07 EST 2011
Log message for revision 119378:
implement get() and __getitem__() to mimic browserresources. this allows for computing URLs to resources outside of page templates
Changed:
U zope.fanstatic/trunk/setup.py
U zope.fanstatic/trunk/src/zope/fanstatic/README.txt
U zope.fanstatic/trunk/src/zope/fanstatic/interfaces.py
A zope.fanstatic/trunk/src/zope/fanstatic/tests/test_computeurl.py
U zope.fanstatic/trunk/src/zope/fanstatic/tests/tests.py
U zope.fanstatic/trunk/src/zope/fanstatic/tests/view.py
U zope.fanstatic/trunk/src/zope/fanstatic/zopesupport.py
-=-
Modified: zope.fanstatic/trunk/setup.py
===================================================================
--- zope.fanstatic/trunk/setup.py 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/setup.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -42,6 +42,7 @@
'zope.annotation',
'zope.app.publication',
'zope.app.wsgi >= 3.10.0',
+ 'zope.browser',
'zope.browserpage',
'zope.container',
'zope.principalregistry',
Modified: zope.fanstatic/trunk/src/zope/fanstatic/README.txt
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/README.txt 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/src/zope/fanstatic/README.txt 2011-01-05 10:16:06 UTC (rev 119378)
@@ -1,10 +1,11 @@
Zope integration for fanstatic
******************************
-This package provides Zope integration for fanstatic. This means
-it's taking care of two things:
+This package provides Zope integration for fanstatic. This means it's
+taking care of two things:
-* provide access to the needed resources throughout the request/response cycle.
+* provide access to the needed resources throughout the
+ request/response cycle.
* provide the base URL for the resources to be rendered.
Modified: zope.fanstatic/trunk/src/zope/fanstatic/interfaces.py
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/interfaces.py 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/src/zope/fanstatic/interfaces.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -1,6 +1,13 @@
from zope.interface import Interface
-
class ISetupZopeFanstatic(Interface):
pass
+class IZopeFanstaticResource(Interface):
+
+ def get(name, default):
+ pass
+
+ def __getitem__(self, name):
+ pass
+
Added: zope.fanstatic/trunk/src/zope/fanstatic/tests/test_computeurl.py
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/tests/test_computeurl.py (rev 0)
+++ zope.fanstatic/trunk/src/zope/fanstatic/tests/test_computeurl.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -0,0 +1,57 @@
+import unittest
+from zope.interface import Interface
+from zope.component import getMultiAdapter
+from zope.publisher.browser import TestRequest
+from zope.traversing.interfaces import ITraversable
+
+import fanstatic
+from zope.fanstatic.zopesupport import ZopeFanstaticResource
+from zope.fanstatic.tests import tests
+
+class ComputeURL(unittest.TestCase):
+
+ layer = tests.layer
+
+ def setUp(self):
+ fanstatic.init_needed()
+ self.context = object()
+ self.request = TestRequest()
+ self.resource_namespace = getMultiAdapter(
+ (self.context, self.request), ITraversable, name='resource')
+
+ def test_lookup_resource(self):
+ # There's a resource library registered for the name 'foo'.
+ resource = self.resource_namespace.traverse('foo', [])
+ self.assert_(isinstance(resource, ZopeFanstaticResource))
+
+ def test_library_url(self):
+ resource = self.resource_namespace.traverse('foo', [])
+ self.assertEquals('http://127.0.0.1/fanstatic/foo', str(resource))
+
+ def test_get(self):
+ resource = self.resource_namespace.traverse('foo', [])
+ a_js = resource.get('a.js')
+ self.assert_(isinstance(a_js, ZopeFanstaticResource))
+ self.assertEquals('http://127.0.0.1/fanstatic/foo/a.js', str(a_js))
+
+ woekie = resource.get('bar').get('baz').get('woekie.png')
+ self.assert_(isinstance(woekie, ZopeFanstaticResource))
+ self.assertEquals(
+ 'http://127.0.0.1/fanstatic/foo/bar/baz/woekie.png', str(woekie))
+
+ def test_getitem(self):
+ resource = self.resource_namespace.traverse('foo', [])
+ a_js = resource['a.js']
+ self.assert_(isinstance(a_js, ZopeFanstaticResource))
+ self.assertEquals('http://127.0.0.1/fanstatic/foo/a.js', str(a_js))
+
+ woekie = resource['bar']['baz']['woekie.png']
+ self.assert_(isinstance(woekie, ZopeFanstaticResource))
+ self.assertEquals(
+ 'http://127.0.0.1/fanstatic/foo/bar/baz/woekie.png', str(woekie))
+
+ def test_call(self):
+ resource = self.resource_namespace.traverse('foo', [])
+ a_js = resource.get('a.js')
+ self.assertEquals(str(a_js), a_js())
+
Modified: zope.fanstatic/trunk/src/zope/fanstatic/tests/tests.py
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/tests/tests.py 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/src/zope/fanstatic/tests/tests.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -24,9 +24,11 @@
def setup_middleware(self, app):
return fanstatic.Injector(app)
+layer = TestLayer(zope.fanstatic.tests)
+
def test_suite():
readme = doctest.DocFileSuite(
'../README.txt',
optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS)
- readme.layer = TestLayer(zope.fanstatic.tests)
+ readme.layer = layer
return unittest.TestSuite([readme])
Modified: zope.fanstatic/trunk/src/zope/fanstatic/tests/view.py
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/tests/view.py 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/src/zope/fanstatic/tests/view.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -29,3 +29,4 @@
class TestInlineResource(object):
pass
+
Modified: zope.fanstatic/trunk/src/zope/fanstatic/zopesupport.py
===================================================================
--- zope.fanstatic/trunk/src/zope/fanstatic/zopesupport.py 2011-01-05 09:18:39 UTC (rev 119377)
+++ zope.fanstatic/trunk/src/zope/fanstatic/zopesupport.py 2011-01-05 10:16:06 UTC (rev 119378)
@@ -7,33 +7,52 @@
import fanstatic
+from zope.fanstatic.interfaces import IZopeFanstaticResource
+
@adapter(IEndRequestEvent)
def set_base_url_on_needed_inclusions(event):
needed = fanstatic.get_needed()
if needed.base_url is None:
needed.base_url = absoluteURL(None, event.request)
+_sentinel = object()
+
class ZopeFanstaticResource(object):
- # Hack to get ++resource++foo/bar/baz.jpg paths working in Zope
- # Pagetemplates. Note that ++resource+foo/bar/baz.jpg URLs will
+ # Hack to get ++resource++foo/bar/baz.jpg *paths* working in Zope
+ # Pagetemplates. Note that ++resource+foo/bar/baz.jpg *URLs* will
# not work with this hack!
+ #
+ # The ZopeFanstaticResource class also implements an __getitem__()
+ # / get() interface, to support rendering URLs to resources from
+ # code.
- implements(ITraversable, IAbsoluteURL)
+ implements(IZopeFanstaticResource, ITraversable, IAbsoluteURL)
def __init__(self, request, library, name=''):
self.request = request
self.library = library
self.name = name
- def traverse(self, name, furtherPath):
+ def get(self, name, default=_sentinel):
+ # XXX return default if given, or NotFound (or something) when
+ # not, in case name is not resolved to an actual resource.
name = '%s/%s' % (self.name, name)
- # XXX check whether the request resource actually exists and
- # warn if not.
return ZopeFanstaticResource(self.request, self.library, name=name)
+ def traverse(self, name, furtherPath):
+ return self.get(name)
+
+ def __getitem__(self, name):
+ resource = self.get(name, None)
+ if resource is None:
+ raise KeyErro(name)
+ return resource
+
def __str__(self):
needed = fanstatic.get_needed()
if needed.base_url is None:
needed.base_url = absoluteURL(None, self.request)
return needed.library_url(self.library) + self.name
+
+ __call__ = __str__
More information about the checkins
mailing list