[Checkins] SVN: hurry.zoperesource/ Initial import: zope integration for hurry.resource.

Martijn Faassen faassen at infrae.com
Tue Oct 7 08:13:07 EDT 2008


Log message for revision 91843:
  Initial import: zope integration for hurry.resource.
  

Changed:
  A   hurry.zoperesource/
  A   hurry.zoperesource/trunk/
  A   hurry.zoperesource/trunk/buildout.cfg
  A   hurry.zoperesource/trunk/setup.py
  A   hurry.zoperesource/trunk/src/
  A   hurry.zoperesource/trunk/src/hurry/
  A   hurry.zoperesource/trunk/src/hurry/__init__.py
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/README.txt
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/__init__.py
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/configure.zcml
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/__init__.py
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/ftesting.zcml
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_multiple.pt
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_single.pt
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/tests.py
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/tests/view.py
  A   hurry.zoperesource/trunk/src/hurry/zoperesource/zopesupport.py

-=-
Added: hurry.zoperesource/trunk/buildout.cfg
===================================================================
--- hurry.zoperesource/trunk/buildout.cfg	                        (rev 0)
+++ hurry.zoperesource/trunk/buildout.cfg	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,11 @@
+[buildout]
+develop = . hurry.resource
+parts = test
+versions = versions
+
+[versions]
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = hurry.zoperesource
+defaults = ['--tests-pattern', '^f?tests$', '-v']

Added: hurry.zoperesource/trunk/setup.py
===================================================================
--- hurry.zoperesource/trunk/setup.py	                        (rev 0)
+++ hurry.zoperesource/trunk/setup.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,30 @@
+from setuptools import setup, find_packages
+import sys, os
+
+setup(
+    name='hurry.zoperesource',
+    version='0.1dev',
+    description="Flexible resources for Zope.",
+    classifiers=[],
+    keywords='',
+    author='Martijn Faassen',
+    author_email='faassen at startifact.com',
+    license='',
+    packages=find_packages('src'),
+    package_dir={'': 'src'},
+    include_package_data=True,
+    zip_safe=False,
+    install_requires=[
+        'setuptools',
+        'z3c.autoinclude',
+        'grokcore.component',
+        'zope.security',
+        'zope.publisher',
+        'zope.app.component',
+        'zope.traversing',
+        'zope.securitypolicy',
+        'zope.testbrowser',
+        'hurry.resource',
+        ],
+    entry_points={},
+    )

Added: hurry.zoperesource/trunk/src/hurry/__init__.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/__init__.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/__init__.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,7 @@
+# this is a namespace package
+try:
+    import pkg_resources
+    pkg_resources.declare_namespace(__name__)
+except ImportError:
+    import pkgutil
+    __path__ = pkgutil.extend_path(__path__, __name__)

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/README.txt
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/README.txt	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/README.txt	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,53 @@
+Zope integration for hurry.resource
+===================================
+
+This package provides Zope integration for hurry.resource. This means
+three taking care of three things:
+
+* maintain the needed resources throughout the request/response cycle.
+
+* know how to make a URL to a resource.
+
+* make so that resource references are automatically inserted in the
+  HTML header.
+
+This library fulfills these conditions for a Zope 3/Grok setup.
+
+We'll run through a few tests to demonstrate it. Note that the real
+code being tested is not in this document itself, but in the views described
+in ``ftesting.zcml``.
+
+We need to be in a request to make this work, so let's up a request to
+a page we have set up in ``ftesting.zcml`` that should cause the
+inclusion of a single resource in its header::
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open('http://localhost/hurry.zoperesource.test_single')
+  >>> print browser.contents 
+  <html>
+  <head>
+      <script type="text/javascript" src="http://localhost/@@/foo/a.js"></script>
+  <BLANKLINE>
+  </head>
+  <body>
+  <p>the widget HTML itself</p>
+  </body>
+  </html>
+
+If a resource happens to need another resource, this resource is also
+automatically included::
+
+  >>> browser.open('http://localhost/hurry.zoperesource.test_multiple')
+  >>> print browser.contents 
+  <html>
+  <head>
+      <script type="text/javascript" src="http://localhost/@@/foo/a.js"></script>
+    <script type="text/javascript" src="http://localhost/@@/foo/b.js"></script>
+  <BLANKLINE>
+  </head>
+  <body>
+  <p>the widget HTML itself</p>
+  </body>
+  </html>

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/__init__.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/__init__.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/__init__.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1 @@
+#

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/configure.zcml
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/configure.zcml	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/configure.zcml	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,24 @@
+<configure
+  xmlns="http://namespaces.zope.org/zope"
+  xmlns:grok="http://namespaces.zope.org/grok">
+
+  <include package="z3c.autoinclude" file="meta.zcml" />
+
+  <includeDependencies package="." />
+
+  <grok:grok package="."/>
+
+  <utility
+      component=".zopesupport.Request"
+      provides="zope.app.publication.interfaces.IBrowserRequestFactory"
+      />
+
+  <class class=".zopesupport.Request">
+    <require like_class="zope.publisher.browser.BrowserRequest" />
+  </class>
+
+  <class class=".zopesupport.Response">
+    <require like_class="zope.publisher.browser.BrowserResponse" />
+  </class>
+
+</configure>

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/__init__.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/__init__.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/__init__.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,2 @@
+#
+

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/ftesting.zcml
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/ftesting.zcml	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/ftesting.zcml	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,49 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser"
+    i18n_domain="zope"
+    package="hurry.zoperesource.tests">
+
+  <!-- set up enough of Zope to run the tests -->
+
+  <include package="zope.app.zcmlfiles" />
+  <include package="zope.app.authentication" />
+
+  <!-- principals -->
+
+  <unauthenticatedPrincipal
+      id="zope.anybody"
+      title="Unauthenticated User" />
+
+  <include package="zope.securitypolicy" file="meta.zcml"/>
+
+  <securityPolicy
+      component="zope.securitypolicy.zopepolicy.ZopeSecurityPolicy" />
+
+  <role id="zope.Anonymous" title="Everybody"
+        description="All users have this role implicitly" />
+
+  <grant permission="zope.View"
+         role="zope.Anonymous" />
+
+  <include package="hurry.zoperesource"/>
+
+  <!-- some pages to use in the tests -->
+
+  <browser:page
+    for="zope.interface.Interface"
+    name="hurry.zoperesource.test_single"
+    permission="zope.Public"
+    template="test_single.pt"
+    class=".view.TestSingle"
+    />
+
+  <browser:page
+    for="zope.interface.Interface"
+    name="hurry.zoperesource.test_multiple"
+    permission="zope.Public"
+    template="test_multiple.pt"
+    class=".view.TestMultiple"
+    />
+
+</configure>

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_multiple.pt
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_multiple.pt	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_multiple.pt	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,7 @@
+<html>
+<head>
+</head>
+<body>
+<p tal:content="view/widget"></p>
+</body>
+</html>

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_single.pt
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_single.pt	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/test_single.pt	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,7 @@
+<html>
+<head>
+</head>
+<body>
+<p tal:content="view/widget"></p>
+</body>
+</html>

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/tests.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/tests.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/tests.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,27 @@
+import os
+import unittest
+import doctest
+
+from zope.app.testing.functional import FunctionalDocFileSuite
+from zope.app.testing import functional
+
+FunctionalLayer = functional.ZCMLLayer(
+    os.path.join(os.path.split(__file__)[0], 'ftesting.zcml'),
+    __name__, 'FunctionalLayer')
+
+def test_suite():
+    optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
+    globs = {}
+    
+    suite = unittest.TestSuite()
+    
+    readme = FunctionalDocFileSuite(
+        '../README.txt',
+        optionflags=optionflags,
+        globs=globs)
+
+    readme.layer = FunctionalLayer
+    
+    suite.addTest(readme)
+
+    return suite

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/tests/view.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/tests/view.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/tests/view.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,19 @@
+from zope import component
+from hurry.resource.interfaces import ICurrentNeededInclusions
+from hurry.resource import Library, ResourceInclusion
+
+foo = Library("foo")
+
+a = ResourceInclusion(foo, "a.js")
+
+b = ResourceInclusion(foo, "b.js", depends=[a])
+
+class TestSingle(object):
+    def widget(self):
+        a.need()
+        return "the widget HTML itself"
+
+class TestMultiple(object):
+    def widget(self):
+        b.need()
+        return "the widget HTML itself"

