[Checkins] SVN: megrok.icon/trunk/src/megrok/icon/ Added base icons feed.

Souheil CHELFOUH souheil at chelfouh.com
Wed Dec 30 19:26:48 EST 2009


Log message for revision 107410:
  Added base icons feed.
  

Changed:
  U   megrok.icon/trunk/src/megrok/icon/README.txt
  U   megrok.icon/trunk/src/megrok/icon/__init__.py
  U   megrok.icon/trunk/src/megrok/icon/directive.py
  U   megrok.icon/trunk/src/megrok/icon/registry.py
  A   megrok.icon/trunk/src/megrok/icon/tests/more/
  A   megrok.icon/trunk/src/megrok/icon/tests/more/an_icon.png
  U   megrok.icon/trunk/src/megrok/icon/utils.py

-=-
Modified: megrok.icon/trunk/src/megrok/icon/README.txt
===================================================================
--- megrok.icon/trunk/src/megrok/icon/README.txt	2009-12-30 23:26:49 UTC (rev 107409)
+++ megrok.icon/trunk/src/megrok/icon/README.txt	2009-12-31 00:26:47 UTC (rev 107410)
@@ -42,13 +42,11 @@
 
   >>> browser.open('http://localhost/++icon++')
   Traceback (most recent call last):
-  ...
   NotFound: Object: <zope.site.folder.Folder object at ...>,
   name: u'++icon++'
 
   >>> browser.open('http://localhost/++icon++i-dont-exist')
   Traceback (most recent call last):
-  ...
   NotFound: Object: <zope.site.folder.Folder object at ...>,
   name: u'i-dont-exist'
 
@@ -58,19 +56,28 @@
 
   >>> browser.open('http://localhost/++icon++tests/i-dont-exist')
   Traceback (most recent call last):
-  ...
   NotFound: Object: <megrok.icon.tests.TestIcons object at ...>,
   name: u'i-dont-exist'
 
-  >>> from megrok.icon import icon, get_component_icon_url
 
-  >>> class MyContent(object):
-  ...   icon(name="emblem-web", registry="tests")
+Defining an icon for a component
+================================
 
+Setting a proper site for the browser tests
+-------------------------------------------
+
   >>> from zope.site.hooks import setSite
   >>> root = getRootFolder()
   >>> setSite(root)
 
+Using the ``icon`` directive
+----------------------------
+
+  >>> from megrok.icon import icon, get_component_icon_url
+
+  >>> class MyContent(object):
+  ...   icon(name="emblem-web", registry=TestIcons)
+
   >>> get_component_icon_url(MyContent, request)
   'http://127.0.0.1/++icon++tests/emblem-web'
 
@@ -79,15 +86,41 @@
   'http://127.0.0.1/++icon++tests/emblem-web'
 
   >>> class AnotherContent(object):
-  ...   icon(name="none", registry="tests")
+  ...   icon(name="none", registry=object)
+  Traceback (most recent call last):
+  ValueError: The specified registry is not a valid IIconRegistry.
 
-  >>> print get_component_icon_url(AnotherContent, request)
-  None
-
   >>> class YetAnotherContent(object):
-  ...   icon(name="an-icon", registry="buzz")
+  ...   icon(name="an-icon", registry=TestIcons)
 
   >>> print get_component_icon_url(YetAnotherContent, request)
   None
 
 
+Implicity registration
+----------------------
+
+  >>> class ContentIcons(IconRegistry):
+  ...   name('content-icons')
+
+  >>> class MyDocument(object):
+  ...   icon(name="some-icon", registry=ContentIcons,
+  ...        path="tests/more/an_icon.png")  
+
+  >>> from megrok.icon import ICONS_BASES
+  >>> ICONS_BASES
+  {<class 'megrok.icon.tests.ContentIcons'>: [('some-icon', '/home/trollfot/work/sandbox/megrok.icon/trunk/src/megrok/icon/tests/../tests/more/an_icon.png')]}
+
+  >>> grok_component('content-icons', ContentIcons)
+  True
+
+  >>> ICONS_BASES
+  {}
+
+  >>> icon_url = get_component_icon_url(MyDocument, request)
+  >>> icon_url
+  'http://127.0.0.1/++icon++content-icons/some-icon'
+
+  >>> browser.open(icon_url)
+  >>> browser.contents
+  '\x89PNG...'

Modified: megrok.icon/trunk/src/megrok/icon/__init__.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/__init__.py	2009-12-30 23:26:49 UTC (rev 107409)
+++ megrok.icon/trunk/src/megrok/icon/__init__.py	2009-12-31 00:26:47 UTC (rev 107410)
@@ -12,7 +12,9 @@
 log = logging.getLogger('iconregistry')
 log.addHandler(ch)
 
