[Checkins] SVN: Sandbox/cklinger/megrok.resource/trunk/ Moved the traversal adapter to its own file. It needs more work. We _could_ want to get rid of the ++noop++ namespace traverser.

Souheil CHELFOUH souheil at chelfouh.com
Fri Nov 20 09:11:29 EST 2009


Log message for revision 105906:
  Moved the traversal adapter to its own file. It needs more work. We _could_ want to get rid of the ++noop++ namespace traverser.
  Removed the __metaclass__ and restaured the grokker. The grokker now checks if a name attribute exists and is set and, otherwise, sets one using the classname or the given grok.name.
  The grokker now does a : directlyProvides(library_class, ILibrary). classProvides is not inherited AND Library is a baseclass, so it's mandatory to do that in the grokk itself.
  No more __metaclass__ insanity. All my apologies to the offended people :)
  

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/__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/ftests/test_mf.py
  A   Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py
  A   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-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/buildout.cfg	2009-11-20 14:11:29 UTC (rev 105906)
@@ -1,16 +1,17 @@
 [buildout]
-extends = versions.cfg
+extends = http://grok.zope.org/releaseinfo/grok-1.1a1.cfg
 develop = .
 parts = test omelette
 newest = false
 versions = versions
 
+
 [test]
 recipe = zc.recipe.testrunner
 eggs = megrok.resource [test]
 defaults = ['--tests-pattern', '^f?tests$', '-v', '-c']
 
+
 [omelette]
 recipe = collective.recipe.omelette
 eggs = ${test:eggs}
-

Modified: Sandbox/cklinger/megrok.resource/trunk/setup.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/setup.py	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/setup.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -30,7 +30,6 @@
 	  'hurry.zoperesource',
           'zope.app.zcmlfiles',
           'z3c.hashedresource',
-          # -*- Extra requirements: -*-
       ],
       extras_require={'test': ['hurry.jquery',],},
       entry_points="""

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/__init__.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -1,4 +1,5 @@
-from directive import *
-from components import *
 from grokcore.view import path
 from grokcore.component import name
+
+from directive import *
+from components import ILibrary, Library

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/components.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -1,43 +1,16 @@
 # -*- coding: utf-8 -*-
 
-import grokcore.component as grok
-from zope.interface import Interface
-from hurry.zoperesource.zopesupport import getRequest
+from zope.interface import Interface, Attribute
 from grokcore.view.components import DirectoryResource
-from megrok.resource import not_hashed
-from zope.component import getAdapter, getMultiAdapter
-from z3c.hashedresource.interfaces import IResourceContentsHash
-from zope.app.component.hooks import getSite
-from zope.traversing.browser.interfaces import IAbsoluteURL
-from hurry.resource.interfaces import ILibraryUrl
-from zope.interface import directlyProvides
 
 
-class Library(DirectoryResource):
-    grok.baseclass()
+class ILibrary(Interface):
+    """A library, including resources.
+    """
+    name = Attribute("The name of the library needed for URL computations")
+    
 
-    class __metaclass__(type):
-        """We do that do avoid having a grokker simply to set an attribute.
-        We could also rely on the classproperty package, but this is quite
-        straightforward.
-        """
-        def bind_grok_name(cls):
-            name = grok.name.bind().get(cls)
-            return name or cls.__name__.lower()
-        name = property(bind_grok_name) 
-
-
- at grok.adapter(Interface)
- at grok.implementer(ILibraryUrl)
-def library_url(library):
-    request = getRequest()
-    nothashed = not_hashed.bind().get(library)
-    resource = getAdapter(request, name=library.name)
-    hash = IResourceContentsHash(resource)
-    base_url = getMultiAdapter((getSite(), request), IAbsoluteURL)
-    url = '%s/@@/++noop++%s/%s' % (base_url, hash, library.name)
-
-    if nothashed:
-       url = str(getMultiAdapter((getSite(), request),
-                                         IAbsoluteURL)) + '/@@/' + library.name
-    return url
+class Library(DirectoryResource):
+    """A library that can include resources.
+    """
+    name = None

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/configure.zcml	2009-11-20 14:11:29 UTC (rev 105906)
@@ -1,15 +1,14 @@
 <configure
     xmlns="http://namespaces.zope.org/zope"
     xmlns:grok="http://namespaces.zope.org/grok"
-    i18n_domain="zope"
-    >
+    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=".components" />
     <grok:grok package=".meta" />
+    <grok:grok package=".traversal" />
 
 </configure>
 

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/directive.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -1,20 +1,17 @@
+# -*- coding: utf-8 -*-
+
 import martian
-from martian import util
-from martian.error import GrokImportError
 
 
-def default_list(factory, module=None, **data):
-    return []
-
-
-def default_library_name(factory, module=None, **data):
-    return factory.__name__.lower()
-
-
-class not_hashed(martian.MarkerDirective):
+class use_hash(martian.Directive):
     scope = martian.CLASS_OR_MODULE
+    store = martian.ONCE
+    default = True
 
+    def factory(self, value):
+        return bool(value)
 
+
 class inclusion(martian.Directive):
     scope = martian.CLASS
     store = martian.MULTIPLE
@@ -30,6 +27,7 @@
     def factory(self, value, name=None, bottom=False):
         return (value, name, bottom)
 
+
 class need(martian.Directive):
     scope = martian.CLASS
     store = martian.MULTIPLE

Modified: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_mf.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_mf.py	2009-11-20 13:52:32 UTC (rev 105905)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/ftests/test_mf.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -92,11 +92,11 @@
 class SimpleView(view.View):
     grok.context(Interface)
     resource.need(myjs)
-    resource.not_hashed()
+    resource.use_hash(False)
+
     template = view.PageTemplateFile('templates/myview.pt')
 
 
-
 ###TestSetup
 def test_suite():
     from zope.testing import doctest

Added: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py	                        (rev 0)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/meta.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+
+import martian
+import grokcore.component as grok
+from zope.interface import directlyProvides
+from megrok.resource import Library, ILibrary
+
+
+def default_library_name(factory, module=None, **data):
+    return factory.__name__.lower()
+
+
+class LibraryGrokker(martian.ClassGrokker):
+    martian.component(Library)
+    martian.directive(grok.name, get_default = default_library_name)
+
+    def execute(self, klass, config, name, **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
+
+        # We provide ILibrary. It is needed since classProvides
+        # is not inherited.
+        directlyProvides(klass, ILibrary)
+        
+        return True    

Added: Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py
===================================================================
--- Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py	                        (rev 0)
+++ Sandbox/cklinger/megrok.resource/trunk/src/megrok/resource/traversal.py	2009-11-20 14:11:29 UTC (rev 105906)
@@ -0,0 +1,29 @@
+# -*- coding: utf-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.traversing.browser.absoluteurl import absoluteURL
+
+from hurry.resource.interfaces import ILibraryUrl
+from hurry.zoperesource.zopesupport import getRequest
+from z3c.hashedresource.interfaces import IResourceContentsHash
+
+
+ at grok.implementer(ILibraryUrl)
+ at grok.adapter(megrok.resource.ILibrary)
+def library_url(library):
+    request = getRequest()
+    use_hash = megrok.resource.use_hash.bind().get(library)
+    base_url = absoluteURL(getSite(), request)
+
+    if use_hash is True:
+        resource = getAdapter(request, name=library.name)
+        hashpath = IResourceContentsHash(resource)
+        return '%s/@@/++noop++%s/%s' % (base_url, hashpath, library.name)
+        
+    return '%s/@@/%s' % (base_url, library.name)



More information about the checkins mailing list