[Checkins] SVN: Sandbox/nadako/zope.browserresource/src/zope/browserresource/ Move the icon directive to "metaconfigure" module.

Dan Korostelev nadako at gmail.com
Sun Aug 23 08:02:56 EDT 2009


Log message for revision 103103:
  Move the icon directive to "metaconfigure" module.
  Fix duplication of code that generates icon url.

Changed:
  U   Sandbox/nadako/zope.browserresource/src/zope/browserresource/icon.py
  U   Sandbox/nadako/zope.browserresource/src/zope/browserresource/meta.zcml
  U   Sandbox/nadako/zope.browserresource/src/zope/browserresource/metaconfigure.py

-=-
Modified: Sandbox/nadako/zope.browserresource/src/zope/browserresource/icon.py
===================================================================
--- Sandbox/nadako/zope.browserresource/src/zope/browserresource/icon.py	2009-08-23 11:48:50 UTC (rev 103102)
+++ Sandbox/nadako/zope.browserresource/src/zope/browserresource/icon.py	2009-08-23 12:02:56 UTC (rev 103103)
@@ -15,20 +15,8 @@
 
 $Id$
 """
-import os
-import re
-
-from zope.component.interface import provideInterface
-from zope.component.zcml import handler
-from zope.configuration.exceptions import ConfigurationError
-from zope.interface import Interface
-from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from zope.traversing.namespace import getResource
 
-from zope.browserresource import metaconfigure
-
-IName = re.compile('I[A-Za-z]+')
-
 class IconView(object):
 
     def __init__(self, context, request, rname, alt, width, height):
@@ -40,15 +28,12 @@
         self.height = height
 
     def __call__(self):
-        # The context is important here, since it becomes the parent of the
-        # icon, which is needed to generate the absolute URL.
-        resource = getResource(self.context, self.rname, self.request)
-        src = resource()
-
         return ('<img src="%s" alt="%s" width="%s" height="%s" border="0" />'
-                % (src, self.alt, self.width, self.height))
+                % (self.url(), self.alt, self.width, self.height))
 
     def url(self):
+        # The context is important here, since it becomes the parent of the
+        # icon, which is needed to generate the absolute URL.
         resource = getResource(self.context, self.rname, self.request)
         src = resource()
         return src
@@ -64,51 +49,3 @@
     def __call__(self, context, request):
         return IconView(context, request, self.rname, self.alt,
                        self.width, self.height)
-
-def IconDirective(_context, name, for_, file=None, resource=None,
-                  layer=IDefaultBrowserLayer, title=None,
-                  width=16, height=16):
-
-    iname = for_.getName()
-
-    if title is None:
-        title = iname
-        if IName.match(title):
-            title = title[1:] # Remove leading 'I'
-
-    if file is not None and resource is not None:
-        raise ConfigurationError(
-            "Can't use more than one of file, and resource "
-            "attributes for icon directives"
-            )
-    elif file is not None:
-        resource = '-'.join(for_.__module__.split('.'))
-        resource = "%s-%s-%s" % (resource, iname, name)
-        ext = os.path.splitext(file)[1]
-        if ext:
-            resource += ext
-        metaconfigure.resource(_context, image=file,
-                               name=resource, layer=layer)
-    elif resource is None:
-        raise ConfigurationError(
-            "At least one of the file, and resource "
-            "attributes for resource directives must be specified"
-            )
-
-    vfactory = IconViewFactory(resource, title, width, height)
-
-    _context.action(
-        discriminator = ('view', name, vfactory, layer),
-        callable = handler,
-        args = ('registerAdapter',
-                vfactory, (for_, layer), Interface, name, _context.info)
-        )
-
-    _context.action(
-        discriminator = None,
-        callable = provideInterface,
-        args = (for_.__module__+'.'+for_.getName(),
-                for_)
-        )
-
-    

Modified: Sandbox/nadako/zope.browserresource/src/zope/browserresource/meta.zcml
===================================================================
--- Sandbox/nadako/zope.browserresource/src/zope/browserresource/meta.zcml	2009-08-23 11:48:50 UTC (rev 103102)
+++ Sandbox/nadako/zope.browserresource/src/zope/browserresource/meta.zcml	2009-08-23 12:02:56 UTC (rev 103103)
@@ -30,7 +30,7 @@
     <meta:directive
         name="icon"
         schema=".metadirectives.IIconDirective"
-        handler=".icon.IconDirective"
+        handler=".metaconfigure.icon"
         />
 
   </meta:directives>

Modified: Sandbox/nadako/zope.browserresource/src/zope/browserresource/metaconfigure.py
===================================================================
--- Sandbox/nadako/zope.browserresource/src/zope/browserresource/metaconfigure.py	2009-08-23 11:48:50 UTC (rev 103102)
+++ Sandbox/nadako/zope.browserresource/src/zope/browserresource/metaconfigure.py	2009-08-23 12:02:56 UTC (rev 103103)
@@ -17,6 +17,7 @@
 """
 import os
 
+from zope.component.interface import provideInterface
 from zope.component.zcml import handler
 from zope.configuration.exceptions import ConfigurationError
 from zope.interface import Interface
@@ -31,6 +32,7 @@
 from zope.browserresource.fileresource import ImageResourceFactory
 from zope.browserresource.i18nfileresource import I18nFileResourceFactory
 from zope.browserresource.pagetemplateresource import PageTemplateResourceFactory
+from zope.browserresource.icon import IconViewFactory
 
 allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
                  'request', '__call__')
@@ -83,6 +85,7 @@
                 factory, (layer,), Interface, name, _context.info),
         )
 
+
 def resourceDirectory(_context, name, directory, layer=IDefaultBrowserLayer,
                       permission='zope.Public'):
     if permission == 'zope.Public':
@@ -105,6 +108,56 @@
         )
 
 
+def icon(_context, name, for_, file=None, resource=None,
+                  layer=IDefaultBrowserLayer, title=None,
+                  width=16, height=16):
+
+    iname = for_.getName()
+
+    if title is None:
+        title = iname
+        if title.startswith('I'):
+            title = title[1:] # Remove leading 'I'
+
+    if file is not None and resource is not None:
+        raise ConfigurationError(
+            "Can't use more than one of file, and resource "
+            "attributes for icon directives"
+            )
+    elif file is not None:
+        resource = '-'.join(for_.__module__.split('.'))
+        resource = "%s-%s-%s" % (resource, iname, name)
+        ext = os.path.splitext(file)[1]
+        if ext:
+            resource += ext
+
+        # give this module another name, so we can use the "resource" directive
+        # in it that won't conflict with our local variable with the same name.
+        from zope.browserresource import metaconfigure
+        metaconfigure.resource(_context, image=file, name=resource, layer=layer)
+    elif resource is None:
+        raise ConfigurationError(
+            "At least one of the file, and resource "
+            "attributes for resource directives must be specified"
+            )
+
+    vfactory = IconViewFactory(resource, title, width, height)
+
+    _context.action(
+        discriminator = ('view', name, vfactory, layer),
+        callable = handler,
+        args = ('registerAdapter',
+                vfactory, (for_, layer), Interface, name, _context.info)
+        )
+
+    _context.action(
+        discriminator = None,
+        callable = provideInterface,
+        args = (for_.__module__+'.'+for_.getName(),
+                for_)
+        )
+
+
 class I18nResource(object):
 
     type = IBrowserRequest



More information about the Checkins mailing list