[Checkins] SVN: grokcore.view/trunk/ merge jw-generic_directory_resource branch

Jan-Wijbrand Kolman janwijbrand at gmail.com
Wed Oct 15 15:25:38 EDT 2008


Log message for revision 92259:
  merge jw-generic_directory_resource branch

Changed:
  U   grokcore.view/trunk/CHANGES.txt
  U   grokcore.view/trunk/README.txt
  U   grokcore.view/trunk/src/grokcore/view/__init__.py
  U   grokcore.view/trunk/src/grokcore/view/components.py
  U   grokcore.view/trunk/src/grokcore/view/directive.py
  A   grokcore.view/trunk/src/grokcore/view/ftests/directoryresource/
  U   grokcore.view/trunk/src/grokcore/view/ftests/staticdir/simple.py
  U   grokcore.view/trunk/src/grokcore/view/ftests/test_functional.py
  A   grokcore.view/trunk/src/grokcore/view/meta/directoryresource.py
  D   grokcore.view/trunk/src/grokcore/view/meta/static.py
  A   grokcore.view/trunk/src/grokcore/view/tests/directoryresource/
  U   grokcore.view/trunk/src/grokcore/view/tests/test_all.py

-=-
Modified: grokcore.view/trunk/CHANGES.txt
===================================================================
--- grokcore.view/trunk/CHANGES.txt	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/CHANGES.txt	2008-10-15 19:25:37 UTC (rev 92259)
@@ -4,7 +4,11 @@
 1.2 (unreleased)
 ----------------
 
-* ...
+* Expose the ``DirectoryResource`` class as a component for registering
+  directories as resources. This is accompanied by the ``path`` directive that
+  is used to point to the directory holding resources by way of an relative (to
+  the module) or absolute path. ``DirectoryResource`` components can be
+  differentiated by name and layer.
 
 1.1 (2008-09-22)
 ----------------

Modified: grokcore.view/trunk/README.txt
===================================================================
--- grokcore.view/trunk/README.txt	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/README.txt	2008-10-15 19:25:37 UTC (rev 92259)
@@ -110,6 +110,30 @@
 
   <img src="hello.png" tal:attributes="src static/hello.png" />
 
+DirectoryResource
+-----------------
+
+In addition to the very convenient "static resources", one can use more
+explicitly configured and flexible DirectoryResource components.
+DirectoryResource component allow for differentiating resources based on layers
+and names and provide a way to register resources in one package and make use
+of these resources in another package's views::
+
+  class FooResource(grokcore.view.DirectoryResource):
+      grokcore.view.path('foo')
+
+Or with an explicit name::
+
+  class BarResource(grokcore.view.DirectoryResource):
+      grokcore.view.name('bar')
+      grokcore.view.path('bar')
+
+Registered for a layer::
+
+  class BazResource(grokcore.view.DirectoryResource):
+      grokcore.view.layer(ISomeLayer)
+      grokcore.view.path('baz/qux')
+
 Layers and skins
 ----------------
 
@@ -230,6 +254,11 @@
     Directive used on a layer interface to register it as skin using a
     human-readable name (``skin_name``).
 
+``path(relative_or_absolute_path)``
+    Directove used in a DirectoryResource registration to point to a non-
+    package directory(hierarchy) containing resources like images, css files,
+    etc.
+
 Other
 -----
 

Modified: grokcore.view/trunk/src/grokcore/view/__init__.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/__init__.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/__init__.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -21,7 +21,8 @@
 
 from grokcore.view.components import View
 from grokcore.view.components import PageTemplate, PageTemplateFile
-from grokcore.view.directive import layer, template, templatedir, skin
+from grokcore.view.components import DirectoryResource
+from grokcore.view.directive import layer, template, templatedir, skin, path
 from grokcore.view.util import url
 
 # Import this module so that it's available as soon as you import the

Modified: grokcore.view/trunk/src/grokcore/view/components.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/components.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/components.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -239,7 +239,6 @@
             continue
         resource_factories[type] = factory
 
-
 class DirectoryResourceFactory(directoryresource.DirectoryResourceFactory):
     # We need this to allow hooking up our own GrokDirectoryResource
     # and to set the checker to None (until we have our own checker)

