[silva.core.conf][Sylvain Viollon] Add support for Icon class.

[email protected] Tue, 27 Aug 2013 18:22:53 +0200
Newsgroups gmane.comp.web.zope.silva.cvs
Message-ID <[email protected]>
author:    Sylvain Viollon
date:      Tue Aug 27 18:22:51 2013 +0200
revision:  328:e6ffc7fbcf01 in silva.core.conf
branch:    2.4
details:   https://hg.infrae.com/silva.core.conf?cmd=changeset;node=e6ffc7fbcf01
modified:  src/silva/core/conf/martiansupport/directives.py src/silva/core/conf/utils.py
added:     
removed:   
log:       Add support for Icon class.


diffstat:

 src/silva/core/conf/martiansupport/directives.py |   5 +-
 src/silva/core/conf/utils.py                     |  67 +++++++++++++----------
 2 files changed, 40 insertions(+), 32 deletions(-)

diffs (135 lines):

diff -r 4e03da40312a -r e6ffc7fbcf01 src/silva/core/conf/martiansupport/directives.py
--- a/src/silva/core/conf/martiansupport/directives.py	Thu May 23 16:40:45 2013 +0200
+++ b/src/silva/core/conf/martiansupport/directives.py	Tue Aug 27 18:22:51 2013 +0200
@@ -5,6 +5,7 @@
 from grokcore.view.directive import  TaggedValueStoreOnce
 from silva.core.conf.martiansupport.utils import ServiceInfo
 from silva.core.conf.martiansupport.utils import TaggedValueStoreMutipleTimes
+from silva.core.interfaces import IIcon
 from zope.interface import Interface
 from martian.util import not_unicode_or_ascii
 from martian.error import GrokImportError
@@ -19,12 +20,12 @@
     default = None
 
     def validate(self, default, **optional):
-        if not_unicode_or_ascii(default):
+        if not_unicode_or_ascii(default) and not IIcon.providedBy(default):
             raise GrokImportError(
                 "The '%s' directive can only be called with "
                 "unicode or ASCII." % self.name)
         for key, value in optional.items():
-            if not_unicode_or_ascii(value):
+            if not_unicode_or_ascii(value) and not IIcon.providedBy(default):
                 raise GrokImportError(
                     "The '%s' directive can only be called with "
                     "unicode or ASCII." % self.name)
diff -r 4e03da40312a -r e6ffc7fbcf01 src/silva/core/conf/utils.py
--- a/src/silva/core/conf/utils.py	Thu May 23 16:40:45 2013 +0200
+++ b/src/silva/core/conf/utils.py	Tue Aug 27 18:22:51 2013 +0200
@@ -22,11 +22,12 @@
 from zope.location.interfaces import ISite
 from zope.publisher.interfaces.browser import IHTTPRequest
 
+from Products.Silva.icon import Icon
 from Products.Silva.icon import registry as icon_registry
 from Products.Silva.ExtensionRegistry import extensionRegistry
 from Products.Five.browser.resource import ImageResourceFactory
 from silva.core import interfaces
-from silva.core.interfaces import ISilvaNameChooser
+from silva.core.interfaces import ISilvaNameChooser, IIcon
 from silva.core.interfaces.events import ContentCreatedEvent
 from silva.core.interfaces.errors import ContentError
 from silva.core.conf.martiansupport.utils import get_service_interface
@@ -39,18 +40,6 @@
     pass
 
 
-class IconResource(ImageResourceFactory.resource):
-    """An icon is a publicly available image.
-    """
-    security = ClassSecurityInfo()
-    security.declarePublic('__call__')
-
-InitializeClass(IconResource)
-
-class IconResourceFactory(ImageResourceFactory):
-    resource = IconResource
-
-
 def normalize_identifier(identifier):
     if isinstance(identifier, unicode):
         return identifier.encode('utf-8')
@@ -327,7 +316,7 @@
             'container_filter': makeZMIFilter(class_, zmi_addable)
             }
     Products.meta_types += (info,)
-
+ 
     # register for test cleanup
     _meta_type_regs.append(class_.meta_type)
 
@@ -349,29 +338,47 @@
         methods[name] = method
         methods[name + '__roles__'] = permission_setting
 
-def registerIcon(config, extension_name, cls, icon_fs_path):
+
+# Icon handling
+
+
+class IconResource(ImageResourceFactory.resource):
+    """An icon is a publicly available image.
+    """
+    security = ClassSecurityInfo()
+    security.declarePublic('__call__')
+
+InitializeClass(IconResource)
+
+
+class IconResourceFactory(ImageResourceFactory):
+    resource = IconResource
+
+
+def registerIcon(config, extension_name, cls, icon):
     """Register icon for a class.
     """
-    if not icon_fs_path:
+    if icon is None:
         return
 
-    extension = extensionRegistry.get_extension(extension_name)
-    fs_path = os.path.join(extension.module_directory, icon_fs_path)
-    name = ''.join((
-            'icon-',
-            cls.meta_type.strip().replace(' ', '-'),
-            os.path.splitext(icon_fs_path)[1] or '.png'))
+    if not IIcon.providedBy(icon):
+        extension = extensionRegistry.get_extension(extension_name)
+        fs_path = os.path.join(extension.module_directory, icon)
+        name = ''.join((
+                'icon-',
+                cls.meta_type.strip().replace(' ', '-'),
+                os.path.splitext(icon)[1] or '.png'))
 
-    factory = IconResourceFactory(name, fs_path)
-    config.action(
-        discriminator = ('resource', name, IHTTPRequest, Interface),
-        callable = provideAdapter,
-        args = (factory, (IHTTPRequest,), Interface, name))
+        factory = IconResourceFactory(name, fs_path)
+        config.action(
+            discriminator = ('resource', name, IHTTPRequest, Interface),
+            callable = provideAdapter,
+            args = (factory, (IHTTPRequest,), Interface, name))
 
-    resource_name = "++resource++" + name
+        icon = Icon("++resource++" + name)
 
-    icon_registry.register(('meta_type', cls.meta_type), resource_name)
-    cls.icon = resource_name
+    icon_registry.register(('meta_type', cls.meta_type), icon)
+    cls.icon = icon.icon
 
 
 def cleanUp():