[Checkins] SVN: zc.resourcelibrary/trunk/ - You can now provide an alternative "directory-resource"

Jim Fulton jim at zope.com
Sun Feb 17 15:49:44 EST 2008


Log message for revision 83992:
  - You can now provide an alternative "directory-resource"
    factory. This facilitates implementation of dynamic resources.
  

Changed:
  U   zc.resourcelibrary/trunk/CHANGES.txt
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/zcml.py

-=-
Modified: zc.resourcelibrary/trunk/CHANGES.txt
===================================================================
--- zc.resourcelibrary/trunk/CHANGES.txt	2008-02-17 20:06:18 UTC (rev 83991)
+++ zc.resourcelibrary/trunk/CHANGES.txt	2008-02-17 20:49:44 UTC (rev 83992)
@@ -5,6 +5,12 @@
 0.9.0 (2008-02-17)
 ------------------
 
+New features:
+
+- You can now provide an alternative "directory-resource"
+  factory. This facilitates implementation of dynamic resources.
+
+
 Bugs fixed:
 
 - Updated the functional-testing zcml file to get rid of a deprication

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt	2008-02-17 20:06:18 UTC (rev 83991)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/README.txt	2008-02-17 20:49:44 UTC (rev 83992)
@@ -324,6 +324,44 @@
     >>> browser.contents.count('src="http://localhost/@@/my-lib/included.js"')
     1
 
+Custom "directory" factories
+----------------------------
+
+By default, a resource directory is created when a directory directive
+is used.  You can add a factory option to specify a different
+resource-directory factory.  This can be used, for example, to provide
+dynamic resources.
+
+
+    >>> zcml("""
+    ... <configure
+    ...     xmlns="http://namespaces.zope.org/zope"
+    ...     package="zc.resourcelibrary">
+    ...
+    ...   <resourceLibrary name="my-lib">
+    ...     <directory
+    ...         source="tests/example/my-lib"
+    ...         include="foo.js"
+    ...         factory="zc.resourcelibrary.tests.tests.TestFactory"
+    ...     />
+    ...   </resourceLibrary>
+    ...
+    ... </configure>
+    ... """)
+
+The factory will be called with a source directory, a security checker
+and a name.  We've created a class that implements a resource
+directory dynamically.
+
+    >>> browser.open('http://localhost/zc.resourcelibrary.test_template_2')
+    >>> '/@@/my-lib/foo.js' in browser.contents
+    True
+
+    >>> browser.open('http://localhost/@@/my-lib/foo.js')
+    >>> print browser.contents,
+    foo = 1;
+
+
 Future Work
 -----------
 

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py	2008-02-17 20:06:18 UTC (rev 83991)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/tests/tests.py	2008-02-17 20:49:44 UTC (rev 83992)
@@ -19,12 +19,36 @@
 from zc.resourcelibrary import tal
 from zope.app.testing import functional
 from zope.configuration import xmlconfig
+import zope.interface
 from zope.pagetemplate import pagetemplate
+import zope.publisher.interfaces.browser
 import doctest
 import os
 import unittest
 import zope.security.management
 
+
+
+class TestFactory:
+
+    zope.interface.implements(
+        zope.publisher.interfaces.browser.IBrowserPublisher)
+
+    def __init__(self, source, checker, name):
+        self.__Security_checker__ = checker
+
+    def __call__(self, request):
+        return self
+    
+    def publishTraverse(self, request, name):
+        return getattr(self, name.replace('.', '_'))
+
+    def foo_js(self):
+        return 'foo = 1;\n'
+
+                       
+
+
 #### testing framework ####
 
 def zcml(s, execute=True):

Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/zcml.py
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/zcml.py	2008-02-17 20:06:18 UTC (rev 83991)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/zcml.py	2008-02-17 20:49:44 UTC (rev 83992)
@@ -64,6 +64,11 @@
         value_type=zope.schema.Text(),
         )
 
+    factory = zope.configuration.fields.GlobalObject(
+        title=u"Factory",
+        description=u"Alternate directory-resource factory",
+        required=False,
+        )
 
 def handler(name, dependencies, required, provided, adapter_name, factory, info=''):
     if dependencies:
@@ -93,7 +98,7 @@
         library_info[name] = LibraryInfo()
         library_info[name].required.extend(require)
 
-    def directory(self, _context, source, include=()):
+    def directory(self, _context, source, include=(), factory=None):
         if not os.path.isdir(source):
             raise ConfigurationError("Directory %r does not exist" % source)
 
@@ -108,8 +113,9 @@
         # is referenced
         library_info[self.name].included.extend(include)
 
-        factory = directoryresource.DirectoryResourceFactory(
-            source, self.checker, self.name)
+        if factory is None:
+            factory = directoryresource.DirectoryResourceFactory
+        factory = factory(source, self.checker, self.name)
 
         _context.action(
             discriminator = ('resource', self.name, IBrowserRequest, self.layer),



More information about the Checkins mailing list