Modified: grokcore.view/trunk/src/grokcore/view/directive.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/directive.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/directive.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -67,3 +67,8 @@
     scope = martian.CLASS
     store = TaggedValueStoreOnce()
     validate = martian.validateText
+
+class path(martian.Directive):
+    scope = martian.CLASS
+    store = martian.ONCE
+    validate = martian.validateText

Copied: grokcore.view/trunk/src/grokcore/view/ftests/directoryresource (from rev 92168, grokcore.view/branches/jw-generic_directory_resource/src/grokcore/view/ftests/directoryresource)

Modified: grokcore.view/trunk/src/grokcore/view/ftests/staticdir/simple.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/ftests/staticdir/simple.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/ftests/staticdir/simple.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -5,8 +5,9 @@
   >>> from zope.testbrowser.testing import Browser
   >>> browser = Browser()
   >>> browser.handleErrors = False
-  >>> browser.open('http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/'
-  ...              'file.txt')
+  >>> browser.open(
+  ...     'http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/'
+  ...     'file.txt')
   >>> print browser.contents
   some text
 
@@ -26,7 +27,9 @@
 
 Static also means that page templates will not be interpreted:
 
-  >>> browser.open('http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/static.pt')
+  >>> browser.open(
+  ...     'http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/'
+  ...     'static.pt')
   >>> print browser.contents
   <html>
   <body>
@@ -36,7 +39,9 @@
 
 We also support subdirectories for resources:
 
-  >>> browser.open('http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/subdir/otherfile.txt')
+  >>> browser.open(
+  ...     'http://localhost/@@/grokcore.view.ftests.staticdir.simple_fixture/'
+  ...     'subdir/otherfile.txt')
   >>> print browser.contents
   This is yet another file.
 

Modified: grokcore.view/trunk/src/grokcore/view/ftests/test_functional.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/ftests/test_functional.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/ftests/test_functional.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -51,6 +51,6 @@
 
 def test_suite():
     suite = unittest.TestSuite()
-    for name in ['view', 'staticdir', 'url']:
+    for name in ['view', 'staticdir', 'url', 'directoryresource']:
         suite.addTest(suiteFromPackage(name))
     return suite

Copied: grokcore.view/trunk/src/grokcore/view/meta/directoryresource.py (from rev 92168, grokcore.view/branches/jw-generic_directory_resource/src/grokcore/view/meta/directoryresource.py)
===================================================================
--- grokcore.view/trunk/src/grokcore/view/meta/directoryresource.py	                        (rev 0)
+++ grokcore.view/trunk/src/grokcore/view/meta/directoryresource.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -0,0 +1,93 @@
+#############################################################################
+#
+# Copyright (c) 2006-2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Grokkers for the static resource directory."""
+
+import os
+
+from zope import interface, component
+from zope.security.checker import NamesChecker
+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+
+import martian
+from martian.error import GrokError
+
+import grokcore.view
+from grokcore.view import components
+
+allowed_resource_names = (
+    'GET', 'HEAD', 'publishTraverse', 'browserDefault', 'request', '__call__')
+
+allowed_resourcedir_names = allowed_resource_names + ('__getitem__', 'get')
+
+def _get_resource_path(module_info, path):
+    resource_path = module_info.getResourcePath(path)
+    if os.path.isdir(resource_path):
+        static_module = module_info.getSubModuleInfo(path)
+        if static_module is not None:
+            if static_module.isPackage():
+                raise GrokError(
+                    "The '%s' resource directory must not "
+                    "be a python package." % path, module_info.getModule())
+            else:
+                raise GrokError(
+                    "A package can not contain both a '%s' "
+                    "resource directory and a module named "
+                    "'%s.py'" % (path, path), module_info.getModule())
+    return resource_path
+
+def _register(config, resource_path, name, layer):
+    # public checker by default
+    checker = NamesChecker(allowed_resourcedir_names)
+    resource_factory = components.DirectoryResourceFactory(
+        resource_path, checker, name)
+
+    adapts = (layer,)
+    provides = interface.Interface
+    config.action(
+        discriminator=('adapter', adapts, provides, name),
+        callable=component.provideAdapter,
+        args=(resource_factory, adapts, provides, name),
+        )
+    return True
+
+class DirectoryResourceGrokker(martian.ClassGrokker):
+    martian.component(components.DirectoryResource)
+
+    martian.directive(grokcore.view.name, default=None)
+    martian.directive(grokcore.view.path)
+    martian.directive(grokcore.view.layer, default=IDefaultBrowserLayer)
+
+    def grok(self, name, factory, module_info, **kw):
+        # Need to store the module info object on the directory resource
+        # class so that it can look up the actual directory.
+        factory.module_info = module_info
+        return super(DirectoryResourceGrokker, self).grok(
+            name, factory, module_info, **kw)
+
+    def execute(self, factory, config, name, path, layer, **kw):
+        resource_path = _get_resource_path(factory.module_info, path)
+        name = name or factory.module_info.dotted_name
+        return _register(config, resource_path, name, layer)
+
+class StaticResourcesGrokker(martian.GlobalGrokker):
+
+    def grok(self, name, module, module_info, config, **kw):
+        # we're only interested in static resources if this module
+        # happens to be a package
+        if not module_info.isPackage():
+            return False
+        resource_path = _get_resource_path(module_info, 'static')
+        name = module_info.dotted_name
+        layer = IDefaultBrowserLayer
+        return _register(config, resource_path, name, layer)

