src/ztfy/utils/zodb.py
branchZTK-1.1
changeset 169 b4b587dd45ca
parent 148 d3668ecd9137
child 170 a08e9a1c5b07
equal deleted inserted replaced
168:c02d355d3ffd 169:b4b587dd45ca
    14 ##############################################################################
    14 ##############################################################################
    15 
    15 
    16 __docformat__ = "restructuredtext"
    16 __docformat__ = "restructuredtext"
    17 
    17 
    18 # import standard packages
    18 # import standard packages
       
    19 from persistent import Persistent
    19 
    20 
    20 # import Zope3 interfaces
    21 # import Zope3 interfaces
    21 from persistent.interfaces import IPersistent
    22 from persistent.interfaces import IPersistent
    22 from transaction.interfaces import ITransactionManager
    23 from transaction.interfaces import ITransactionManager
    23 from ZODB.interfaces import IConnection
    24 from ZODB.interfaces import IConnection
    24 
    25 
    25 # import local interfaces
    26 # import local interfaces
       
    27 from ztfy.utils.interfaces import IZEOConnection
    26 
    28 
    27 # import Zope3 packages
    29 # import Zope3 packages
       
    30 from ZEO import ClientStorage
       
    31 from ZODB import DB
    28 from zope.component import adapter
    32 from zope.component import adapter
    29 from zope.interface import implementer
    33 from zope.container.contained import Contained
       
    34 from zope.interface import implementer, implements
       
    35 from zope.schema.fieldproperty import FieldProperty
    30 
    36 
    31 # import local packages
    37 # import local packages
       
    38 
       
    39 
       
    40 class ZEOConnectionInfo(object):
       
    41     """ZEO connection info"""
       
    42 
       
    43     implements(IZEOConnection)
       
    44 
       
    45     server_name = FieldProperty(IZEOConnection['server_name'])
       
    46     server_port = FieldProperty(IZEOConnection['server_port'])
       
    47     storage = FieldProperty(IZEOConnection['storage'])
       
    48     username = FieldProperty(IZEOConnection['username'])
       
    49     password = FieldProperty(IZEOConnection['password'])
       
    50     server_realm = FieldProperty(IZEOConnection['server_realm'])
       
    51 
       
    52     def getConnection(self, wait=False, get_storage=False):
       
    53         """Get a tuple made of storage and DB connection for given settings"""
       
    54         storage = ClientStorage.ClientStorage((str(self.server_name), self.server_port),
       
    55                                               storage=self.server_storage,
       
    56                                               username=self.server_username or '',
       
    57                                               password=self.server_password or '',
       
    58                                               realm=self.server_realm,
       
    59                                               wait=wait)
       
    60         db = DB(storage)
       
    61         return (storage, db) if get_storage else db
       
    62 
       
    63 
       
    64 class ZEOConnectionUtility(ZEOConnectionInfo, Persistent, Contained):
       
    65     """Persistent ZEO connection settings utility"""
       
    66 
    32 
    67 
    33 
    68 
    34 # IPersistent adapters copied from zc.twist package
    69 # IPersistent adapters copied from zc.twist package
    35 # also register this for adapting from IConnection
    70 # also register this for adapting from IConnection
    36 @adapter(IPersistent)
    71 @adapter(IPersistent)