src/pyams_utils/zodb.py
branchdev-tf
changeset 427 63284c98cdc1
parent 408 cf2304af0fab
equal deleted inserted replaced
426:2022e4da3ad9 427:63284c98cdc1
    81     """ZEO connection object
    81     """ZEO connection object
    82 
    82 
    83     This object can be used to store all settings to be able to open a ZEO connection.
    83     This object can be used to store all settings to be able to open a ZEO connection.
    84     Note that this class is required only for tasks specifically targeting a ZEO database
    84     Note that this class is required only for tasks specifically targeting a ZEO database
    85     connection (like a ZEO packer scheduler task); for generic ZODB operations, just use a
    85     connection (like a ZEO packer scheduler task); for generic ZODB operations, just use a
    86     :class:`ZODBConnection` class defined through Pyramid's configuration file.
    86     :py:class:`ZODBConnection <pyams_utils.zodb.ZODBConnection>` class defined through Pyramid's
       
    87     configuration file.
    87 
    88 
    88     Note that a ZEO connection object is a context manager, so you can use it like this:
    89     Note that a ZEO connection object is a context manager, so you can use it like this:
    89 
    90 
    90     .. code-block:: python
    91     .. code-block:: python
    91 
    92 
   145         :param boolean get_storage: if *True*, the method should return a tuple containing
   146         :param boolean get_storage: if *True*, the method should return a tuple containing
   146             storage and DB objects; otherwise only DB object is returned
   147             storage and DB objects; otherwise only DB object is returned
   147         :return: tuple containing ZEO client storage and DB object (if *get_storage* argument is
   148         :return: tuple containing ZEO client storage and DB object (if *get_storage* argument is
   148             set to *True*), or only DB object otherwise
   149             set to *True*), or only DB object otherwise
   149         """
   150         """
   150         db = DB((self.server_name, self.server_port),
   151         zdb = DB((self.server_name, self.server_port),
   151                 storage=self.storage,
   152                  storage=self.storage,
   152                 username=self.username or '',
   153                  username=self.username or '',
   153                 password=self.password or '',
   154                  password=self.password or '',
   154                 realm=self.server_realm,
   155                  realm=self.server_realm,
   155                 blob_dir=self.blob_dir,
   156                  blob_dir=self.blob_dir,
   156                 shared_blob_dir=self.shared_blob_dir,
   157                  shared_blob_dir=self.shared_blob_dir,
   157                 wait_timeout=wait_timeout)
   158                  wait_timeout=wait_timeout)
   158         return (db.storage, db) if get_storage else db
   159         return (zdb.storage, zdb) if get_storage else zdb
   159 
   160 
   160     @property
   161     @property
   161     def connection(self):
   162     def connection(self):
   162         """Connection getter"""
   163         """Connection getter"""
   163         return self._connection
   164         return self._connection
   207 def get_connection_from_settings(settings=None):
   208 def get_connection_from_settings(settings=None):
   208     """Load connection matching registry settings"""
   209     """Load connection matching registry settings"""
   209     if settings is None:
   210     if settings is None:
   210         settings = get_global_registry().settings  # pylint: disable=no-member
   211         settings = get_global_registry().settings  # pylint: disable=no-member
   211     for name, uri in get_uris(settings):
   212     for name, uri in get_uris(settings):
   212         db = db_from_uri(uri, name, {})
   213         zdb = db_from_uri(uri, name, {})
   213         return db.open()
   214         return zdb.open()
   214 
   215 
   215 
   216 
   216 class ZODBConnection:
   217 class ZODBConnection:
   217     """ZODB connection wrapper
   218     """ZODB connection wrapper
   218 
   219 
   246     def connection(self):
   247     def connection(self):
   247         """Connection getter"""
   248         """Connection getter"""
   248         return self._connection
   249         return self._connection
   249 
   250 
   250     @property
   251     @property
   251     def db(self):
   252     def db(self):  # pylint: disable=invalid-name
   252         """Database getter"""
   253         """Database getter"""
   253         return self._db
   254         return self._db
   254 
   255 
   255     @property
   256     @property
   256     def storage(self):
   257     def storage(self):
   259 
   260 
   260     def get_connection(self):
   261     def get_connection(self):
   261         """Load named connection matching registry settings"""
   262         """Load named connection matching registry settings"""
   262         for name, uri in get_uris(self.settings):
   263         for name, uri in get_uris(self.settings):
   263             if name == self.name:
   264             if name == self.name:
   264                 db = db_from_uri(uri, name, {})
   265                 zdb = db_from_uri(uri, name, {})
   265                 connection = self._connection = db.open()
   266                 connection = self._connection = zdb.open()
   266                 self._db = connection.db()
   267                 self._db = connection.db()
   267                 self._storage = self.db.storage
   268                 self._storage = self.db.storage
   268                 return connection
   269                 return connection
   269         return None
   270         return None
   270 
   271