[Checkins] SVN: grok/branches/darrylcousins-branch/ Added mars.resource

Darryl Cousins darryl at darrylcousins.net.nz
Thu Jul 5 22:08:21 EDT 2007


Log message for revision 77496:
  Added mars.resource

Changed:
  U   grok/branches/darrylcousins-branch/buildout.cfg
  A   grok/branches/darrylcousins-branch/mars.resource/
  A   grok/branches/darrylcousins-branch/mars.resource/setup.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/__init__.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/README.txt
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/__init__.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/components.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.txt
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/interfaces.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.zcml
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/__init__.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/ftesting.zcml
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resource.py
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/logo.jpg
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/site.css
  A   grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/test_all.py
  U   grok/branches/darrylcousins-branch/mars.view/src/mars/view/directive.txt

-=-
Modified: grok/branches/darrylcousins-branch/buildout.cfg
===================================================================
--- grok/branches/darrylcousins-branch/buildout.cfg	2007-07-06 01:14:14 UTC (rev 77495)
+++ grok/branches/darrylcousins-branch/buildout.cfg	2007-07-06 02:08:20 UTC (rev 77496)
@@ -1,5 +1,5 @@
 [buildout]
-develop = . mars.layer mars.template mars.macro mars.view mars.contentprovider mars.viewlet
+develop = . mars.layer mars.template mars.macro mars.view mars.contentprovider mars.viewlet mars.resource
 parts = app data instance test
 find-links = http://download.zope.org/distribution/
 
@@ -16,6 +16,7 @@
        mars.view
        mars.viewlet
        mars.contentprovider
+       mars.resource
 recipe = zc.zope3recipes:app
 site.zcml = <include package="zope.security" file="meta.zcml" />
             <include package="zope.i18n" file="meta.zcml" />
@@ -97,5 +98,6 @@
        mars.view
        mars.viewlet
        mars.contentprovider
+       mars.resource
 defaults = ['--tests-pattern', '^f?tests$', '-v']
 

