[Checkins] SVN: Sandbox/cklinger/megrok.resource/trunk/ Simplified the directives, the grokker and the inclusion.

Souheil CHELFOUH souheil at chelfouh.com
Sat Dec 19 20:01:32 EST 2009


Log message for revision 106788:
  Simplified the directives, the grokker and the inclusion.
  We now can easily include ResourceInclusion in a view.
  The traversal remains kind of tricky. I'm not sure how to handle this.
  Right now, the `include` directive is taken care of during the traversal,
  if the class which uses it implements a specific interface.
  

Changed:
  U   Sandbox/cklinger/megrok.resource/trunk/buildout.cfg
  U   Sandbox/cklinger/megrok.resource/trunk/setup.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/README.txt
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/event.py
  D   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftesting.zcml
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/__init__.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/ftesting.zcml
  A   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_general.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py
  U   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py

-=-
Modified: Sandbox/cklinger/megrok.resource/trunk/buildout.cfg
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/buildout.cfg	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/buildout.cfg	2009-12-20 01:01:32 UTC (rev 106788)
@@ -6,6 +6,10 @@
 versions = versions
 
 
+[versions]
+grokcore.view = 1.13.1
+
+
 [test]
 recipe = zc.recipe.testrunner
 eggs = megrok.resource [test]

Modified: Sandbox/cklinger/megrok.resource/trunk/setup.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/setup.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/setup.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -8,7 +8,6 @@
       description="Grok Resources based on hurry.resource",
       long_description=open("README.txt").read() + "\n" +
                        open("HISTORY.txt").read(),