Added: hurry.zoperesource/trunk/src/hurry/zoperesource/zopesupport.py
===================================================================
--- hurry.zoperesource/trunk/src/hurry/zoperesource/zopesupport.py	                        (rev 0)
+++ hurry.zoperesource/trunk/src/hurry/zoperesource/zopesupport.py	2008-10-07 12:13:06 UTC (rev 91843)
@@ -0,0 +1,77 @@
+# zope integration for hurry.resource
+from grokcore import component as grok
+
+from zope import interface
+from zope import component
+import zope.security.management
+from zope.publisher.interfaces import IRequest
+from zope.app.component.hooks import getSite
+from zope.traversing.browser.interfaces import IAbsoluteURL
+from zope.publisher.browser import BrowserRequest, BrowserResponse, isHTML
+from zope.app.publication.interfaces import IBrowserRequestFactory
+
+from hurry.resource import NeededInclusions, Library
+from hurry.resource.interfaces import ICurrentNeededInclusions, ILibraryUrl
+
+class CurrentNeededInclusions(grok.GlobalUtility):
+    grok.implements(ICurrentNeededInclusions)
+    grok.provides(ICurrentNeededInclusions)
+    
+    def __call__(self):
+        request = getRequest()
+        if not hasattr(request, 'hurry_resource_needed'):
+            request.hurry_resource_needed = NeededInclusions()
+        return request.hurry_resource_needed
+
+ at grok.adapter(Library)
+ at grok.implementer(ILibraryUrl)
+def library_url(library):
+    request = getRequest()
+    return str(component.getMultiAdapter((getSite(), request),
+                                         IAbsoluteURL)) + '/@@/' + library.name
+
+class Request(BrowserRequest):
+    interface.classProvides(IBrowserRequestFactory)
+
+    def _createResponse(self):
+        return Response()
+
+class Response(BrowserResponse):
+    def _implicitResult(self, body):
+        content_type = self.getHeader('content-type')
+        if content_type is None:
+            if isHTML(body):
+                content_type = 'text/html'
+            else:
+                content_type = 'text/plain'
+            self.setHeader('x-content-type-warning', 'guessed from content')
+            self.setHeader('content-type', content_type)
+        
+        # check the content type disregarding parameters and case
+        if content_type and content_type.split(';', 1)[0].lower() in (
+            'text/html', 'text/xml'):
+            # act on HTML and XML content only!
+
+            needed = component.getUtility(ICurrentNeededInclusions)()
+
+            html = needed.render()
+            
+            if html:
+                body = body.replace('<head>', '<head>\n    %s\n' %
+                                    html, 1)
+        return super(Response, self)._implicitResult(body)
+
+class NoRequestError(Exception):
+    pass
+
+def getRequest():
+    try:
+        i = zope.security.management.getInteraction() # raises NoInteraction
+    except zope.security.interfaces.NoInteraction:
+        raise NoRequestError()
+
+    for p in i.participations:
+        if IRequest.providedBy(p):
+            return p
+
+    raise NoRequestError()



More information about the Checkins mailing list