Added: grok/branches/darrylcousins-branch/mars.resource/setup.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/setup.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/setup.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,33 @@
+import os
+from setuptools import setup, find_packages
+
+def read(*rnames):
+    return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
+
+setup(
+    name='mars.resource',
+    version='0.1',
+    author='Darryl Cousins',
+    author_email='darryl.cousins at tfws.org.nz',
+    url='http://www.tfws.org.nz/mars',
+    description="""\
+Martian is a library that allows the embedding of configuration
+information in Python code. Martian can then grok the system and
+do the appropriate configuration registrations.
+
+This package uses martian to define resources and resource directories""",
+    long_description=(
+        read('src/mars/resource/README.txt')
+        ),
+    packages=find_packages('src'),
+    package_dir = {'': 'src'},
+    include_package_data = True,
+    zip_safe=False,
+    license='ZPL',
+    install_requires=['setuptools',
+                      'martian',
+                      'grok',
+                     ],
+)
+
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/setup.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/__init__.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/__init__.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/__init__.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,7 @@
+try:
+    # Declare this a namespace package if pkg_resources is available.
+    import pkg_resources
+    pkg_resources.declare_namespace('mars')
+except ImportError:
+    pass
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/README.txt
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/README.txt	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/README.txt	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,31 @@
+=============
+Mars Resource
+=============
+
+Martian is a library that allows the embedding of configuration
+information in Python code. Martian can then grok the system and
+do the appropriate configuration registrations.
+
+z3c packages bring significant clarity and a pattern for forms, view and
+templates.
+
+This package uses martian to configure resources and resource directories.
+
+Example Code
+------------
+
+::
+
+    Code
+
+Directives
+----------
+
+Please see ``directive.txt``.
+
+Tests
+-----
+
+See test directory.
+
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/README.txt
___________________________________________________________________
Name: svn:keywords
   + Date Author

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/__init__.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/__init__.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/__init__.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,3 @@
+from directive import file, image, directory
+from components import ResourceFactory, ResourceDirectoryFactory
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/components.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/components.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/components.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,6 @@
+class ResourceFactory(object):
+    pass
+
+
+class ResourceDirectoryFactory(object):
+    pass


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/components.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,12 @@
+from martian.directive import (InterfaceOrClassDirective,
+                               SingleTextDirective,
+                               ClassDirectiveContext)
+
+file = SingleTextDirective('mars.resource.file',
+                           ClassDirectiveContext())
+
+image = SingleTextDirective('mars.resource.image',
+                           ClassDirectiveContext())
+
+directory = SingleTextDirective('mars.resource.directory',
+                           ClassDirectiveContext())


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.txt
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.txt	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.txt	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,34 @@
+========================
+Mars Template Directives
+========================
+
+Directives specific to this package
+-----------------------------------
+
+* mars.resource.file(name):
+  Path to the resource
+  **Required** one only of image or file for Resource
+
+* mars.resource.image(name):
+  Path to the resource
+  **Required** one only of image or file for Resource
+
+* mars.resource.directory(name):
+  Path to the resource directory
+  **Required** for ResourceDirectory
+
+Also the mars.layer directive may be used
+-----------------------------------------
+
+* mars.layer.layer(class_or_interface):
+  The layer for which the template should be available.
+  Default: zope.publisher.browser.interfaces.IDefaultBrowserLayer
+
+Relevant grok directives
+------------------------
+
+* grok.name(name):
+  Name by which resource or resource directory is located
+  **Required**
+
+[And possibly grok.require??]


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/directive.txt
___________________________________________________________________
Name: svn:keywords
   + Date Author

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/interfaces.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/interfaces.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/interfaces.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,14 @@
+import zope.interface
+
+class IMarsResourceDirectives(zope.interface.Interface):
+
+    def file(path):
+        """Path to the resource
+        Required for Resource
+        """
+
+    def directory(path):
+        """Path to the resource directory
+        Required for ResourceDirectory
+        """
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/interfaces.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,130 @@
+import os
+
+import zope.interface
+import zope.component
+from zope.security.checker import NamesChecker, CheckerPublic
+from zope.publisher.interfaces.browser import (IBrowserRequest,
+                                                IDefaultBrowserLayer)
+from zope.app.publisher.browser.fileresource import (FileResourceFactory,
+                                                    ImageResourceFactory)
+from zope.app.publisher.browser.directoryresource import DirectoryResourceFactory
+
+import grok
+from grok.util import check_permission, get_default_permission
+
+import martian
+from martian import util
+
+import mars.resource
+allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
+                 'request', '__call__')
+
+class ResourceGrokker(martian.ClassGrokker):
+    component_class = mars.resource.ResourceFactory
+
+    def grok(self, name, factory, context, module_info, templates):
+        factory.module_info = module_info
+        factory_name = factory.__name__.lower()
+
+        # we need a path to the file containing the resource
+        file_name = util.class_annotation(factory, 'mars.resource.file', '')
+        image_name = util.class_annotation(factory, 'mars.resource.image', '')
+        if file_name == '' and image_name == '':
+            raise GrokError("Either mars.resource.file or mars.resource.image"
+                            " must be defined for %s."
+                            % (factory.__name__),
+                            factory)
+
+        file = image = None
+        if image_name != '':
+            file_name = image_name
+            image = filepath = os.path.join(os.path.dirname(module_info.path), file_name)
+        else:
+            file = filepath = os.path.join(os.path.dirname(module_info.path), file_name)
+
+        if not os.path.exists(filepath):
+            filepath = None
+            # allow for absolute path to resource
+            if os.path.exists(file_name):
+                filepath = file_name
+        if filepath is None:
+            raise GrokError("No resource found for %s using path %s."
+                            " Please use mars.resource.file or"
+                            " mars.resource.image to define path to the"
+                            " file containing the resource"
+                            % (factory.__name__, file_name),
+                            factory)
+
+        provides = util.class_annotation(factory, 'grok.provides', IBrowserRequest)
+        view_layer = util.class_annotation(factory, 'mars.layer.layer',
+                                       None) or module_info.getAnnotation('mars.layer.layer',
+                                       None) or IDefaultBrowserLayer
+        view_name = util.class_annotation(factory, 'grok.name', '')
+
+        # protect resource, public by default
+# TODO this isn't working - the resource is always public.
+        permission = get_default_permission(factory)
+        if permission is not None:
+            check_permission(factory, permission)
+        if permission is None or permission == 'zope.Public':
+            checker = NamesChecker(allowed_names)
+        else:
+            checker = NamesChecker(allowed_names, permission)
+
+        if file:
+            factory = FileResourceFactory(file, checker, view_name)
+        elif image:
+            factory = ImageResourceFactory(image, checker, view_name)
+
+        #print '\nname:', view_name, 'factory:', factory, 'provides:', provides, '\n'
+        zope.component.provideAdapter(factory,
+                                 adapts=(view_layer, ),
+                                 provides=provides,
+                                 name=view_name)
+        return True
+
+class ResourceDirectoryGrokker(martian.ClassGrokker):
+    component_class = mars.resource.ResourceDirectoryFactory
+
+    def grok(self, name, factory, context, module_info, templates):
+        factory.module_info = module_info
+        factory_name = factory.__name__.lower()
+
+        # we need a path to the file containing the resource
+        directory_name = util.class_annotation(factory, 'mars.resource.directory', '')
+        directory = os.path.join(os.path.dirname(module_info.path), directory_name)
+
+        if not os.path.isdir(directory):
+            raise GrokError("No directory found for %s using path %s."
+                            " Please use mars.resource.directory"
+                            " to define path to the directory."
+                            % (factory.__name__, directory_name),
+                            factory)
+
+        provides = util.class_annotation(factory, 'grok.provides', IBrowserRequest)
+        view_layer = util.class_annotation(factory, 'mars.layer.layer',
+                                       None) or module_info.getAnnotation('mars.layer.layer',
+                                       None) or IDefaultBrowserLayer
+        view_name = util.class_annotation(factory, 'grok.name', factory_name)
+
+        # protect resource, public by default
+# TODO this isn't working - the resource is always public.
+        permission = get_default_permission(factory)
+        if permission is not None:
+            check_permission(factory, permission)
+        anames = allowed_names + ('__getitem__', 'get')
+        if permission is None or permission == 'zope.Public':
+            checker = NamesChecker(anames)
+        else:
+            checker = NamesChecker(anames, permission)
+
+        factory = DirectoryResourceFactory(directory, checker, view_name)
+
+        #print '\nname:', view_name, 'factory:', factory, 'provides:', provides, '\n'
+        zope.component.provideAdapter(factory,
+                                 adapts=(view_layer, ),
+                                 provides=provides,
+                                 name=view_name)
+        return True
+
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.zcml
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.zcml	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/meta.zcml	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,2 @@
+<grok package=".meta" xmlns="http://namespaces.zope.org/grok" />
+

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/__init__.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/__init__.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/__init__.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1 @@
+#


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/ftesting.zcml
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/ftesting.zcml	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/ftesting.zcml	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,17 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           xmlns:meta="http://namespaces.zope.org/meta"
+           xmlns:browser="http://namespaces.zope.org/browser"
+           i18n_domain="zope"
+           package="mars.template.tests">
+
+  <include package="grok" file="meta.zcml" />
+  <include package="mars.layer" file="meta.zcml" />
+  <include package="mars.resource" file="meta.zcml" />
+
+  <include package="z3c.layer.minimal.tests" file="ftesting.zcml" />
+
+  <include package="grok" />
+
+</configure>
+
+

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resource.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resource.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resource.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,61 @@
+"""
+Test the claimed directives.
+
+  >>> import grok
+  >>> grok.grok('mars.resource.tests.resource')
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> #browser.addHeader('Authorization', 'Basic mgr:mgrpw')
+
+These tests make use of minimal layer
+
+  >>> skinURL = 'http://localhost/++skin++myskin'
+  >>> browser.open(skinURL + '/++resource++site.css')
+  >>> print browser.contents
+  body {
+      background-color: white;
+      color: black;
+  }
+
+  >>> browser.open(skinURL + '/++resource++logo.jpg')
+
+And using the resource directory
+
+  >>> browser.open(skinURL + '/++resource++resources/site.css')
+  >>> print browser.contents
+  body {
+      background-color: white;
+      color: black;
+  }
+
+"""
+
+import grok
+import mars.resource
+import mars.layer
+
+class IMyLayer(mars.layer.IMinimalLayer):
+    pass
+
+# set layer on module level, all class declarations that use directive
+# mars.layer.layer will use this layer - Skin, views, resources and templates
+mars.layer.layer(IMyLayer)
+
+class MySkin(mars.layer.Skin):
+    pass
+
+# define a file resource
+class Style(mars.resource.ResourceFactory):
+    grok.name('site.css')
+    mars.resource.file('resources/site.css')
+
+# define an image resource
+class Logo(mars.resource.ResourceFactory):
+    grok.name('logo.jpg')
+    mars.resource.file('resources/logo.jpg')
+
+# define a resource directory, takes name from factory.__name__
+class Resources(mars.resource.ResourceDirectoryFactory):
+    mars.resource.directory('resources')


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resource.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/logo.jpg
===================================================================
(Binary files differ)


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/logo.jpg
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/site.css
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/site.css	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/resources/site.css	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,4 @@
+body {
+    background-color: white;
+    color: black;
+}

