Author: bree
Date: Mon Apr  3 16:01:02 2006
New Revision: 2766

Added:
   azax/branches/plugin/plugins/EXTERNALS.TXT
   azax/branches/plugin/plugins/interfaces.py
   azax/branches/plugin/plugins/registry.py
Modified:
   azax/branches/plugin/   (props changed)
   azax/branches/plugin/EXTERNALS.TXT
   azax/branches/plugin/configure.zcml
   azax/branches/plugin/meta.zcml
   azax/branches/plugin/plugins/   (props changed)
   azax/branches/plugin/plugins/__init__.py
   azax/branches/plugin/plugins/command.py
   azax/branches/plugin/plugins/configure.py
   azax/branches/plugin/plugins/configure.zcml
   azax/branches/plugin/plugins/directives.py
   azax/branches/plugin/plugins/meta.zcml
   azax/branches/plugin/plugins/plugin.py
   azax/branches/plugin/tests/test_azaxview.py
Log:
Change plugin registration; make cooperate with concatresource.

This means that the additional plugins are now transparently registered into the
kukit.js resource.

Modified: azax/branches/plugin/EXTERNALS.TXT
==============================================================================
--- azax/branches/plugin/EXTERNALS.TXT  (original)
+++ azax/branches/plugin/EXTERNALS.TXT  Mon Apr  3 16:01:02 2006
@@ -2,7 +2,7 @@
 # results of svn propget svn:externals
 # https://svn.z3lab.org/z3lab/azax/trunk
 #
-# created by: svn propset svn:externals -F EXTERNALS.TXT .
+# You can update your working dir by: 
+#     svn propset svn:externals -F EXTERNALS.TXT .
 #
 kukit http://codespeak.net/svn/kukit/branch/plugin
-

Modified: azax/branches/plugin/configure.zcml
==============================================================================
--- azax/branches/plugin/configure.zcml (original)
+++ azax/branches/plugin/configure.zcml Mon Apr  3 16:01:02 2006
@@ -1,14 +1,16 @@
 <configure xmlns="http://namespaces.zope.org/zope";
            xmlns:browser="http://namespaces.zope.org/browser";
            xmlns:five="http://namespaces.zope.org/five";>
-  
+ 
+  <include package=".plugins"/>
+
   <browser:resource
     file="kukit/3rd_party/sarissa.js"
     name="sarissa.js"
   />
 
-  <browser:resource
-    file="kukit/kukit/kukit.js"
+  <browser:concatresource
+    files="kukit/kukit/kukit.js"
     name="kukit.js"
   />
 
@@ -61,5 +63,5 @@
     file="tests/js/testStyleParse.js"
     name="testAzaxStyleParse.js"
   />
-
+  
 </configure>

Modified: azax/branches/plugin/meta.zcml
==============================================================================
--- azax/branches/plugin/meta.zcml      (original)
+++ azax/branches/plugin/meta.zcml      Mon Apr  3 16:01:02 2006
@@ -1,6 +1,6 @@
 <configure
      xmlns="http://namespaces.zope.org/meta";>
 
-   <include package=".plugins" />
+   <include package=".plugins" file="meta.zcml"/>
 
 </configure>

Added: azax/branches/plugin/plugins/EXTERNALS.TXT
==============================================================================
--- (empty file)
+++ azax/branches/plugin/plugins/EXTERNALS.TXT  Mon Apr  3 16:01:02 2006
@@ -0,0 +1,10 @@
+#
+# results of svn propget svn:externals
+# https://svn.z3lab.org/z3lab/azax/trunk/plugins
+#
+# You can update your working dir by: 
+#     svn propset svn:externals -F EXTERNALS.TXT .
+#
+
+# concatresource is now included like as batteries
+concatresource 
http://codespeak.net/svn/z3/jsonserver/branch/merge/concatresource

Modified: azax/branches/plugin/plugins/__init__.py
==============================================================================
--- azax/branches/plugin/plugins/__init__.py    (original)
+++ azax/branches/plugin/plugins/__init__.py    Mon Apr  3 16:01:02 2006
@@ -2,5 +2,6 @@
 Module init
 '''
 
-from plugin import AzaxPluginError
+from registry import AzaxPluginError
+from plugin import registerPlugin
 from command import Command

Modified: azax/branches/plugin/plugins/command.py
==============================================================================
--- azax/branches/plugin/plugins/command.py     (original)
+++ azax/branches/plugin/plugins/command.py     Mon Apr  3 16:01:02 2006
@@ -6,9 +6,8 @@
 
     '''
 
-    plugins = {}
+    plugintype = 'command'
 
-    def __init__(self, name, jsresource):
-        Plugin.__init__(self, name, jsresource)
+    def __init__(self, name, jsfile):
+        Plugin.__init__(self, name, jsfile)
 
-Command.__pluginclass__ = Command

