src/pyams_utils/zodb.py
branchdev-tf
changeset 408 cf2304af0fab
parent 292 b338586588ad
child 419 05ff71a02b2d
--- a/src/pyams_utils/zodb.py	Wed Nov 20 19:26:23 2019 +0100
+++ b/src/pyams_utils/zodb.py	Fri Nov 22 18:51:37 2019 +0100
@@ -10,33 +10,34 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+""""PyAMS_utils.zodb module
 
+This modules provides several utilities used to manage ZODB connections and persistent objects
+"""
 
-# import standard library
+from ZEO import DB
+from ZODB.interfaces import IConnection
+from persistent import Persistent
+from persistent.interfaces import IPersistent
+from pyramid.events import subscriber
+from pyramid_zodbconn import db_from_uri, get_uris
+from transaction.interfaces import ITransactionManager
+from zope.annotation.interfaces import IAttributeAnnotatable
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
+from zope.schema import getFieldNames
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
-# import interfaces
-from persistent.interfaces import IPersistent
+from pyams_utils.adapter import adapter_config
 from pyams_utils.interfaces.site import IOptionalUtility
 from pyams_utils.interfaces.zeo import IZEOConnection
-from transaction.interfaces import ITransactionManager
-from ZODB.interfaces import IConnection
-from zope.annotation.interfaces import IAttributeAnnotatable
-from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
+from pyams_utils.registry import get_global_registry, get_utilities_for
+from pyams_utils.vocabulary import vocabulary_config
 
-# import packages
-from persistent import Persistent
-from pyams_utils.adapter import adapter_config
-from pyams_utils.registry import get_utilities_for, get_global_registry
-from pyams_utils.vocabulary import vocabulary_config
-from pyramid.events import subscriber
-from pyramid_zodbconn import get_uris, db_from_uri
-from ZEO import DB
-from zope.container.contained import Contained
-from zope.interface import implementer
-from zope.schema import getFieldNames
-from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+__docformat__ = 'restructuredtext'
 
 
 @adapter_config(context=IPersistent, provides=IConnection)
@@ -53,19 +54,20 @@
         cur = getattr(cur, '__parent__', None)
         if cur is None:
             return None
-    return cur._p_jar
+    return cur._p_jar  # pylint: disable=protected-access
 
 
 # IPersistent adapters copied from zc.twist package
 # also register this for adapting from IConnection
 @adapter_config(context=IPersistent, provides=ITransactionManager)
 def persistent_transaction_manager(obj):
+    """Transaction manager adapter for persistent objects"""
     conn = IConnection(obj)  # typically this will be
                              # zope.keyreference.persistent.connectionOfPersistent
     try:
         return conn.transaction_manager
     except AttributeError:
-        return conn._txn_mgr
+        return conn._txn_mgr  # pylint: disable=protected-access
         # or else we give up; who knows.  transaction_manager is the more
         # recent spelling.
 
@@ -75,13 +77,13 @@
 #
 
 @implementer(IZEOConnection)
-class ZEOConnection(object):
+class ZEOConnection:
     """ZEO connection object
 
     This object can be used to store all settings to be able to open a ZEO connection.
-    Note that this class is required only for tasks specifically targeting a ZEO database connection (like a ZEO
-    packer scheduler task); for generic ZODB operations, just use a :class:`ZODBConnection` class defined through
-    Pyramid's configuration file.
+    Note that this class is required only for tasks specifically targeting a ZEO database
+    connection (like a ZEO packer scheduler task); for generic ZODB operations, just use a
+    :class:`ZODBConnection` class defined through Pyramid's configuration file.
 
     Note that a ZEO connection object is a context manager, so you can use it like this:
 
@@ -157,6 +159,7 @@
 
     @property
     def connection(self):
+        """Connection getter"""
         return self._connection
 
     # Context manager methods
@@ -195,21 +198,22 @@
 class ZEOConnectionVocabulary(SimpleVocabulary):
     """ZEO connections vocabulary"""
 
-    def __init__(self, context=None):
-        terms = [SimpleTerm(name, title=util.name) for name, util in get_utilities_for(IZEOConnection)]
+    def __init__(self, context=None):  # pylint: disable=unused-argument
+        terms = [SimpleTerm(name, title=util.name)
+                 for name, util in get_utilities_for(IZEOConnection)]
         super(ZEOConnectionVocabulary, self).__init__(terms)
 
 
 def get_connection_from_settings(settings=None):
     """Load connection matching registry settings"""
     if settings is None:
-        settings = get_global_registry().settings
+        settings = get_global_registry().settings  # pylint: disable=no-member
     for name, uri in get_uris(settings):
         db = db_from_uri(uri, name, {})
         return db.open()
 
 
-class ZODBConnection(object):
+class ZODBConnection:
     """ZODB connection wrapper
 
     Connections are extracted from Pyramid's settings file in *zodbconn.uri* entries.
@@ -231,7 +235,7 @@
     def __init__(self, name='', settings=None):
         self.name = name or ''
         if not settings:
-            settings = get_global_registry().settings
+            settings = get_global_registry().settings  # pylint: disable=no-member
         self.settings = settings
 
     _connection = None
@@ -240,14 +244,17 @@
 
     @property
     def connection(self):
+        """Connection getter"""
         return self._connection
 
     @property
     def db(self):
+        """Database getter"""
         return self._db
 
     @property
     def storage(self):
+        """Storage getter"""
         return self._storage
 
     def get_connection(self):
@@ -259,8 +266,10 @@
                 self._db = connection.db()
                 self._storage = self.db.storage
                 return connection
+        return None
 
     def close(self):
+        """Connection close"""
         self._connection.close()
         self._db.close()
         self._storage.close()
@@ -278,16 +287,16 @@
 class ZODBConnectionVocabulary(SimpleVocabulary):
     """ZODB connections vocabulary"""
 
-    def __init__(self, context=None):
-        settings = get_global_registry().settings
+    def __init__(self, context=None):  # pylint: disable=unused-argument
+        settings = get_global_registry().settings  # pylint: disable=no-member
         terms = [SimpleTerm(name, title=name) for name, uri in get_uris(settings)]
         super(ZODBConnectionVocabulary, self).__init__(terms)
 
 
-volatile_marker = object()
+VOLATILE_MARKER = object()
 
 
-class volatile_property:
+class volatile_property:  # pylint: disable=invalid-name
     """Property decorator to define volatile attributes into persistent classes"""
 
     def __init__(self, fget, doc=None):
@@ -300,8 +309,8 @@
         if inst is None:
             return self
         attrname = '_v_{0}'.format(self.__name__)
-        value = getattr(inst, attrname, volatile_marker)
-        if value is volatile_marker:
+        value = getattr(inst, attrname, VOLATILE_MARKER)
+        if value is VOLATILE_MARKER:
             value = self.fget(inst)
             setattr(inst, attrname, value)
         return value