Deleted: grokcore.view/trunk/src/grokcore/view/meta/static.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/meta/static.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/meta/static.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -1,69 +0,0 @@
-#############################################################################
-#
-# Copyright (c) 2006-2007 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Grokkers for the static resource directory."""
-
-import os
-
-from zope import interface, component
-from zope.security.checker import NamesChecker
-from zope.publisher.interfaces.browser import IDefaultBrowserLayer
-
-import martian
-from martian.error import GrokError
-
-from grokcore.view import components
-
-allowed_resource_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
-                          'request', '__call__')
-allowed_resourcedir_names = allowed_resource_names + ('__getitem__', 'get')
-
-class StaticResourcesGrokker(martian.GlobalGrokker):
-
-    def grok(self, name, module, module_info, config, **kw):
-        # we're only interested in static resources if this module
-        # happens to be a package
-        if not module_info.isPackage():
-            return False
-
-        resource_path = module_info.getResourcePath('static')
-        if os.path.isdir(resource_path):
-            static_module = module_info.getSubModuleInfo('static')
-            if static_module is not None:
-                if static_module.isPackage():
-                    raise GrokError(
-                        "The 'static' resource directory must not "
-                        "be a python package.",
-                        module_info.getModule())
-                else:
-                    raise GrokError(
-                        "A package can not contain both a 'static' "
-                        "resource directory and a module named "
-                        "'static.py'", module_info.getModule())
-
-        # public checker by default
-        checker = NamesChecker(allowed_resourcedir_names)
-
-        resource_factory = components.DirectoryResourceFactory(
-            resource_path, checker, module_info.dotted_name)
-        adapts = (IDefaultBrowserLayer,)
-        provides = interface.Interface
-        name = module_info.dotted_name
-        config.action(
-            discriminator=('adapter', adapts, provides, name),
-            callable=component.provideAdapter,
-            args=(resource_factory, adapts, provides, name),
-            )
-        return True
-
-

Copied: grokcore.view/trunk/src/grokcore/view/tests/directoryresource (from rev 92168, grokcore.view/branches/jw-generic_directory_resource/src/grokcore/view/tests/directoryresource)

Modified: grokcore.view/trunk/src/grokcore/view/tests/test_all.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/tests/test_all.py	2008-10-15 18:24:55 UTC (rev 92258)
+++ grokcore.view/trunk/src/grokcore/view/tests/test_all.py	2008-10-15 19:25:37 UTC (rev 92259)
@@ -42,6 +42,6 @@
 
 def test_suite():
     suite = unittest.TestSuite()
-    for name in ['view', 'static', 'skin', 'template']:
+    for name in ['view', 'static', 'skin', 'template', 'directoryresource']:
         suite.addTest(suiteFromPackage(name))
     return suite



More information about the Checkins mailing list