Modified: azax/branches/plugin/plugins/configure.py
==============================================================================
--- azax/branches/plugin/plugins/configure.py   (original)
+++ azax/branches/plugin/plugins/configure.py   Mon Apr  3 16:01:02 2006
@@ -1,11 +1,20 @@
 
 import os.path
 from command import Command
+from plugin import registerPlugin
 
-class AzaxConfigurationError(Exception):
-    pass
+#class AzaxConfigurationError(Exception):
+#    pass
 
-def registerCommand(_context, name, jsresource=None):
+def registerCommand(_context, name, jsfile=None):
+    'Directive that registers a command' 
+    
+    # check to see if the file exists
+    if jsfile is not None:
+        file(jsfile, 'rb').close()
 
-    command = Command(name, jsresource)
-    command.register()
+    _context.action(
+        discriminator = ('registerAzaxCommand', name, jsfile),
+        callable = registerPlugin,
+        args = (Command, name, jsfile),
+        )

Modified: azax/branches/plugin/plugins/configure.zcml
==============================================================================
--- azax/branches/plugin/plugins/configure.zcml (original)
+++ azax/branches/plugin/plugins/configure.zcml Mon Apr  3 16:01:02 2006
@@ -2,26 +2,21 @@
      xmlns:browser="http://namespaces.zope.org/browser";
      xmlns:azax="http://namespaces.zope.org/azax";
      >
- 
-     <!-- example plugin registration
      
-     <browser:resource
-          file="browser/my_plugins.js"
-          name="my_plugins.js"
-          />
+    <!-- XXX concatresource is now included locally. -->
+    <include package=".concatresource" />
 
+     <!-- example plugin registration
+     
      <azax:registerCommand
           name="myCustomCommand"
-          jsresource="/++resource++my_plugins.js"
+          jsfile="browser/my_plugins.js"
           />
   
      -->
 
-     <!-- custom components should not include the next line -->
-     <include file="meta.zcml" />
-
      <!-- register core plugins 
-          jsresource is omitted since these are defined in the core
+          jsfile is omitted since these are defined in the core
      -->
      
      <azax:registerCommand

Modified: azax/branches/plugin/plugins/directives.py
==============================================================================
--- azax/branches/plugin/plugins/directives.py  (original)
+++ azax/branches/plugin/plugins/directives.py  Mon Apr  3 16:01:02 2006
@@ -1,5 +1,6 @@
 from zope.interface import Interface
 from zope.schema import TextLine
+from zope.configuration.fields import Path
 
 class IRegisterCommandDirective(Interface):
     'Register an AZAX command plugin'
@@ -10,8 +11,8 @@
          required=True,
          )
     
