[Zope3-checkins] CVS: Zope3/src/zope/app/apidoc/viewmodule - browser.py:1.4

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Mar 28 18:41:44 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/apidoc/viewmodule
In directory cvs.zope.org:/tmp/cvs-serv23205/src/zope/app/apidoc/viewmodule

Modified Files:
	browser.py 
Log Message:


Added tests.



Fixed view factory data extractor.



Only the global Presentation Service has getRegisteredMatching(), so we can
only use it for now. :(



Adjusted code to expect only one factory for views, instead of a list.




=== Zope3/src/zope/app/apidoc/viewmodule/browser.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/apidoc/viewmodule/browser.py:1.3	Fri Mar  5 10:46:58 2004
+++ Zope3/src/zope/app/apidoc/viewmodule/browser.py	Sun Mar 28 18:41:43 2004
@@ -22,16 +22,37 @@
 from zope.app import zapi
 from zope.app.publisher.browser.icon import IconViewFactory
 from zope.app.apidoc.utilities import getPythonPath, getPermissionIds
+from zope.app.apidoc.utilities import columnize
 from zope.app.component.interface import searchInterfaceIds
 from zope.app.component.interface import getInterface
 
-__metaclass__ = type
-
-class Menu:
+class Menu(object):
     """Views module Menu"""
 
     def getPresentationTypes(self):
-        """Get a list of presentation types."""
+        """Get a list of presentation types.
+
+        Currently the presentation types are hard coded, which will change,
+        once we define types for them.
+
+        Example::
+
+          >>> import pprint
+          >>> pprint = pprint.PrettyPrinter(width=69).pprint
+          >>> menu = Menu()
+          >>> types = menu.getPresentationTypes()
+          >>> types = [type.items() for type in types]
+          >>> types.sort()
+          >>> pprint(types)
+          [[('path', 'zope.publisher.interfaces.browser.IBrowserRequest'),
+            ('name', 'IBrowserRequest')],
+           [('path', 'zope.publisher.interfaces.ftp.IFTPRequest'),
+            ('name', 'IFTPRequest')],
+           [('path', 'zope.publisher.interfaces.http.IHTTPRequest'),
+            ('name', 'IHTTPRequest')],
+           [('path', 'zope.publisher.interfaces.xmlrpc.IXMLRPCRequest'),
+            ('name', 'IXMLRPCRequest')]]
+        """
         return [{'name': path.split('.')[-1], 'path': path}
             for path in ['zope.publisher.interfaces.http.IHTTPRequest',
                          'zope.publisher.interfaces.browser.IBrowserRequest',
@@ -41,22 +62,107 @@
 
     def getInterfaceIds(self):
         """Get a list of the ids of all interfaces registered with the
-        interface service."""
-        ids = searchInterfaceIds()
+        interface service.
+
+        Example::
+
+          >>> import pprint
+          >>> pprint = pprint.PrettyPrinter(width=69).pprint
+          >>> menu = Menu()
+          >>> menu.getInterfaceIds()
+          ['IBrowserRequest', 'IFoo']
+        """
+        ids = searchInterfaceIds(self)
         ids.sort()
         return ids
 
-class SkinLayerUsage:
-    """View for skins, layers and usages."""
 
-    def getSkins(self):
-        return [{'name': skin, 'layers': layers}
+class SkinLayer(object):
+    """View for skins and layers."""
+
+    def getSkins(self, columns=True):
+        """Get all skins and their layers.
+
+        Example::
+
+          >>> import pprint
+          >>> pprint = pprint.PrettyPrinter(width=69).pprint
+          >>> from zope.app.apidoc.viewmodule import ViewModule
+          >>> view = SkinLayer()
+          >>> view.context = ViewModule()
+          >>> skins = view.getSkins(False)
+          >>> skins = [skin.items() for skin in skins]
+          >>> skins = [skin for skin in skins if skin.sort() is None]
+          >>> skins.sort()
+          >>> pprint(skins)
+          [[('layers', ('default',)), ('name', 'default')],
+           [('layers', ('default',)), ('name', 'skinA')],
+           [('layers', ('layer4', 'layer2', 'layer1', 'default')),
+            ('name', 'skinC')],
+           [('layers', ('layer5', 'layer4', 'default')), ('name', 'skinB')]]
+        """
+        info = [{'name': skin, 'layers': layers}
                 for skin, layers in self.context.getSkinLayerMapping().items()]
+        if columns:
+            info = columnize(info)
+        return info
 
 
 def _getFactoryData(factory):
-    """Squeeze some useful information out of the view factory"""
-    info = {'path': None, 'template': None, 'resource': None,
+    """Squeeze some useful information out of the view factory
+
+    Examples::
+
+      >>> from tests import pprintDict
+
+      The factory is a SimpleViewClass for a Page Template:
+
+      >>> from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
+      >>> view = SimpleViewClass('index.pt')
+      >>> info = _getFactoryData(view)
+      >>> pprintDict(info)
+      [('path', 'zope.app.pagetemplate.simpleviewclass.SimpleViewClass'),
+       ('referencable', False),
+       ('resource', None),
+       ('template',
+        '/opt/zope/Zope3/Zope3-Fresh/src/zope/app/apidoc/viewmodule/index.pt'),
+       ('url', None)]
+ 
+      The factory is a simple type:
+      
+      >>> info = _getFactoryData(3)
+      >>> pprintDict(info)
+      [('path', None),
+       ('referencable', False),
+       ('resource', None),
+       ('template', None),
+       ('url', None)]
+
+      The factory is an instance:
+
+      >>> class Factory(object):
+      ...     pass
+
+      >>> info = _getFactoryData(Factory())
+      >>> pprintDict(info)
+      [('path', 'zope.app.apidoc.viewmodule.browser.Factory'),
+       ('referencable', True),
+       ('resource', None),
+       ('template', None),
+       ('url', 'zope/app/apidoc/viewmodule/browser/Factory')]
+
+      The factory is a class or type:
+
+      >>> info = _getFactoryData(Factory)
+      >>> pprintDict(info)
+      [('path', 'zope.app.apidoc.viewmodule.browser.Factory'),
+       ('referencable', True),
+       ('resource', None),
+       ('template', None),
+       ('url', 'zope/app/apidoc/viewmodule/browser/Factory')]
+      
+    """
+    info = {'path': None, 'url': None, 'template': None, 'resource': None,
             'referencable': False}
 
     if hasattr(factory, '__name__') and \
@@ -71,6 +177,9 @@
         info['path'] = getPythonPath(factory.__bases__[0])
         info['referencable'] = True
 
+    elif hasattr(factory, '__name__') and factory.__name__ == 'proxyView':
+        info['path'] = factory.__module__ + '.proxyView'
+
     elif not hasattr(factory, '__name__'):
         info['path'] = getPythonPath(factory.__class__)
         info['referencable'] = True
@@ -83,23 +192,29 @@
         info['path'] = getPythonPath(factory)
         info['referencable'] = True
 
+    if info['referencable']:
+        info['url'] = info['path'].replace('.', '/')
+
     if isinstance(factory, IconViewFactory):
         info['resource'] = factory.rname
 
     return info
     
 
-class ViewsDetails:
+class ViewsDetails(object):
     """View for Views"""
 
     def __init__(self, context, request):
+        """Initialize the view."""
         self.context = context
         self.request = request
 
-        self.iface = getInterface(request['iface'])
-        self.type = getInterface(request['type'])
+        self.iface = getInterface(self.context, request['iface'])
+        self.type = getInterface(self.context, request['type'])
 
-        service = zapi.getService(context, 'Presentation')
+        # XXX: The local presentation service does not have a
+        # getRegisteredMatching() method. Sigh. 
+        service = zapi.getService(None, 'Presentation')
         self.views = service.getRegisteredMatching(object=self.iface,
                                                    request=self.type)
 
@@ -108,21 +223,52 @@
 
     def getViewsByLayers(self):
         """Generate the data structure that is used to create the list of
-        views."""
+        views.
+
+        Example::
+
+          >>> import pprint
+          >>> pprint = pprint.PrettyPrinter(width=69).pprint
+          >>> from zope.publisher.browser import TestRequest
+        
+          >>> form ={'iface': 'IFoo',
+          ...        'type': 'IBrowserRequest'}
+          >>> view = ViewsDetails(None, TestRequest(form=form))
+          >>> layer = view.getViewsByLayers()[0]
+          >>> layer['name']
+          'default'
+          >>> view = layer['views'][0]
+          >>> view['factory'] = view['factory'].items()
+          >>> view['factory'].sort()
+          >>> view = view.items()
+          >>> view.sort()
+          >>> pprint(view)
+          [('factory',
+            [('path', 'zope.app.apidoc.viewmodule.tests.FooView'),
+             ('referencable', True),
+             ('resource', None),
+             ('template', None),
+             ('url', 'zope/app/apidoc/viewmodule/tests/FooView')]),
+           ('name', u'index.html'),
+           ('read_perm', None),
+           ('required', 'zope.app.apidoc.viewmodule.tests.IFoo'),
+           ('type', 'zope.publisher.interfaces.browser.IBrowserRequest'),
+           ('write_perm', None)]
+        """
         result = []
         for layer, views in self.views.items():
             entries = []
-            for required, provided, more_req, name, factories in views:
+            for required, provided, more_req, name, factory in views:
                 if self.show_all or \
                        not (required is None or required is Interface):
                     entry = {'name' : name,
                              'required' : getPythonPath(required),
                              'type' : getPythonPath(more_req[0]),
-                             'factory' : _getFactoryData(factories[-1])
+                             'factory' : _getFactoryData(factory)
                              }
                     # Educated choice of the attribute name
                     entry.update(getPermissionIds('publishTraverse',
-                                                  klass=factories[-1]))
+                                                  klass=factory))
                     entries.append(entry)
 
             if entries:




More information about the Zope3-Checkins mailing list