[Checkins] SVN: megrok.resource/trunk/ Moved the interfaces to a dedicated module.
Souheil CHELFOUH
souheil at chelfouh.com
Wed Dec 23 11:14:47 EST 2009
Log message for revision 107017:
Moved the interfaces to a dedicated module.
Now, the 'include' directive automatically assign the IResourcesIncluder to the class.
Changed:
U megrok.resource/trunk/docs/HISTORY.txt
U megrok.resource/trunk/setup.py
U megrok.resource/trunk/src/megrok/resource/README.txt
U megrok.resource/trunk/src/megrok/resource/__init__.py
U megrok.resource/trunk/src/megrok/resource/components.py
U megrok.resource/trunk/src/megrok/resource/directives.py
U megrok.resource/trunk/src/megrok/resource/event.py
A megrok.resource/trunk/src/megrok/resource/interfaces.py
-=-
Modified: megrok.resource/trunk/docs/HISTORY.txt
===================================================================
--- megrok.resource/trunk/docs/HISTORY.txt 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/docs/HISTORY.txt 2009-12-23 16:14:46 UTC (rev 107017)
@@ -1,6 +1,16 @@
Changelog
=========
+0.3 (2009-12-23)
+----------------
+
+* The IResourcesIncluder interface is now automatically implemented
+ by the class while using the `include` directive. This makes the
+ whole system a lot more natural.
+
+* The interfaces have been moved to a dedicated module. If you used
+ to import from `components`, please, correct your code.
+
0.2 (2009-12-22)
----------------
Modified: megrok.resource/trunk/setup.py
===================================================================
--- megrok.resource/trunk/setup.py 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/setup.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -3,7 +3,7 @@
from setuptools import setup, find_packages
from os.path import join
-version = '0.2'
+version = '0.3'
HISTORY = open(join("docs", "HISTORY.txt")).read()
README = open(join("src", "megrok", "resource", "README.txt")).read()
Modified: megrok.resource/trunk/src/megrok/resource/README.txt
===================================================================
--- megrok.resource/trunk/src/megrok/resource/README.txt 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/src/megrok/resource/README.txt 2009-12-23 16:14:46 UTC (rev 107017)
@@ -124,35 +124,34 @@
... grok.context(Interface)
... resource.include(css_a)
...
- ... def render(self):
- ... return u"<html><head></head></html>"
+ ... def render(self): return ""
- >>> grok.testing.grok_component('MyView', MyView)
- True
-
-If we try to render the view, we notice that the HTML header is
-empty. The resources are not included::
-
- >>> browser.open('http://localhost/@@myview')
- >>> print browser.contents
- <html><head></head></html>
-
For the resources to be automatically included during the traversal,
we need to inform the publishing machinery that our component (the
-view), is a IResourcesIncluder::
+view), is a IResourcesIncluder. This is done automatically, when using
+the "include" directive::
- >>> from zope.interface import classImplements
- >>> classImplements(MyView, resource.IResourcesIncluder)
+ >>> resource.IResourcesIncluder.implementedBy(MyView)
+ True
-Now, when we render the view, the resources should be automatically
-added to the HTML result::
+Of course, this should not remove the existing interfaces
+implementations::
- >>> browser.open('http://localhost/@@myview')
- >>> print browser.contents
- <html><head>
- <link... href="http://localhost/@@/++noop++.../somecss/a.css" />
- </head></html>
+ >>> from zope.interface import Interface
+ >>> class IMySuperIface(Interface): pass
+ >>> class MyView(view.View):
+ ... grok.context(Interface)
+ ... grok.implements(IMySuperIface)
+ ... resource.include(css_a)
+ ...
+ ... def render(self): return "<html><head></head></html>"
+
+ >>> resource.IResourcesIncluder.implementedBy(MyView)
+ True
+ >>> IMySuperIface.implementedBy(MyView)
+ True
+
The `include` directive can be stacked, if several resources are to be
included::
@@ -174,7 +173,6 @@
>>> class YetAnotherView(view.View):
... grok.context(Interface)
- ... grok.implements(resource.IResourcesIncluder)
... resource.include(EasyResources)
...
... def render(self):
Modified: megrok.resource/trunk/src/megrok/resource/__init__.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/__init__.py 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/src/megrok/resource/__init__.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -4,5 +4,5 @@
from megrok.resource.directives import include, use_hash, resource
from megrok.resource.components import Library, ResourceLibrary
-from megrok.resource.components import IResourcesIncluder, ILibrary
+from megrok.resource.interfaces import IResourcesIncluder, ILibrary
from megrok.resource.utils import component_includes
Modified: megrok.resource/trunk/src/megrok/resource/components.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/components.py 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/src/megrok/resource/components.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -3,22 +3,11 @@
from grokcore.component import baseclass
from grokcore.view import DirectoryResource
from zope.component import getUtility
-from zope.interface import Interface, Attribute
from hurry.resource import ResourceInclusion, GroupInclusion
from hurry.resource.interfaces import ICurrentNeededInclusions
from megrok.resource.directives import resource
+from megrok.resource.interfaces import ILibrary
-
-class IResourcesIncluder(Interface):
- """A publishable component that can include resources.
- """
-
-
-class ILibrary(Interface):
- """A library, including resources.
- """
- name = Attribute("The name of the library needed for URL computations")
-
class Library(DirectoryResource):
"""A library that exposes resources through an URL.
Modified: megrok.resource/trunk/src/megrok/resource/directives.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/directives.py 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/src/megrok/resource/directives.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -3,6 +3,9 @@
import martian
from hurry.resource import ResourceInclusion
from hurry.resource.interfaces import IInclusion
+from megrok.resource.interfaces import IResourcesIncluder
+from zope.interface import classImplements
+from zope.interface.declarations import addClassAdvisor, _implements_advice
def validateInclusion(directive, value):
@@ -25,7 +28,18 @@
store = martian.MULTIPLE
validate = validateInclusion
+ def factory(self, resource):
+ addClassAdvisor(_resources_advice, depth=3)
+ return resource
+
+def _resources_advice(cls):
+ if include.bind().get(cls):
+ if not IResourcesIncluder.implementedBy(cls):
+ classImplements(cls, IResourcesIncluder)
+ return cls
+
+
class resource(martian.Directive):
scope = martian.CLASS
store = martian.MULTIPLE
Modified: megrok.resource/trunk/src/megrok/resource/event.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/event.py 2009-12-23 16:13:39 UTC (rev 107016)
+++ megrok.resource/trunk/src/megrok/resource/event.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -7,9 +7,9 @@
@grok.subscribe(IResourcesIncluder, IBeforeTraverseEvent)
-def handle_inclusion(view, event):
- view = removeSecurityProxy(view)
- needs = include.bind().get(view)
+def handle_inclusion(includer, event):
+ includer = removeSecurityProxy(includer)
+ needs = include.bind().get(includer)
if needs:
for resource in needs:
resource.need()
Added: megrok.resource/trunk/src/megrok/resource/interfaces.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/interfaces.py (rev 0)
+++ megrok.resource/trunk/src/megrok/resource/interfaces.py 2009-12-23 16:14:46 UTC (rev 107017)
@@ -0,0 +1,12 @@
+from zope.interface import Interface, Attribute
+
+
+class IResourcesIncluder(Interface):
+ """A publishable component that can include resources.
+ """
+
+class ILibrary(Interface):
+ """A library, including resources.
+ """
+ name = Attribute("The name of the library needed for URL computations")
+
More information about the checkins
mailing list