-    jsresource = TextLine(
-         title=u"Javascript resource",
-         description=u"The javascript resource that defines the plugin",
+    jsfile = Path(
+         title=u"Javascript file",
+         description=u"The path of the javascript file that defines the 
plugin",
          required=False,
          )

Added: azax/branches/plugin/plugins/interfaces.py
==============================================================================
--- (empty file)
+++ azax/branches/plugin/plugins/interfaces.py  Mon Apr  3 16:01:02 2006
@@ -0,0 +1,7 @@
+from zope.interface import Interface
+
+class IAzaxPluginRegistry(Interface):
+    '''Utility of registration
+
+    This allows components to register additional AZAX plugins.
+    '''

Modified: azax/branches/plugin/plugins/meta.zcml
==============================================================================
--- azax/branches/plugin/plugins/meta.zcml      (original)
+++ azax/branches/plugin/plugins/meta.zcml      Mon Apr  3 16:01:02 2006
@@ -1,6 +1,9 @@
 <configure
      xmlns="http://namespaces.zope.org/meta";>
- 
+
+   <!-- XXX concatresource is now included locally. -->
+   <include package=".concatresource" file="meta.zcml"/>
+
    <directives namespace="http://namespaces.zope.org/azax";>
      <directive
        name="registerCommand"

Modified: azax/branches/plugin/plugins/plugin.py
==============================================================================
--- azax/branches/plugin/plugins/plugin.py      (original)
+++ azax/branches/plugin/plugins/plugin.py      Mon Apr  3 16:01:02 2006
@@ -1,47 +1,31 @@
 
-class AzaxPluginError(Exception):
-    pass
-    
-class JsResource(object):
-
-    def __init__(self, name):
-        self.name = name
-        self.content = []
+from registry import getRegistry
 
+def registerPlugin(cls, *arg, **kw):
+    'Convenience method to help registration'
+    plugin = cls(*arg, **kw)
+    plugin.register()
+    
 class Plugin(object):
     'The base plugin class'
 
-    jsresources = {}  # the js resource files
-    plugins = None  # must be set to {} in the plugin class
-    __pluginclass__ = None # must be set to the plugin class
-    
-    def __init__(self, name, jsresource):
+    plugintype = None  # need to overwrite this with the plugin type
+
+    def __init__(self, name, jsfile):
         self.name = name
-        self.jsresource = jsresource
+        self.jsfile = jsfile
     
     def register(self):
         '''The registration method
-
-        This can be extended for a given plugin type by adding more
-        class singleton properties, etc.
         '''
-        if self.name in self.plugins:
-            raise AzaxPluginError, 'Duplicate registration attempt for plugin 
"%s" of class "%s"' % (self.name, self.__pluginclass__.__name__)
-        self.plugins[self.name] = self
-        if self.jsresource is not None:
-            try:
-                r = self.jsresources[self.jsresource]
-            except KeyError:
-                r = self.jsresources[self.jsresource] = 
JsResource(self.jsresource)
-            r.content.append((self.__pluginclass__, self.name))
-
+        getRegistry().register(self)
+        
     def isRegistered(cls, name):
         'Tells if name is a registered plugin for the pluginclass'
-        return name in cls.plugins
+        return getRegistry().isRegistered(cls.plugintype, name)
     isRegistered = classmethod(isRegistered)
     
     def checkRegistered(cls, name):
         'Check if name is a registered plugin and raise error otherwise'
-        if not cls.isRegistered(name):
-            raise AzaxPluginError, '"%s" is not a registered plugin for plugin 
class "%s"' % (name, cls.__pluginclass__.__name__)
+        getRegistry().checkRegistered(cls.plugintype, name)
     checkRegistered = classmethod(checkRegistered)

Added: azax/branches/plugin/plugins/registry.py
==============================================================================
--- (empty file)
+++ azax/branches/plugin/plugins/registry.py    Mon Apr  3 16:01:02 2006
@@ -0,0 +1,78 @@
+import zope.component
+from zope.component.exceptions import ComponentLookupError
+from interfaces import IAzaxPluginRegistry
+from zope.interface import implements
+# concatresource is an embedded product but interface is aliased to Products
+import concatresource
+from Products.concatresource.interfaces import IConcatResourceAddon
+
+def getRegistry(context=None):
+    'Convenience method to get or create the registry.'
+    try:
+        registry = zope.component.getUtility(IAzaxPluginRegistry)
+    except ComponentLookupError:
+        # Register the global utility, also register it for concatresources
+        registry = AzaxPluginRegistry()
+        utilities = 
zope.component.getService(zope.component.servicenames.Utilities)
+        utilities.provideUtility(IAzaxPluginRegistry, registry)
+        utilities.provideUtility(IConcatResourceAddon, registry, 
name='kukit.js')
+    return registry
+    
+class AzaxPluginError(Exception):
+    pass
+    
+class JsFile(object):
+
+    def __init__(self, name):
+        self.name = name
+        self.content = []
+
+class AzaxPluginRegistry(object):
+    implements(IAzaxPluginRegistry, IConcatResourceAddon)
+    
+    def __init__(self):
+        self.jsfiles = {}
+        self.plugins = {}
+    
+    def register(self, plugin):
+        'Register a plugin'
+        # register the plugin
+        if self.isRegistered(plugin.plugintype, plugin.name): 
+            raise AzaxPluginError, 'Duplicate registration attempt for plugin 
"%s" of type "%s"' % (self.name, self.plugintype)
+        plugins_for_class = self.plugins[plugin.plugintype]
+        plugins_for_class[plugin.name] = plugin
+        # register jsfile
+        if plugin.jsfile is not None:
+            try:
+                r = self.jsfiles[plugin.jsfile]
+            except KeyError:
+                r = self.jsfiles[plugin.jsfile] = JsFile(plugin.jsfile)
+            r.content.append((plugin.plugintype, plugin.name))
+
+    def isRegistered(self, plugintype, name):
+        'Tells if name is a registered plugin for the plugintype'
+        try:
+            self.getRegistered(plugintype, name)
+        except AzaxPluginError:
+            return False
+        else:
+            return True
+    
+    def getRegistered(self, plugintype, name):
+        'Get the registered plugin and raise error otherwise'
+        try:
+            plugins_for_class = self.plugins[plugintype]
+        except KeyError:
+            plugins_for_class = self.plugins[plugintype] = {}
+        try:
+            plugin = plugins_for_class[name]
+        except KeyError:
+            raise AzaxPluginError, '"%s" is not a registered plugin for plugin 
type "%s"' % (name, plugintype)
+        return plugin
+            
+    def checkRegistered(self, plugintype, name):
+        'Check if name is a registered plugin and raise error otherwise'
+        self.getRegistered(plugintype, name)
+
+    def getAddonFiles(self):
+        return self.jsfiles.keys()

Modified: azax/branches/plugin/tests/test_azaxview.py
==============================================================================
--- azax/branches/plugin/tests/test_azaxview.py (original)
+++ azax/branches/plugin/tests/test_azaxview.py Mon Apr  3 16:01:02 2006
@@ -87,9 +87,6 @@
         else:
             import Products.Five.zcml
             Products.Five.zcml._context = None
-        from Products.azax import plugins
-        plugins.Command.plugins = {}
-        plugins.plugin.jsresources = {}
 
 class TestAzaxView(AzaxViewTestCase):
     
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to