[Zope3-checkins] SVN: Zope3/branches/roger-contentprovider/src/zope/viewlet/meta Added metaconfigure and metadrective

Roger Ineichen roger at projekt01.ch
Sat Oct 8 08:54:07 EDT 2005


Log message for revision 38942:
  Added metaconfigure and metadrective

Changed:
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/meta.zcml
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/metaconfigure.py
  U   Zope3/branches/roger-contentprovider/src/zope/viewlet/metadirectives.py

-=-
Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/meta.zcml
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/meta.zcml	2005-10-08 12:39:24 UTC (rev 38941)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/meta.zcml	2005-10-08 12:54:06 UTC (rev 38942)
@@ -9,6 +9,12 @@
         handler=".metaconfigure.viewletDirective"
         />
 
+    <meta:directive
+        name="viewletManager"
+        schema=".metadirectives.IViewletManagerDirective"
+        handler=".metaconfigure.viewletManagerDirective"
+        />
+
   </meta:directives>
 
 </configure>

Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/metaconfigure.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/metaconfigure.py	2005-10-08 12:39:24 UTC (rev 38941)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/metaconfigure.py	2005-10-08 12:54:06 UTC (rev 38942)
@@ -32,9 +32,87 @@
 
 from zope.contentprovider.interfaces import IRegion
 from zope.viewlet import viewlet
+from zope.viewlet import manager
 from zope.viewlet import interfaces
 
 
+def viewletManagerDirective(_context, name, permission, viewletType,
+                     for_=Interface, layer=IDefaultBrowserLayer,
+                     class_=None, template=None, weight=0,
+                     allowed_interface=None):
+
+    required = {}
+
+    # Get the permission; mainly to correctly handle CheckerPublic.
+    permission = viewmeta._handle_permission(_context, permission)
+
+    # Either the class or template must be specified.
+    if not (class_ or template):
+        raise ConfigurationError("Must specify a class or template")
+
+    if not class_:
+        class_ = manager.ViewletManager
+
+    # Make sure that the template exists and that all low-level API methods
+    # have the right permission.
+    if template:
+        template = os.path.abspath(str(_context.path(template)))
+        if not os.path.isfile(template):
+            raise ConfigurationError("No such file", template)
+        required['__getitem__'] = permission
+
+    if template:
+        # Create a new class for the viewlet manager template and class.
+        new_class = viewlet.SimpleViewletClass(
+            template, bases=(class_, ), weight=weight)
+    else:
+        if not hasattr(class_, 'browserDefault'):
+            cdict = {
+                'browserDefault':
+                lambda self, request: (getattr(self, attribute), ())
+                }
+        else:
+            cdict = {}
+
+        cdict['_weight'] = weight
+        cdict['__name__'] = name
+        cdict['__page_attribute__'] = attribute
+        new_class = type(class_.__name__,
+                         (class_, viewlet.SimpleAttributeViewlet), cdict)
+
+    if hasattr(class_, '__implements__'):
+        classImplements(new_class, IBrowserPublisher)
+
+    # set type if not None
+    if getattr(class_, 'viewletType'):
+        classImplements(new_class, IBrowserPublisher)
+
+    # Make sure the new class implements the region
+    classImplements(new_class, region)
+
+    for attr_name in (attribute, 'browserDefault', '__call__',
+                      'publishTraverse', 'weight'):
+        required[attr_name] = permission
+
+    viewmeta._handle_allowed_interface(
+        _context, allowed_interface, permission, required)
+
+    viewmeta._handle_for(_context, for_)
+    metaconfigure.interface(_context, view)
+    metaconfigure.interface(_context, region, IRegion)
+
+    checker.defineChecker(new_class, checker.Checker(required))
+
+    # register viewlet
+    _context.action(
+        discriminator = ('viewletManager', for_, layer, view, region, name),
+        callable = metaconfigure.handler,
+        args = ('provideAdapter',
+                (for_, layer, view), region, name, new_class,
+                 _context.info),)
+
+
+
 def viewletDirective(_context, name, permission, region,
                      for_=Interface, layer=IDefaultBrowserLayer,
                      view=IBrowserView,

Modified: Zope3/branches/roger-contentprovider/src/zope/viewlet/metadirectives.py
===================================================================
--- Zope3/branches/roger-contentprovider/src/zope/viewlet/metadirectives.py	2005-10-08 12:39:24 UTC (rev 38941)
+++ Zope3/branches/roger-contentprovider/src/zope/viewlet/metadirectives.py	2005-10-08 12:54:06 UTC (rev 38942)
@@ -23,6 +23,44 @@
 from zope.app.publisher.browser import metadirectives
 
 
+class IViewletManagerDirective(metadirectives.IPagesDirective):
+    """A directive to register a new viewlet manager.
+
+    Viewlet manager registrations are very similar to page registrations, 
+    except that they are additionally qualified by a type where is used for
+    lookup viewlets of this type.
+    """
+
+    viewletType = GlobalInterface(
+        title=u"Viewlet type",
+        description=u"The type interface for viewlets.",
+        required=True)
+
+    weight = Int(
+        title=u"weight",
+        description=u"Integer key for sorting viewlets in the same region.",
+        required=False)
+
+    name = TextLine(
+        title=u"The name of the page (view)",
+        description=u"""
+        The name shows up in URLs/paths. For example 'foo' or
+        'foo.html'. This attribute is required unless you use the
+        subdirective 'page' to create sub views. If you do not have
+        sub pages, it is common to use an extension for the view name
+        such as '.html'. If you do have sub pages and you want to
+        provide a view name, you shouldn't use extensions.""",
+        required=True
+        )
+    template = Path(
+        title=u"The name of a template that implements the page.",
+        description=u"""
+        Refers to a file containing a page template (should end in
+        extension '.pt' or '.html').""",
+        required=False
+        )
+
+
 class IViewletDirective(metadirectives.IPagesDirective,
                         metadirectives.IViewPageSubdirective):
     """A directive to register a new viewlet.



More information about the Zope3-Checkins mailing list