--- a/src/pyams_utils/request.py Wed Nov 20 19:26:23 2019 +0100
+++ b/src/pyams_utils/request.py Fri Nov 22 18:51:37 2019 +0100
@@ -10,7 +10,10 @@
# FOR A PARTICULAR PURPOSE.
#
-__docformat__ = 'restructuredtext'
+"""PyAMS_utils.request module
+
+
+"""
import logging
@@ -24,13 +27,15 @@
from pyams_utils.interfaces import ICacheKeyValue, MissingRequestError
from pyams_utils.registry import get_global_registry
-
-logger = logging.getLogger('PyAMS (utils)')
-
-_marker = object()
+__docformat__ = 'restructuredtext'
-class RequestSelector(object):
+LOGGER = logging.getLogger('PyAMS (utils)')
+
+_MARKER = object()
+
+
+class RequestSelector:
"""Interface based request selector
This selector can be used as a subscriber predicate to define
@@ -45,12 +50,13 @@
'''This is an event handler for an IPyAMSRequest modification event'''
"""
- def __init__(self, ifaces, config):
+ def __init__(self, ifaces, config): # pylint: disable=unused-argument
if not isinstance(ifaces, (list, tuple, set)):
ifaces = (ifaces,)
self.interfaces = ifaces
def text(self):
+ """Predicate label"""
return 'request_selector = %s' % str(self.interfaces)
phash = text
@@ -72,9 +78,10 @@
If no request is currently running, a new one is created.
`key` is a required argument; if None, the key will be the method's object
- :param str key: annotations value key; if *None*, the key will be the method's object; if *key* is a callable
- object, it will be called to get the actual session key
- :param prefix: str; prefix to use for session key; if *None*, the prefix will be the property name
+ :param str key: annotations value key; if *None*, the key will be the method's object; if
+ *key* is a callable object, it will be called to get the actual session key
+ :param prefix: str; prefix to use for session key; if *None*, the prefix will be the property
+ name
"""
def request_decorator(func):
@@ -93,18 +100,19 @@
key += '::' + '::'.join((ICacheKeyValue(arg) for arg in key_args))
if kwargs:
key += '::' + \
- '::'.join(('{0}={1}'.format(key, ICacheKeyValue(val)) for key, val in kwargs.items()))
- logger.debug(">>> Looking for request cache key {0}".format(key))
- data = get_request_data(request, key, _marker)
- if data is _marker:
- logger.debug("<<< no cached value!")
+ '::'.join(('{0}={1}'.format(key, ICacheKeyValue(val))
+ for key, val in kwargs.items()))
+ LOGGER.debug(">>> Looking for request cache key {0}".format(key))
+ data = get_request_data(request, key, _MARKER)
+ if data is _MARKER:
+ LOGGER.debug("<<< no cached value!")
data = func
if callable(data):
data = data(obj, *args, **kwargs)
set_request_data(request, key, data)
else:
- logger.debug("<<< cached value found!")
- logger.debug(" < {0!r}".format(data))
+ LOGGER.debug("<<< cached value found!")
+ LOGGER.debug(" < {0!r}".format(data))
else:
data = func
if callable(data):
@@ -126,7 +134,7 @@
@request_property(key=None)
def has_permission(self, permission, context=None):
if context is None:
- context = self.context
+ context = self.context # pylint: disable=no-member
try:
reg = self.registry
except AttributeError:
@@ -163,6 +171,7 @@
return None
+# pylint: disable=invalid-name,too-many-arguments
def check_request(path='/', environ=None, base_url=None, headers=None,
POST=None, registry=None, principal_id=None, **kwargs):
"""Get current request, or create a new blank one if missing"""
@@ -177,13 +186,15 @@
if factory is None:
factory = PyAMSRequest
request = factory.blank(path, environ, base_url, headers, POST, **kwargs)
- request.registry = registry
+ request.registry = registry # pylint: disable=attribute-defined-outside-init
if principal_id is not None:
try:
+ # pylint: disable=import-outside-toplevel
from pyams_security.utility import get_principal
except ImportError:
pass
else:
+ # pylint: disable=attribute-defined-outside-init
request.principal = get_principal(request, principal_id)
return request
@@ -210,12 +221,13 @@
return IAnnotations(request)
-def get_debug(request):
+def get_debug(request): # pylint: disable=unused-argument
"""Define 'debug' request property
This function is automatically defined as a custom request method on package include.
"""
class Debug():
+ """Request debug class"""
def __init__(self):
self.showTAL = False
self.sourceAnnotations = False
@@ -232,7 +244,7 @@
"""
try:
annotations = request.annotations
- except (TypeError, AttributeError) as e:
+ except (TypeError, AttributeError):
annotations = get_annotations(request)
return annotations.get(key, default)
@@ -246,6 +258,6 @@
"""
try:
annotations = request.annotations
- except (TypeError, AttributeError) as e:
+ except (TypeError, AttributeError):
annotations = get_annotations(request)
annotations[key] = value