src/pyams_viewlet/metaconfigure.py
changeset 0 b3d96ac70a99
child 47 df022d00a9c4
equal deleted inserted replaced
-1:000000000000 0:b3d96ac70a99
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_viewlet.interfaces import IViewletManager, IViewlet
       
    20 from pyramid.interfaces import IRequest, IView
       
    21 
       
    22 # import packages
       
    23 from pyams_viewlet.manager import ViewletManager, ViewletManagerFactory
       
    24 from pyramid.exceptions import ConfigurationError
       
    25 from zope.component import zcml
       
    26 from zope.component.interface import provideInterface
       
    27 from zope.interface import Interface, classImplements
       
    28 
       
    29 
       
    30 def ViewletManagerDirective(_context, name,
       
    31                             context=Interface,
       
    32                             layer=IRequest,
       
    33                             view=IView,
       
    34                             provides=IViewletManager,
       
    35                             class_=None,
       
    36                             permission=None):
       
    37 
       
    38     # If class is not given we use the basic viewlet manager.
       
    39     if class_ is None:
       
    40         class_ = ViewletManager
       
    41 
       
    42     # Create a new class based on the class.
       
    43     cdict = {'permission': permission}
       
    44     new_class = ViewletManagerFactory(name, provides, bases=(class_,), cdict=cdict)
       
    45 
       
    46     # Register interfaces
       
    47     _handle_for(_context, context)
       
    48     zcml.interface(_context, view)
       
    49 
       
    50     # Create a checker for the viewlet manager
       
    51     # checker.defineChecker(new_class, checker.Checker(required))
       
    52 
       
    53     # register a viewlet manager
       
    54     _context.action(discriminator=('viewletManager', context, layer, view, name),
       
    55                     callable=zcml.handler,
       
    56                     args=('registerAdapter', new_class,
       
    57                           (context, layer, view),
       
    58                           provides, name, _context.info))
       
    59 
       
    60 
       
    61 def ViewletDirective(_context, name, class_,
       
    62                      context=Interface,
       
    63                      layer=IRequest,
       
    64                      view=IView,
       
    65                      manager=IViewletManager,
       
    66                      attribute='render',
       
    67                      permission=None,
       
    68                      **kwargs):
       
    69 
       
    70     # Make sure the has the right form, if specified.
       
    71     if attribute != 'render':
       
    72         if not hasattr(class_, attribute):
       
    73             raise ConfigurationError("The provided class doesn't have the specified attribute")
       
    74 
       
    75     cdict = {}
       
    76     cdict['__name__'] = name
       
    77     cdict['__page_attribute__'] = attribute
       
    78     cdict['permission'] = permission
       
    79     cdict.update(kwargs)
       
    80     new_class = type(class_.__name__, (class_,), cdict)
       
    81 
       
    82     classImplements(new_class, IViewlet)
       
    83 
       
    84     # Register the interfaces.
       
    85     _handle_for(_context, context)
       
    86     zcml.interface(_context, view)
       
    87 
       
    88     # register viewlet
       
    89     _context.action(discriminator=('viewlet', context, layer, view, manager, name),
       
    90                     callable=zcml.handler,
       
    91                     args=('registerAdapter', new_class,
       
    92                           (context, layer, view, manager),
       
    93                           IViewlet, name, _context.info))
       
    94 
       
    95 
       
    96 def _handle_for(_context, for_):
       
    97     if for_ is not None:
       
    98         _context.action(discriminator=None,
       
    99                         callable=provideInterface,
       
   100                         args=('', for_))