Added: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/test_all.py
===================================================================
--- grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/test_all.py	                        (rev 0)
+++ grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/test_all.py	2007-07-06 02:08:20 UTC (rev 77496)
@@ -0,0 +1,22 @@
+import unittest
+from pkg_resources import resource_listdir
+
+from grok.ftests.test_grok_functional import FunctionalDocTestSuite
+
+from zope.app.testing import functional
+functional.defineLayer('TestLayer', 'ftesting.zcml')
+
+def test_suite():
+    suite = unittest.TestSuite()
+    dottedname = 'mars.resource.tests.%s'
+    for name in ['resource']:
+        test = FunctionalDocTestSuite(dottedname % name)
+        test.layer = TestLayer
+        suite.addTest(test)
+
+    return suite
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+


Property changes on: grok/branches/darrylcousins-branch/mars.resource/src/mars/resource/tests/test_all.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: grok/branches/darrylcousins-branch/mars.view/src/mars/view/directive.txt
===================================================================
--- grok/branches/darrylcousins-branch/mars.view/src/mars/view/directive.txt	2007-07-06 01:14:14 UTC (rev 77495)
+++ grok/branches/darrylcousins-branch/mars.view/src/mars/view/directive.txt	2007-07-06 02:08:20 UTC (rev 77496)
@@ -36,6 +36,12 @@
   defined if the template has been registered as a named adapter.
   Default: ''
 
+* grok.require(permission):
+  Protect the view class with ``permission``.
+  ``permission`` must already be defined, e.g. using
+  grok.define_permission.
+  Default: []
+
 * grok.provides(class_or_interface):
   Interface the class is looked up as, probably wouldn't be used.
   Default: zope.interface.Interface



More information about the Checkins mailing list