src/pyams_pagelet/pagelet.py
changeset 12 fc3542685741
parent 7 1cfafd51022f
--- a/src/pyams_pagelet/pagelet.py	Mon Jun 11 17:30:43 2018 +0200
+++ b/src/pyams_pagelet/pagelet.py	Wed Nov 27 19:37:28 2019 +0100
@@ -10,34 +10,36 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
+"""PyAMS_pagelet.pagelet module
 
-# import standard packages
+This module provides the core pagelet implementation, and a "pagelet_config" decorator which
+can be use to register pagelets instead of ZCML directives.
+"""
+
 import logging
-logger = logging.getLogger('PyAMS (pagelet)')
 
 import venusian
+from pyramid.httpexceptions import HTTPUnauthorized
+from pyramid.interfaces import IRequest
+from pyramid.response import Response
+from pyramid_chameleon.interfaces import IChameleonTranslate
+from zope.component import queryUtility
+from zope.interface import Interface, implementer
 
-# import interfaces
 from pyams_pagelet.interfaces import IPagelet, IPageletRenderer, PageletCreatedEvent
 from pyams_template.interfaces import IContentTemplate, ILayoutTemplate
-from pyramid.interfaces import IRequest
-from pyramid_chameleon.interfaces import IChameleonTranslate
+from pyams_utils.adapter import adapter_config
+
 
-# import packages
-from pyams_utils.adapter import adapter_config
-from pyramid.httpexceptions import HTTPUnauthorized
-from pyramid.response import Response
-from zope.component import queryUtility
-from zope.interface import implementer, Interface
+__docformat__ = 'restructuredtext'
 
+LOGGER = logging.getLogger('PyAMS (pagelet)')
 
 REDIRECT_STATUS_CODES = (301, 302, 303)
 
 
 @implementer(IPagelet)
-class Pagelet(object):
+class Pagelet:
     """Content provider with layout support"""
 
     template = None
@@ -53,14 +55,20 @@
         request.registry.notify(PageletCreatedEvent(self))
 
     def update(self):
-        self.request.annotations['view'] = self
+        """See `zope.contentprovider.interfaces.IContentProvider`"""
+        annotations = getattr(self.request, 'annotations', None)
+        if annotations is not None:
+            annotations['view'] = self
 
     def render(self):
+        """See `zope.contentprovider.interfaces.IContentProvider`"""
         request = self.request
-        cdict = {'context': self.context,
-                 'request': request,
-                 'view': self,
-                 'translate': queryUtility(IChameleonTranslate)}
+        cdict = {
+            'context': self.context,
+            'request': request,
+            'view': self,
+            'translate': queryUtility(IChameleonTranslate)
+        }
         if self.template is None:
             registry = request.registry
             template = registry.queryMultiAdapter((self, request, self.context),
@@ -68,7 +76,7 @@
             if template is None:
                 template = registry.getMultiAdapter((self, request), IContentTemplate)
             return template(**cdict)
-        return self.template(**cdict)
+        return self.template(**cdict)  # pylint: disable=not-callable
 
     def __call__(self, **kwargs):
         """Call update and return layout template"""
@@ -77,10 +85,12 @@
             return ''
 
         request = self.request
-        cdict = {'context': self.context,
-                 'request': request,
-                 'view': self,
-                 'translate': queryUtility(IChameleonTranslate)}
+        cdict = {
+            'context': self.context,
+            'request': request,
+            'view': self,
+            'translate': queryUtility(IChameleonTranslate)
+        }
         cdict.update(kwargs)
         if self.layout is None:
             registry = request.registry
@@ -89,11 +99,11 @@
             if layout is None:
                 layout = registry.getMultiAdapter((self, request), ILayoutTemplate)
             return Response(layout(**cdict))
-        return Response(self.layout(**cdict))
+        return Response(self.layout(**cdict))  # pylint: disable=not-callable
 
 
 @adapter_config(name='pagelet', context=(Interface, IRequest, IPagelet), provides=IPageletRenderer)
-class PageletRenderer(object):
+class PageletRenderer:
     """Pagelet renderer"""
 
     def __init__(self, context, request, pagelet):
@@ -103,13 +113,14 @@
         self.request = request
 
     def update(self):
-        pass
+        """See `zope.contentprovider.interfaces.IContentProvider`"""
 
     def render(self):
+        """See `zope.contentprovider.interfaces.IContentProvider`"""
         return self.__parent__.render()
 
 
-class pagelet_config(object):
+class pagelet_config:  # pylint: disable=invalid-name
     """Function or class decorator used to declare a pagelet"""
 
     venusian = venusian  # for testing injection
@@ -126,33 +137,34 @@
         settings = self.__dict__.copy()
         depth = settings.pop('_depth', 0)
 
-        def callback(context, name, ob):
+        def callback(context, name, obj):  # pylint: disable=unused-argument
+            """Venusian decorator callback"""
             cdict = {
                 '__name__': settings.get('name'),
-                '__module__': ob.__module__,
+                '__module__': obj.__module__,
                 'permission': settings.get('permission')
             }
-            new_class = type(ob.__name__, (ob, Pagelet), cdict)
+            new_class = type(obj.__name__, (obj, Pagelet), cdict)
 
-            config = context.config.with_package(info.module)
-            logger.debug('Registering pagelet view "{0}" for {1} ({2})'.format(settings.get('name'),
-                                                                               str(settings.get('context', Interface)),
-                                                                               str(new_class)))
-            config.registry.registerAdapter(new_class,
-                                            (settings.get('context', Interface),
-                                             settings.get('request_type', IRequest)),
-                                            IPagelet, settings.get('name'))
+            LOGGER.debug('Registering pagelet view "{0}" for {1} ({2})'.format(
+                settings.get('name'), str(settings.get('context', Interface)), str(new_class)))
+            config = context.config.with_package(info.module)  # pylint: disable=no-member
+            registry = settings.get('registry') or config.registry
+            registry.registerAdapter(new_class,
+                                     (settings.get('context', Interface),
+                                      settings.get('request_type', IRequest)),
+                                     IPagelet, settings.get('name'))
             config.add_view(view=new_class, **settings)
 
         info = self.venusian.attach(wrapped, callback, category='pyams_pagelet',
                                     depth=depth + 1)
 
-        if info.scope == 'class':
+        if info.scope == 'class':  # pylint: disable=no-member
             # if the decorator was attached to a method in a class, or
             # otherwise executed at class scope, we need to set an
             # 'attr' into the settings if one isn't already in there
             if settings.get('attr') is None:
                 settings['attr'] = wrapped.__name__
 
-        settings['_info'] = info.codeinfo  # fbo "action_method"
+        settings['_info'] = info.codeinfo  # pylint: disable=no-member
         return wrapped