[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ Make menu items named adapters instead of subscribers. This moves some

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Sep 24 17:17:52 EDT 2004


Log message for revision 27683:
  Make menu items named adapters instead of subscribers. This moves some 
  getMenu() logic automatically into the adapter registry. It's a bit nicer 
  and probably faster.
  


Changed:
  U   Zope3/trunk/src/zope/app/apidoc/viewmodule/browser.py
  U   Zope3/trunk/src/zope/app/container/browser/tests/test_adding.py
  U   Zope3/trunk/src/zope/app/container/browser/tests/test_directive.py
  U   Zope3/trunk/src/zope/app/publisher/browser/menu.py
  U   Zope3/trunk/src/zope/app/publisher/browser/tests/test_addMenuItem.py


-=-
Modified: Zope3/trunk/src/zope/app/apidoc/viewmodule/browser.py
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/viewmodule/browser.py	2004-09-24 21:16:07 UTC (rev 27682)
+++ Zope3/trunk/src/zope/app/apidoc/viewmodule/browser.py	2004-09-24 21:17:51 UTC (rev 27683)
@@ -15,7 +15,7 @@
 
 $Id$
 """
-from types import ClassType
+from types import ClassType, FunctionType
 
 from zope.interface import Interface
 from zope.component.adapter import AdapterRegistration
@@ -190,6 +190,8 @@
     elif type(factory) in (type, ClassType):
         info['path'] = getPythonPath(factory)
 
+    elif isinstance(factory, FunctionType):
+        info['path'] = getPythonPath(factory.factory)
     else:
         info['path'] = getPythonPath(factory)
 

Modified: Zope3/trunk/src/zope/app/container/browser/tests/test_adding.py
===================================================================
--- Zope3/trunk/src/zope/app/container/browser/tests/test_adding.py	2004-09-24 21:16:07 UTC (rev 27682)
+++ Zope3/trunk/src/zope/app/container/browser/tests/test_adding.py	2004-09-24 21:17:51 UTC (rev 27683)
@@ -90,7 +90,7 @@
                     {'title':title, 'action':action,
                      '_for': for_, 'extra':extra})
     zope.interface.classImplements(newclass, menuItemType)
-    ztapi.subscribe((for_, IBrowserRequest), menuItemType, newclass)
+    ztapi.provideAdapter((for_, IBrowserRequest), menuItemType, newclass, title)
 
 
 class Test(PlacelessSetup, unittest.TestCase):

Modified: Zope3/trunk/src/zope/app/container/browser/tests/test_directive.py
===================================================================
--- Zope3/trunk/src/zope/app/container/browser/tests/test_directive.py	2004-09-24 21:16:07 UTC (rev 27682)
+++ Zope3/trunk/src/zope/app/container/browser/tests/test_directive.py	2004-09-24 21:17:51 UTC (rev 27683)
@@ -28,6 +28,7 @@
 
 class Context(object):
     actions = ()
+    info = ''
     
     def action(self, discriminator, callable, args):
         self.actions += ((discriminator, callable, args), )
@@ -56,14 +57,20 @@
     >>> containerViews(context, for_=I, contents='zope.ManageContent',
     ...                add='zope.ManageContent', index='zope.View')
     >>> context
-    ((None,
+    ((('adapter',
+       (<InterfaceClass zope.app.container.browser.tests.test_directive.I>,
+        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+       <InterfaceClass zope.app.menus.zmi_views>,
+       u'Contents'),
       <function handler>,
       ('Adapters',
-       'subscribe',
+       'register',
        (<InterfaceClass zope.app.container.browser.tests.test_directive.I>,
         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
        <InterfaceClass zope.app.menus.zmi_views>,
-       <function MenuItemFactory>)),
+       u'Contents',
+       <function MenuItemFactory>,
+       '')),
      (None,
       <function provideInterface>,
       ('', <InterfaceClass zope.app.menus.zmi_views>)),
@@ -111,14 +118,20 @@
        'index.html',
        <class 'zope.app.publisher.browser.viewmeta.Contents'>,
        'info')),
-     (None,
+     (('adapter',
+       (<InterfaceClass zope.app.container.browser.tests.test_directive.I>,
+        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+       <InterfaceClass zope.app.menus.zmi_actions>,
+       u'Add'),
       <function handler>,
       ('Adapters',
-       'subscribe',
+       'register',
        (<InterfaceClass zope.app.container.browser.tests.test_directive.I>,
         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
        <InterfaceClass zope.app.menus.zmi_actions>,
-       <function MenuItemFactory>)),
+       u'Add',
+       <function MenuItemFactory>,
+       'info')),
      (None,
       <function provideInterface>,
       ('', <InterfaceClass zope.app.menus.zmi_actions>)),

Modified: Zope3/trunk/src/zope/app/publisher/browser/menu.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/menu.py	2004-09-24 21:16:07 UTC (rev 27682)
+++ Zope3/trunk/src/zope/app/publisher/browser/menu.py	2004-09-24 21:17:51 UTC (rev 27683)
@@ -29,7 +29,7 @@
 
 from zope.app import zapi
 from zope.app.component.interface import provideInterface
-from zope.app.component.metaconfigure import subscriber, proxify
+from zope.app.component.metaconfigure import adapter, proxify
 from zope.app.pagetemplate.engine import Engine
 from zope.app.publication.browser import PublicationTraverser
 from zope.app.publisher.browser import BrowserView
@@ -286,7 +286,8 @@
     ...     newclass = type(title, (BrowserMenuItem,),
     ...                     {'title':title, 'action':action, 'order':order})
     ...     classImplements(newclass, menuItemType)
-    ...     ztapi.subscribe((for_, IBrowserRequest), menuItemType, newclass)
+    ...     ztapi.provideAdapter((for_, IBrowserRequest), menuItemType,
+    ...                          newclass, title)
 
     >>> class IFoo(Interface): pass
     >>> class IFooBar(IFoo): pass
@@ -317,21 +318,12 @@
     ['i4', 'i5']
     """
     result = []
-    seen = {}
-    for item in zapi.subscribers((object, request), menuItemType):
-        # Make sure we don't repeat a specification for a given title
-        if item.title in seen:
-            continue
-        seen[item.title] = 1
-
-        if not item.available():
-            continue
-
-        result.append(item)
+    for name, item in zapi.getAdapters((object, request), menuItemType):
+        if item.available():
+            result.append(item)
+            if len(result) >= max:
+                break
         
-        if len(result) >= max:
-            break
-        
     # Now order the result. This is not as easy as it seems.
     #
     # (1) Look at the interfaces and put the more specific menu entries to the
@@ -515,7 +507,7 @@
     []
     >>> items.menuItem(context, u'view.html', 'View')
     >>> context.actions[0]['args'][:2]
-    ('Adapters', 'subscribe')
+    ('Adapters', 'register')
     >>> len(context.actions)
     4
     """
@@ -554,10 +546,11 @@
                 item = proxify(item, checker)
 
             return item
+        MenuItemFactory.factory = BrowserMenuItem
 
-        subscriber(_context, MenuItemFactory,
-                   (self.for_, IBrowserRequest), self.menuItemType)
-
+        adapter(_context, (MenuItemFactory,), self.menuItemType,
+                (self.for_, IBrowserRequest), name=title)
+        
     def __call__(self, _context):
         # Nothing to do.
         pass

Modified: Zope3/trunk/src/zope/app/publisher/browser/tests/test_addMenuItem.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/tests/test_addMenuItem.py	2004-09-24 21:16:07 UTC (rev 27682)
+++ Zope3/trunk/src/zope/app/publisher/browser/tests/test_addMenuItem.py	2004-09-24 21:17:51 UTC (rev 27683)
@@ -30,14 +30,20 @@
   <function provideInterface>,
   ('zope.component.interfaces.IFactory',
    <InterfaceClass zope.component.interfaces.IFactory>)),
- (None,
+ (('adapter',
+   (<InterfaceClass zope.app.container.interfaces.IAdding>,
+    <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+   <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
+   'Add an X'),
   <function handler>,
   ('Adapters',
-   'subscribe',
+   'register',
    (<InterfaceClass zope.app.container.interfaces.IAdding>,
     <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
    <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
-   <function MenuItemFactory>)),
+   'Add an X',
+   <function MenuItemFactory>,
+   '')),
  (None,
   <function provideInterface>,
   ('',
@@ -68,6 +74,7 @@
 
 class Context(object):
     actions = ()
+    info = ''
     
     def action(self, discriminator, callable, args):
         self.actions += ((discriminator, callable, args), )
@@ -87,14 +94,20 @@
     ...             permission="zope.ManageContent", description="blah blah",
     ...             filter="context/foo")
     >>> context
-    ((None,
+    ((('adapter',
+       (<InterfaceClass zope.app.container.interfaces.IAdding>,
+        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+       <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
+       'Add an X'),
       <function handler>,
       ('Adapters',
-       'subscribe',
+       'register',
        (<InterfaceClass zope.app.container.interfaces.IAdding>,
         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
        <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
-       <function MenuItemFactory>)),
+       'Add an X',
+       <function MenuItemFactory>,
+       '')),
      (None,
       <function provideInterface>,
       ('',
@@ -116,14 +129,20 @@
     ...             permission="zope.ManageContent", description="blah blah",
     ...             filter="context/foo", view="AddX")
     >>> context
-    ((None,
+    ((('adapter',
+       (<InterfaceClass zope.app.container.interfaces.IAdding>,
+        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+       <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
+       'Add an X'),
       <function handler>,
       ('Adapters',
-       'subscribe',
+       'register',
        (<InterfaceClass zope.app.container.interfaces.IAdding>,
         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
        <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
-       <function MenuItemFactory>)),
+       'Add an X',
+       <function MenuItemFactory>,
+       '')),
      (None,
       <function provideInterface>,
       ('',
@@ -159,14 +178,20 @@
       <function provideInterface>,
       ('zope.component.interfaces.IFactory',
        <InterfaceClass zope.component.interfaces.IFactory>)),
-     (None,
+     (('adapter',
+       (<InterfaceClass zope.app.container.interfaces.IAdding>,
+        <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
+       <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
+       'Add an X'),
       <function handler>,
       ('Adapters',
-       'subscribe',
+       'register',
        (<InterfaceClass zope.app.container.interfaces.IAdding>,
         <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>),
        <InterfaceClass zope.app.publisher.interfaces.browser.AddMenu>,
-       <function MenuItemFactory>)),
+       'Add an X',
+       <function MenuItemFactory>,
+       '')),
      (None,
       <function provideInterface>,
       ('',



More information about the Zope3-Checkins mailing list