[Checkins] SVN: five.grok/trunk/ Add support for DirectoryResource.

Sylvain Viollon sylvain at infrae.com
Sat Nov 15 11:27:29 EST 2008


Log message for revision 92980:
  Add support for DirectoryResource.
  
  

Changed:
  U   five.grok/trunk/docs/HISTORY.txt
  U   five.grok/trunk/setup.py
  U   five.grok/trunk/src/five/grok/__init__.py
  U   five.grok/trunk/src/five/grok/components.py
  U   five.grok/trunk/src/five/grok/meta.py

-=-
Modified: five.grok/trunk/docs/HISTORY.txt
===================================================================
--- five.grok/trunk/docs/HISTORY.txt	2008-11-15 15:39:55 UTC (rev 92979)
+++ five.grok/trunk/docs/HISTORY.txt	2008-11-15 16:27:29 UTC (rev 92980)
@@ -12,6 +12,10 @@
   with grok.PageTemplate).
   [thefunny42]
 
+* Added support for the DirectoryResource component (new in
+  `grokcore.view`_ 1.2).
+  [thefunny42]
+
 five.grok - 1.0a1 (2008-10-22)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -30,7 +34,7 @@
   of the one from zope.app.pagetemplate.
   [optilude]
 
-* Added grokcore.view support with tests.
+* Added `grokcore.view`_ support with tests.
   [regebro, jfroche, gotcha et al.]
 
 * Added tests for grok.subscriber directive.
@@ -40,6 +44,6 @@
   views).
   [regebro, gotcha]
 
-
+.. _grokcore.view: http://pypi.python.org/pypi/grokcore.view
 .. _grokcore.viewlet: http://pypi.python.org/pypi/grokcore.viewlet
 .. _grokcore.formlib: http://pypi.python.org/pypi/grokcore.formlib

Modified: five.grok/trunk/setup.py
===================================================================
--- five.grok/trunk/setup.py	2008-11-15 15:39:55 UTC (rev 92979)
+++ five.grok/trunk/setup.py	2008-11-15 16:27:29 UTC (rev 92980)
@@ -31,7 +31,7 @@
         'martian',
         'grokcore.component',
         'grokcore.formlib',
-        'grokcore.view >= 1.1',
+        'grokcore.view >= 1.2',
         'grokcore.viewlet',
         'grokcore.security',
         ],

Modified: five.grok/trunk/src/five/grok/__init__.py
===================================================================
--- five.grok/trunk/src/five/grok/__init__.py	2008-11-15 15:39:55 UTC (rev 92979)
+++ five.grok/trunk/src/five/grok/__init__.py	2008-11-15 16:27:29 UTC (rev 92980)
@@ -26,3 +26,5 @@
 from five.grok.components import ZopeTwoPageTemplate as PageTemplate
 from five.grok.components import ZopeTwoPageTemplateFile as PageTemplateFile
 
+# Override DirectoryResource to use Zope 2 one
+from five.grok.components import ZopeTwoDirectoryResource as DirectoryResource

Modified: five.grok/trunk/src/five/grok/components.py
===================================================================
--- five.grok/trunk/src/five/grok/components.py	2008-11-15 15:39:55 UTC (rev 92979)
+++ five.grok/trunk/src/five/grok/components.py	2008-11-15 16:27:29 UTC (rev 92980)
@@ -141,7 +141,7 @@
         self.setFromFilename(filename, _prefix)
 
 
-class DirectoryResource(resource.DirectoryResource):
+class ZopeTwoDirectoryResource(resource.DirectoryResource):
     # We subclass this, because we want to override the default factories for
     # the resources so that .pt and .html do not get created as page
     # templates
@@ -153,7 +153,7 @@
         resource_factories[type] = factory
 
 
-class DirectoryResourceFactory(resource.DirectoryResourceFactory):
+class ZopeTwoDirectoryResourceFactory(resource.DirectoryResourceFactory):
     # __name__ is needed if you want to get url's of resources
 
     def __init__(self, name, path):
@@ -161,7 +161,7 @@
         self.__rsrc = self.factory(path, name)
 
     def __call__(self, request):
-        resource = DirectoryResource(self.__rsrc, request)
+        resource = ZopeTwoDirectoryResource(self.__rsrc, request)
         resource.__name__ = self.__name # We need to add name
         return resource
 

Modified: five.grok/trunk/src/five/grok/meta.py
===================================================================
--- five.grok/trunk/src/five/grok/meta.py	2008-11-15 15:39:55 UTC (rev 92979)
+++ five.grok/trunk/src/five/grok/meta.py	2008-11-15 16:27:29 UTC (rev 92980)
@@ -21,6 +21,7 @@
 from zope import interface, component
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from five.grok import components
+from grokcore.view.meta.directoryresource import _get_resource_path
 from martian.error import GrokError
 
 from Products.Five.security import protectClass, protectName
@@ -52,6 +53,40 @@
         return True
 
 
+def _register_resource(config, resource_path, name, layer):
+    resource_factory = components.ZopeTwoDirectoryResourceFactory(
+        name, resource_path)
+    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.ZopeTwoDirectoryResource)
+
+    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_resource(config, resource_path, name, layer)
+
+
 class StaticResourcesGrokker(martian.GlobalGrokker):
 
     def grok(self, name, module, module_info, config, **kw):
@@ -59,39 +94,12 @@
         # 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_resource(config, resource_path, name, layer)
 
-        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())
 
-            # FIXME: This is public, we need to set security on resources ?
-            name = module_info.dotted_name
-            resource_factory = components.DirectoryResourceFactory(
-                name, resource_path)
-            adapts = (IDefaultBrowserLayer,)
-            provides = interface.Interface
-
-            config.action(
-                discriminator=('adapter', adapts, provides, name),
-                callable=component.provideAdapter,
-                args=(resource_factory, adapts, provides, name),
-                )
-            return True
-
-        return False
-
-
 class ViewletSecurityGrokker(martian.ClassGrokker):
     martian.component(five.grok.Viewlet)
     martian.directive(grokcore.security.require, name='permission')



More information about the Checkins mailing list