[Checkins] SVN: megrok.resource/trunk/src/megrok/resource/ Renamed directive module to directives as it contains more than one directive.
Souheil CHELFOUH
souheil at chelfouh.com
Tue Dec 22 18:42:47 EST 2009
Log message for revision 106965:
Renamed directive module to directives as it contains more than one directive.
The Library now inherits directly from DirectoryResource (grokcore.view component)
New component : the ResourceLibrary that is a mix between a resource and a library.
Read the HISTORY.txt for more info !
Changed:
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
D megrok.resource/trunk/src/megrok/resource/directive.py
A megrok.resource/trunk/src/megrok/resource/directives.py
U megrok.resource/trunk/src/megrok/resource/meta.py
-=-
Modified: megrok.resource/trunk/src/megrok/resource/README.txt
===================================================================
--- megrok.resource/trunk/src/megrok/resource/README.txt 2009-12-22 23:19:50 UTC (rev 106964)
+++ megrok.resource/trunk/src/megrok/resource/README.txt 2009-12-22 23:42:46 UTC (rev 106965)
@@ -8,7 +8,7 @@
Setup
=====
-Let's import and init the necessary work environment.
+Let's import and init the necessary work environment::
>>> import grokcore.component as grok
>>> from zope.testbrowser.testing import Browser
@@ -58,7 +58,7 @@
----------------
Resources can be declared as part of their library, with their
-dependencies.
+dependencies::
>>> css_a = resource.ResourceInclusion(SomeCSS, 'a.css')
>>> css_b = resource.ResourceInclusion(SomeCSS, 'b.css')
@@ -67,14 +67,40 @@
------------------
Sometimes, resources need to be grouped logically. They can be
-declared in a group inclusion:
+declared in a group inclusion::
>>> css_group = resource.GroupInclusion([css_a, css_b])
>>> css_group.inclusions()
[<ResourceInclusion 'a.css' in library 'somecss'>,
<ResourceInclusion 'b.css' in library 'somecss'>]
+Library resource
+----------------
+Sometimes, it can be too cumbersome to declare the resources and the
+library separatly. When the resource is not destined to be re-used, it
+can be tempting to register everything with a single declaration. The
+ResourceLibrary component is what you need in these situations.
+
+A Resource library is a mix between a library and a group
+inclusion. You need to define the usual path and name and then, the
+linked resources::
+
+ >>> class EasyResources(resource.ResourceLibrary):
+ ... resource.path('ftests/css')
+ ... resource.resource('a.css')
+ ... resource.resource('b.css')
+
+ >>> grok.testing.grok_component('someresources', EasyResources)
+ True
+
+Once the component has been grokked, the resources are available::
+
+ >>> EasyResources.inclusions()
+ [<ResourceInclusion 'a.css' in library 'easyresources'>,
+ <ResourceInclusion 'b.css' in library 'easyresources'>]
+
+
Including resources in components
=================================
@@ -105,7 +131,7 @@
True
If we try to render the view, we notice that the HTML header is
-empty. The resources are not included.
+empty. The resources are not included::
>>> browser.open('http://localhost/@@myview')
>>> print browser.contents
@@ -144,7 +170,27 @@
<link... href="http://localhost/@@/++noop++.../somecss/b.css" />
</head></html>
+A ResourceLibrary component can be included just like a normal resource::
+ >>> class YetAnotherView(view.View):
+ ... grok.context(Interface)
+ ... grok.implements(resource.IResourcesIncluder)
+ ... resource.include(EasyResources)
+ ...
+ ... def render(self):
+ ... return u"<html><head></head></html>"
+
+ >>> grok.testing.grok_component('yav', YetAnotherView)
+ True
+
+ >>> browser.open('http://localhost/@@yetanotherview')
+ >>> print browser.contents
+ <html><head>
+ <link... href="http://localhost/@@/++noop++.../easyresources/a.css" />
+ <link... href="http://localhost/@@/++noop++.../easyresources/b.css" />
+ </head></html>
+
+
Include validation
------------------
Modified: megrok.resource/trunk/src/megrok/resource/__init__.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/__init__.py 2009-12-22 23:19:50 UTC (rev 106964)
+++ megrok.resource/trunk/src/megrok/resource/__init__.py 2009-12-22 23:42:46 UTC (rev 106965)
@@ -2,6 +2,7 @@
from grokcore.component import name
from hurry.resource import ResourceInclusion, GroupInclusion
-from megrok.resource.directive import include, use_hash
-from megrok.resource.components import IResourcesIncluder, ILibrary, Library
+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.utils import component_includes
Modified: megrok.resource/trunk/src/megrok/resource/components.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/components.py 2009-12-22 23:19:50 UTC (rev 106964)
+++ megrok.resource/trunk/src/megrok/resource/components.py 2009-12-22 23:42:46 UTC (rev 106965)
@@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
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
class IResourcesIncluder(Interface):
@@ -15,9 +20,32 @@
name = Attribute("The name of the library needed for URL computations")
-class Library(object):
+class Library(DirectoryResource):
"""A library that exposes resources through an URL.
This component is only used to declare a resources folder.
"""
baseclass()
name = None
+
+
+class ResourceLibrary(Library):
+ """A library that behaves like a group inclusion.
+ This prevents code redundance for simple libraries with
+ few resources.
+ """
+ baseclass()
+ depends = []
+
+ @classmethod
+ def need(cls):
+ needed = getUtility(ICurrentNeededInclusions)()
+ needed.need(cls)
+
+ @classmethod
+ def inclusions(cls):
+ """Get all inclusions needed by this inclusion.
+ """
+ result = []
+ for depend in cls.depends:
+ result.extend(depend.inclusions())
+ return result
Deleted: megrok.resource/trunk/src/megrok/resource/directive.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/directive.py 2009-12-22 23:19:50 UTC (rev 106964)
+++ megrok.resource/trunk/src/megrok/resource/directive.py 2009-12-22 23:42:46 UTC (rev 106965)
@@ -1,25 +0,0 @@
-# -*- coding: utf-8 -*-
-
-import martian
-from hurry.resource.interfaces import IInclusion
-
-
-def validateInclusion(directive, value):
- if not IInclusion.providedBy(value):
- raise ValueError(
- "You can only include IInclusions components.")
-
-
-class use_hash(martian.Directive):
- scope = martian.CLASS_OR_MODULE
- store = martian.ONCE
- default = True
-
- def factory(self, value):
- return bool(value)
-
-
-class include(martian.Directive):
- scope = martian.CLASS
- store = martian.MULTIPLE
- validate = validateInclusion
Copied: megrok.resource/trunk/src/megrok/resource/directives.py (from rev 106819, megrok.resource/trunk/src/megrok/resource/directive.py)
===================================================================
--- megrok.resource/trunk/src/megrok/resource/directives.py (rev 0)
+++ megrok.resource/trunk/src/megrok/resource/directives.py 2009-12-22 23:42:46 UTC (rev 106965)
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+
+import martian
+from hurry.resource import ResourceInclusion
+from hurry.resource.interfaces import IInclusion
+
+
+def validateInclusion(directive, value):
+ if not IInclusion.providedBy(value):
+ raise ValueError(
+ "You can only include IInclusions components.")
+
+
+class use_hash(martian.Directive):
+ scope = martian.CLASS_OR_MODULE
+ store = martian.ONCE
+ default = True
+
+ def factory(self, value):
+ return bool(value)
+
+
+class include(martian.Directive):
+ scope = martian.CLASS
+ store = martian.MULTIPLE
+ validate = validateInclusion
+
+
+class resource(martian.Directive):
+ scope = martian.CLASS
+ store = martian.MULTIPLE
+
+ def factory(self, relpath, depends=None, bottom=False):
+ return (relpath, depends, bottom)
Modified: megrok.resource/trunk/src/megrok/resource/meta.py
===================================================================
--- megrok.resource/trunk/src/megrok/resource/meta.py 2009-12-22 23:19:50 UTC (rev 106964)
+++ megrok.resource/trunk/src/megrok/resource/meta.py 2009-12-22 23:42:46 UTC (rev 106965)
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
import martian
-import grokcore.component as grok
+from grokcore import component
+from grokcore import view
+
from grokcore.view.meta.directoryresource import DirectoryResourceGrokker
from hurry.resource import ResourceInclusion, NeededInclusions
-from megrok.resource.components import Library, ILibrary
+from hurry.resource.interfaces import IInclusion
+from megrok.resource import Library, ILibrary, ResourceLibrary
+from megrok.resource import resource
from zope.interface import alsoProvides
@@ -12,21 +16,39 @@
return factory.__name__.lower()
-class LibraryGrokker(DirectoryResourceGrokker):
+class LibraryGrokker(martian.ClassGrokker):
martian.component(Library)
- martian.directive(grok.name, get_default=default_library_name)
+ martian.priority(500)
+ martian.directive(view.path)
+ martian.directive(component.name, get_default=default_library_name)
- def execute(self, factory, config, name, path, layer, **kw):
- DirectoryResourceGrokker.execute(
- self, factory, config, name, path, layer, **kw)
-
+ def execute(self, factory, config, name, path, **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(factory, 'name', None) is None:
factory.name = name
+ # We need to make sure the name is available for the Directory
+ # Resource Grokker.
+ if not component.name.bind().get(factory):
+ component.name.set(factory, name)
+
# We provide ILibrary. It is needed since classProvides
# is not inherited.
alsoProvides(factory, ILibrary)
-
return True
+
+
+class ResourceLibraryGrokker(martian.ClassGrokker):
+ martian.component(ResourceLibrary)
+ martian.directive(resource, default=[])
+
+ def create_resources(self, library, resources):
+ for (filename, depends, bottom) in resources:
+ yield ResourceInclusion(
+ library, filename, depends=depends, bottom=bottom)
+
+ def execute(self, factory, config, resource, **kw):
+ factory.depends = list(self.create_resources(factory, resource))
+ alsoProvides(factory, IInclusion)
+ return True
More information about the checkins
mailing list