[Checkins] SVN: megrok.icon/trunk/src/megrok/icon/ Added some text to the tests. Not using double underscore variables anymore
Souheil CHELFOUH
souheil at chelfouh.com
Thu Dec 31 08:51:45 EST 2009
Log message for revision 107446:
Added some text to the tests. Not using double underscore variables anymore
Changed:
U megrok.icon/trunk/src/megrok/icon/README.txt
U megrok.icon/trunk/src/megrok/icon/interfaces.py
U megrok.icon/trunk/src/megrok/icon/registry.py
-=-
Modified: megrok.icon/trunk/src/megrok/icon/README.txt
===================================================================
--- megrok.icon/trunk/src/megrok/icon/README.txt 2009-12-31 13:19:06 UTC (rev 107445)
+++ megrok.icon/trunk/src/megrok/icon/README.txt 2009-12-31 13:51:44 UTC (rev 107446)
@@ -97,26 +97,51 @@
None
-Implicity registration
-----------------------
+Implicit registration
+---------------------
+It can be handy to register an icon and link it to a component, in a
+single declaration. The `icon` directive provides a simple way to do
+that. The keyword argument `path` indicated that the given icon needs
+to be registered in the given registry.
+
+Let's create a new registry::
+
>>> class ContentIcons(IconRegistry):
... name('content-icons')
+The directive `icon` can then be used for an implicit registration::
+
>>> class MyDocument(object):
... icon(name="some-icon", registry=ContentIcons,
... path="tests/more/an_icon.png")
+When the directive is read and interpreted, the icon registry is not
+yet registered in the component registry. Therefore, it's impossible
+to directly add the icon. A temporary registry is used :
+ICONS_BASES. ICONS_BASES is a simple dictionnary that uses the icon
+registry class as a key and a list of tuples (icon name, icon path),
+as a value.
+
+This registry is consumed when the utility is instanciated
+therefore, it's done when it's registered in the zca::
+
>>> from megrok.icon import ICONS_BASES
>>> ICONS_BASES
{<class 'megrok.icon.tests.ContentIcons'>: [('some-icon', '.../tests/more/an_icon.png')]}
+We register the registry as an utility::
+
>>> grok_component('content-icons', ContentIcons)
True
+The temporary registry is consumed::
+
>>> ICONS_BASES
{}
+The icon is now available through the registry::
+
>>> icon_url = get_component_icon_url(MyDocument, request)
>>> icon_url
'http://127.0.0.1/++icon++content-icons/some-icon'
Modified: megrok.icon/trunk/src/megrok/icon/interfaces.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/interfaces.py 2009-12-31 13:19:06 UTC (rev 107445)
+++ megrok.icon/trunk/src/megrok/icon/interfaces.py 2009-12-31 13:51:44 UTC (rev 107446)
@@ -4,7 +4,9 @@
from zope.interface import Interface
from zope.container.constraints import contains
+ALLOWED = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']
+
class IIcon(Interface):
"""An icon resource.
"""
@@ -40,6 +42,12 @@
with the given name.
"""
- __registry__ = schema.Object(
+ allowed = schema.List(
+ title=u"Mimetypes allowed for icon files.",
+ default=ALLOWED,
+ required=True)
+
+ registry = schema.Object(
schema=IIconRegistryStorage,
- title=u"Icon resource registry")
+ title=u"Icon resource registry",
+ required=True)
Modified: megrok.icon/trunk/src/megrok/icon/registry.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/registry.py 2009-12-31 13:19:06 UTC (rev 107445)
+++ megrok.icon/trunk/src/megrok/icon/registry.py 2009-12-31 13:51:44 UTC (rev 107446)
@@ -12,8 +12,8 @@
from zope.browserresource.file import FileResourceFactory
from zope.publisher.interfaces.browser import IBrowserPage
-ALLOWED = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']
+
CHECKER = NamesChecker(list(IBrowserPage))
@@ -32,8 +32,8 @@
grok.baseclass()
grok.implements(IIconRegistry)
- __allowed__ = ALLOWED
- __registry__ = FieldProperty(IIconRegistry['__registry__'])
+ allowed = FieldProperty(IIconRegistry['allowed'])
+ registry = FieldProperty(IIconRegistry['registry'])
def _generate_registry(self):
registry = Dict()
@@ -47,8 +47,8 @@
return False
icon = Icon(name, path)
mimetype, enc = mimetypes.guess_type(path)
- if mimetype in self.__allowed__:
- self.__registry__[name] = icon
+ if mimetype in self.allowed:
+ self.registry[name] = icon
else:
print "skipping %s (%s) [WRONG MIMETYPE]" % (path, mimetype)
@@ -70,10 +70,10 @@
dirs.remove('.svn')
def registered(self, name):
- return name in self.__registry__
+ return name in self.registry
def get(self, name):
- return self.__registry__.get(name)
+ return self.registry.get(name)
def resource(self, name):
icon = self.get(name)
@@ -82,7 +82,7 @@
return FileResourceFactory(icon.path, CHECKER, icon.name)
def __init__(self):
- self.__registry__ = self._generate_registry()
+ self.registry = self._generate_registry()
name = view.name.bind().get(self)
path = view.path.bind().get(self)
if path: self.populate(path)
More information about the checkins
mailing list