-      # Get more strings from http://www.python.org/pypi?%3Aaction=list_classifiers
       classifiers=[
         "Programming Language :: Python",
         "Topic :: Software Development :: Libraries :: Python Modules",
@@ -27,6 +26,7 @@
           'setuptools',
           'grokcore.component',
           'grokcore.view',
+          'hurry.resource >= 0.4.1',
 	  'hurry.zoperesource',
           'zope.app.zcmlfiles',
           'z3c.hashedresource',

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/README.txt
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/README.txt	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/README.txt	2009-12-20 01:01:32 UTC (rev 106788)
@@ -10,7 +10,7 @@
  - hurry.resource
  - hurry.zoperesource
  - grokcore.view.ResourceDirectory
- - z3c.hashedresource (if our zope.app.publisher >= 3.8.2)
+ - z3c.hashedresource
 
 
 How does it work:
@@ -20,22 +20,17 @@
 -----
 
   >>> from zope.app.testing.functional import getRootFolder
-  >>> from zope.app.component.site import SiteManagerContainer 
-  >>> from zope.app.component.site import LocalSiteManager
-  >>> from zope.app.component.hooks import setSite
+  >>> from zope.site import SiteManagerContainer 
+  >>> from zope.site import LocalSiteManager
+  >>> from zope.site.hooks import setSite
 
-  >>> class Application(SiteManagerContainer):
-  ...     pass
-
   >>> root = getRootFolder()
-  >>> root['myapp'] = myapp = Application()
+  >>> root['myapp'] = myapp = SiteManagerContainer()
 
 Our application has to be a Site to access the right url for the inclusions
 
   >>> root['myapp'].setSiteManager(LocalSiteManager(root['myapp']))
   >>> setSite(root['myapp'])
-  >>> root['myapp']
-  <megrok.resource.ftests.Application object at ...>
 
 
 Inclusions & Library
@@ -44,38 +39,83 @@
 Let's start with a Library. A Library is in the context of megrok.resource
 a ResourceDirectory which holds a kind of different ResourceInclusions
 
-  >>> from megrok.resource import Library, inclusion, include
+  >>> from megrok.resource import Library, ResourceInclusion, include
   >>> import grokcore.view as view 
   >>> import grokcore.component as grok 
 
-  >>> class MyStyles(Library):
-  ...    view.path('css')
-  ...    grok.name('mystyles')
-  ...
-  ...    inclusion(name='myjs', file='a.js')  
+  >>> class SomeCSS(Library):
+  ...    view.path('ftests/css')
+  ...    view.name('somecss')
 
-  >>> grok.testing.grok_component('MyStyles', MyStyles)
+  >>> grok.testing.grok_component('somecss', SomeCSS)
   True
 
+  >>> from megrok.resource import ILibrary
+  >>> ILibrary.providedBy(SomeCSS)
+  True
 
+  >>> from zope.component import getAdapter
+  >>> from zope.publisher.browser import TestRequest
+  >>> library = getAdapter(TestRequest(), name='somecss')
+  >>> library
+  <grokcore.view.components.DirectoryResource object at ...>
+
+
 View & Include
 --------------
 
+  >>> from zope.testbrowser.testing import Browser
+  >>> from zope.component import getMultiAdapter
+
+  >>> browser = Browser()
+  >>> browser.handleErrors = False 
+
+  >>> css_a = ResourceInclusion(SomeCSS, 'a.css')
+  >>> css_b = ResourceInclusion(SomeCSS, 'b.css')
+
 To include a resource we need in the most cases a view.
 
   >>> class MyView(view.View):
-  ...     grok.context(Application)
-  ...     include(MyStyles, 'myjs')
+  ...   grok.context(SiteManagerContainer)
+  ...   include(css_a)
+  ...
+  ...   def render(self):
+  ...	  return u"<html><head></head></html>"
 
   >>> grok.testing.grok_component('MyView', MyView)
   True
 
+  >>> browser.open('http://localhost/@@myview')
+  >>> print browser.contents
+  <html><head>
+      <link rel="stylesheet" type="text/css" href="http://localhost/@@/++noop++.../somecss/a.css" />
+  </head></html>
 
-  >>> from zope.component import getMultiAdapter
-  >>> from zope.publisher.browser import TestRequest
+  >>> class AnotherView(view.View):
+  ...   grok.context(SiteManagerContainer)
+  ...   include(css_a)
+  ...   include(css_b)
+  ...
+  ...   def render(self):
+  ...	  return u"<html><head></head></html>"
 
-  >>> myview = getMultiAdapter((myapp, TestRequest()), name=u'myview')
-  >>> myview
-  <megrok.resource.ftests.MyView object at ...>
+  >>> grok.testing.grok_component('AnotherView', AnotherView)
+  True
 
-  >>> myview()
+  >>> browser.open('http://localhost/@@anotherview')
+  >>> print browser.contents
+  <html><head>
+    <link rel="stylesheet" type="text/css" href="http://localhost/@@/++noop++.../somecss/a.css" />
+    <link rel="stylesheet" type="text/css" href="http://localhost/@@/++noop++.../somecss/b.css" />
+  </head></html>
+
+
+  >>> from megrok.resource import use_hash
+  >>> use_hash.set(SomeCSS, False)
+  
+  >>> browser.open('http://localhost/@@anotherview')
+  >>> print browser.contents
+  <html><head>
+    <link rel="stylesheet" type="text/css" href="http://localhost/@@/somecss/a.css" />
+    <link rel="stylesheet" type="text/css" href="http://localhost/@@/somecss/b.css" />
+  </head></html>

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -1,5 +1,6 @@
 from grokcore.view import path
 from grokcore.component import name
 
-from directive import *
-from components import ILibrary, Library
+from directive import include, use_hash
+from components import IResourcesIncluder, ILibrary, Library
+from hurry.resource import ResourceInclusion, GroupInclusion

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -1,16 +1,26 @@
 # -*- coding: utf-8 -*-
 
-from zope.interface import Interface, Attribute
-from grokcore.view.components import DirectoryResource
+from grokcore.component import baseclass
+from grokcore.view import View
+from zope.interface import Interface, Attribute, classImplements
 
 
+class IResourcesIncluder(Interface):
+    """A publishable component that can include resources.
+    """
+
+classImplements(View, IResourcesIncluder)
+
+
 class ILibrary(Interface):
     """A library, including resources.
     """
     name = Attribute("The name of the library needed for URL computations")
+
     
-
-class Library(DirectoryResource):
-    """A library that can include resources.
+class Library(object):
+    """A library that exposes resources through an URL.
+    This component is only used to declare a resources folder.
     """
+    baseclass()
     name = None

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml	2009-12-20 01:01:32 UTC (rev 106788)
@@ -3,12 +3,13 @@
     xmlns:grok="http://namespaces.zope.org/grok"
     i18n_domain="zope">
 
-    <include package="grokcore.component" file="meta.zcml" />
-    <include package="hurry.zoperesource" />
-    <include package="z3c.hashedresource" />
-    <grok:grok package=".event" />
-    <grok:grok package=".meta" />
-    <grok:grok package=".traversal" />
-
+  <include package="grokcore.component" file="meta.zcml" />
+  <include package="grokcore.view" file="meta.zcml" />
+  <include package="hurry.zoperesource" />
+  <include package="z3c.hashedresource" />
+  
+  <grok:grok package=".event" />
+  <grok:grok package=".traversal" />
+  <grok:grok package=".meta" />
+  
 </configure>
-

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -12,22 +12,9 @@
         return bool(value)
 
 
-class inclusion(martian.Directive):
-    scope = martian.CLASS
-    store = martian.MULTIPLE
-
-    def factory(self, name, file, depends=[], bottom=False):
-        return (name, file, depends, bottom)
-
-
 class include(martian.Directive):
     scope = martian.CLASS
     store = martian.MULTIPLE
     
-    def factory(self, value, name=None, bottom=False):
-        return (value, name, bottom)
-
-
-class need(martian.Directive):
-    scope = martian.CLASS
-    store = martian.MULTIPLE
+    def factory(self, resource):
+        return resource

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/event.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/event.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/event.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -1,19 +1,17 @@
 # -*- coding: utf-8 -*-
 
-import hurry.resource
 import grokcore.component as grok
 import grokcore.view as view
 
 from zope.app.publication.interfaces import IBeforeTraverseEvent
 from zope.security.proxy import removeSecurityProxy
-from megrok.resource import include, need
-from hurry.resource import ResourceInclusion
+from megrok.resource import include, IResourcesIncluder
 
 
- at grok.subscribe(view.View, IBeforeTraverseEvent)
+ at grok.subscribe(IResourcesIncluder, IBeforeTraverseEvent)
 def handle_inclusion(view, event):
     with_bottom = False
     view = removeSecurityProxy(view)
-    needs = need.bind().get(view)
-    for lib in needs:
-        lib.need()
+    needs = include.bind().get(view)
+    for resource in needs:
+        resource.need()

Deleted: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftesting.zcml
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftesting.zcml	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftesting.zcml	2009-12-20 01:01:32 UTC (rev 106788)
@@ -1,45 +0,0 @@
-<configure
-    xmlns="http://namespaces.zope.org/zope"
-    xmlns:grok="http://namespaces.zope.org/grok"
-    i18n_domain="zope"
-    >
-
-  <include package="zope.app.zcmlfiles" file="meta.zcml" />
-  <include package="zope.securitypolicy" file="meta.zcml" />
-
-  <include package="zope.app.zcmlfiles" />
-  <include package="zope.app.authentication" />
-
-  <!-- Load minimal support of grokcore.view, views + templates  -->
-  <include package="grokcore.view" file="meta.zcml" />
-  <include package="grokcore.view" />
-
-  <include package="megrok.resource" />
-  <grok:grok package="megrok.resource.ftests" />
-   
-
-  <securityPolicy
-      component="zope.securitypolicy.zopepolicy.ZopeSecurityPolicy"
-      />
-
-  <unauthenticatedPrincipal
-      id="zope.anybody"
-      title="Unauthenticated User"
-      />
-  <grant
-      permission="zope.View"
-      principal="zope.anybody"
-      />
-
-  <principal
-      id="zope.mgr"
-      title="Manager"
-      login="mgr"
-      password="mgrpw"
-      />
-
-  <role id="zope.Manager" title="Site Manager" />
-  <grantAll role="zope.Manager" />
-  <grant role="zope.Manager" principal="zope.mgr" />
-
-</configure>

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/__init__.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/__init__.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/__init__.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -2,8 +2,13 @@
 
 import os.path
 import megrok.resource
+import grokcore.view as view
+from megrok.resource import Library
 from zope.app.testing.functional import ZCMLLayer
 
 ftesting_zcml = os.path.join(os.path.dirname(__file__), 'ftesting.zcml')
 FunctionalLayer = ZCMLLayer(
     ftesting_zcml, __name__, 'FunctionalLayer', allow_teardown=True)
+
+class SomeCSS(Library):
+    view.path('ftests/css')

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/ftesting.zcml
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/ftesting.zcml	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/ftesting.zcml	2009-12-20 01:01:32 UTC (rev 106788)
@@ -1,22 +1,17 @@
 <configure
     xmlns="http://namespaces.zope.org/zope"
     xmlns:grok="http://namespaces.zope.org/grok"
-    i18n_domain="zope"
-    >
+    i18n_domain="zope">
 
+  <include package="grokcore.view" file="meta.zcml" />
   <include package="zope.app.zcmlfiles" file="meta.zcml" />
   <include package="zope.securitypolicy" file="meta.zcml" />
 
   <include package="zope.app.zcmlfiles" />
   <include package="zope.app.authentication" />
-
-  <!-- Load minimal support of grokcore.view, views + templates  -->
-  <include package="grokcore.view" file="meta.zcml" />
   <include package="grokcore.view" />
-
   <include package="megrok.resource" />
-  <grok:grok package="megrok.resource.ftests" />   
-
+ 
   <securityPolicy
       component="zope.securitypolicy.zopepolicy.ZopeSecurityPolicy"
       />

Added: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_general.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_general.py	                        (rev 0)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_general.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+from zope.app.testing import functional
+from zope.testing import doctest, module
+from zope.publisher.browser import TestRequest
+from megrok.resource.ftests import FunctionalLayer
+
+
+def setUp(test):
+    module.setUp(test, 'megrok.resource.ftests')
+
+def tearDown(test):
+    module.tearDown(test)
+
+def test_suite():
+    suite = unittest.TestSuite()      
+    readme = functional.FunctionalDocFileSuite(
+        '../README.txt', setUp=setUp, tearDown=tearDown)
+    readme.layer = FunctionalLayer
+    suite.addTest(readme)
+    return suite

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -2,26 +2,31 @@
 
 import martian
 import grokcore.component as grok
-from zope.interface import directlyProvides
-from megrok.resource import Library, ILibrary
+from grokcore.view.meta.directoryresource import DirectoryResourceGrokker
+from hurry.resource import ResourceInclusion, NeededInclusions
+from megrok.resource.components import Library, ILibrary
+from zope.interface import alsoProvides
 
 
 def default_library_name(factory, module=None, **data):
     return factory.__name__.lower()
 
 
-class LibraryGrokker(martian.ClassGrokker):
+class LibraryGrokker(DirectoryResourceGrokker):
     martian.component(Library)
-    martian.directive(grok.name, get_default = default_library_name)
+    martian.directive(grok.name, get_default=default_library_name)
 
-    def execute(self, klass, config, name, **kw):
+    def execute(self, factory, config, name, path, layer, **kw):
+        DirectoryResourceGrokker.execute(
+            self, factory, config, name, path, layer, **kw)
+        
         # We set the name using the grok.name or the class name
         # We do that only if the attribute is not already set.
-        if getattr(klass, 'name', None) is None:
-            klass.name = name
+        if getattr(factory, 'name', None) is None:
+            factory.name = name
 
         # We provide ILibrary. It is needed since classProvides
         # is not inherited.
-        directlyProvides(klass, ILibrary)
+        alsoProvides(factory, ILibrary)
         
         return True    

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py	2009-12-19 18:49:49 UTC (rev 106787)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py	2009-12-20 01:01:32 UTC (rev 106788)
@@ -3,10 +3,8 @@
 import megrok.resource
 import grokcore.component as grok
 
-from zope.interface import Interface
-from zope.interface import directlyProvides
 from zope.component import getAdapter
-from zope.app.component.hooks import getSite
+from zope.site.hooks import getSite
 from zope.traversing.browser.absoluteurl import absoluteURL
 
 from hurry.resource.interfaces import ILibraryUrl



More information about the checkins mailing list