-from megrok.icon.directive import icon 
+ICONS_BASES = {}
+
 from megrok.icon.interfaces import IIcon, IIconRegistry, IIconRegistryStorage
+from megrok.icon.directive import icon
 from megrok.icon.registry import Icon, IconRegistry
 from megrok.icon.utils import get_icon_url, get_component_icon_url

Modified: megrok.icon/trunk/src/megrok/icon/directive.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/directive.py	2009-12-30 23:26:49 UTC (rev 107409)
+++ megrok.icon/trunk/src/megrok/icon/directive.py	2009-12-31 00:26:47 UTC (rev 107410)
@@ -1,9 +1,34 @@
+# -*- coding: utf-8 -*-
+import os
 import martian
+from sys import modules
+from megrok.icon import ICONS_BASES, IIconRegistry
 
+def validateIcon(directive, name, registry, path=None):
+    if not IIconRegistry.implementedBy(registry):
+        raise ValueError(
+            "The specified registry is not a valid IIconRegistry.")
 
+
+def feed_base(registry, name, path):
+    base = ICONS_BASES.get(registry)
+    if base is None:
+        base = ICONS_BASES[registry] = []
+    base.append((name, path))
+    
+
 class icon(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
+    validate = validateIcon
 
-    def factory(self, name, registry='common'):
+    def factory(self, name, registry, path=None):
+        if path is not None:
+            if not os.path.isfile(path):
+                pyfile = modules[self.frame.f_locals['__module__']].__file__
+                path = os.path.join(os.path.dirname(pyfile), path)
+                if not os.path.isfile(path):
+                    raise ValueError, '%r is not a valid file' % path
+            feed_base(registry, name, path)
+                
         return (name, registry)

Modified: megrok.icon/trunk/src/megrok/icon/registry.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/registry.py	2009-12-30 23:26:49 UTC (rev 107409)
+++ megrok.icon/trunk/src/megrok/icon/registry.py	2009-12-31 00:26:47 UTC (rev 107410)
@@ -4,7 +4,7 @@
 import mimetypes
 from os.path import join, getsize
 from grokcore import view, component as grok
-from megrok.icon import IIcon, IIconRegistry, IIconRegistryStorage
+from megrok.icon import ICONS_BASES, IIcon, IIconRegistry, IIconRegistryStorage
 from zc.dict import Dict
 from zope.interface import directlyProvides
 from zope.schema.fieldproperty import FieldProperty
@@ -33,6 +33,7 @@
     grok.implements(IIconRegistry)
     
     __registry__ = FieldProperty(IIconRegistry['__registry__'])
+    initial_icons = []
     allowed = ALLOWED
 
     def _generate_registry(self):
@@ -52,6 +53,10 @@
         else:
             print "skipping %s (%s) [WRONG MIMETYPE]" % (path, mimetype)
 
+    def consume(self, icons):
+        for name, path in icons:
+            self.add(name, path)
+
     def populate(self, path):
         if not os.path.isdir(path):
             path = os.path.join(os.path.dirname(__file__), path)
@@ -79,5 +84,9 @@
 
     def __init__(self):
         self.__registry__ = self._generate_registry()
+        name = view.name.bind().get(self)
         path = view.path.bind().get(self)
         if path: self.populate(path)
+        if self.__class__ in ICONS_BASES:
+            self.consume(ICONS_BASES.get(self.__class__))
+            del ICONS_BASES[self.__class__]

Added: megrok.icon/trunk/src/megrok/icon/tests/more/an_icon.png
===================================================================
(Binary files differ)


Property changes on: megrok.icon/trunk/src/megrok/icon/tests/more/an_icon.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Modified: megrok.icon/trunk/src/megrok/icon/utils.py
===================================================================
--- megrok.icon/trunk/src/megrok/icon/utils.py	2009-12-30 23:26:49 UTC (rev 107409)
+++ megrok.icon/trunk/src/megrok/icon/utils.py	2009-12-31 00:26:47 UTC (rev 107410)
@@ -15,7 +15,8 @@
     
 
 def get_component_icon_url(component, request):
-    name, registry_name = icon.bind().get(component)
+    name, factory = icon.bind().get(component)
+    registry_name = grok.name.bind().get(factory)
     registry = queryUtility(IIconRegistry, name=registry_name)
     if registry is not None:
         if registry.registered(name):



More information about the checkins mailing list