1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
=======================
Persistent Registries
=======================
.. testsetup::
from zope.component.testing import setUp
setUp()
.. automodule:: zope.component.persistentregistry
Conforming Adapter Lookup
=========================
Here, we'll demonstrate that changes work even when data are stored in
a database and when accessed from multiple connections.
Start by setting up a database and creating two transaction
managers and database connections to work with.
.. doctest::
>>> import ZODB.MappingStorage
>>> db = ZODB.MappingStorage.DB()
>>> import transaction
>>> t1 = transaction.TransactionManager()
>>> c1 = db.open(transaction_manager=t1)
>>> r1 = c1.root()
>>> t2 = transaction.TransactionManager()
>>> c2 = db.open(transaction_manager=t2)
>>> r2 = c2.root()
Create a set of components registries in the database, alternating
connections.
.. doctest::
>>> from zope.component.persistentregistry import PersistentComponents
>>> from zope.component.tests.examples import I1
>>> from zope.component.tests.examples import I2
>>> from zope.component.tests.examples import U
>>> from zope.component.tests.examples import U1
>>> from zope.component.tests.examples import U12
>>> from zope.component.tests.examples import handle1
>>> from zope.component.tests.examples import handle2
>>> from zope.component.tests.examples import handle3
>>> from zope.component.tests.examples import handle4
>>> _ = t1.begin()
>>> r1[1] = PersistentComponents('1')
>>> t1.commit()
>>> _ = t2.begin()
>>> r2[2] = PersistentComponents('2', (r2[1], ))
>>> t2.commit()
>>> _ = t1.begin()
>>> r1[3] = PersistentComponents('3', (r1[1], ))
>>> t1.commit()
>>> _ = t2.begin()
>>> r2[4] = PersistentComponents('4', (r2[2], r2[3]))
>>> t2.commit()
>>> _ = t1.begin()
>>> r1[1].__bases__
()
>>> r1[2].__bases__ == (r1[1], )
True
>>> r1[1].registerUtility(U1(1))
>>> r1[1].queryUtility(I1)
U1(1)
>>> r1[2].queryUtility(I1)
U1(1)
>>> t1.commit()
>>> _ = t2.begin()
>>> r2[1].registerUtility(U1(2))
>>> r2[2].queryUtility(I1)
U1(2)
>>> r2[4].queryUtility(I1)
U1(2)
>>> t2.commit()
>>> _ = t1.begin()
>>> r1[1].registerUtility(U12(1), I2)
>>> r1[4].queryUtility(I2)
U12(1)
>>> t1.commit()
>>> _ = t2.begin()
>>> r2[3].registerUtility(U12(3), I2)
>>> r2[4].queryUtility(I2)
U12(3)
>>> t2.commit()
>>> _ = t1.begin()
>>> r1[1].registerHandler(handle1, info="First handler")
>>> r1[2].registerHandler(handle2, required=[U])
>>> r1[3].registerHandler(handle3)
>>> r1[4].registerHandler(handle4)
>>> r1[4].handle(U1(1))
handle1 U1(1)
handle3 U1(1)
handle2 (U1(1),)
handle4 U1(1)
>>> t1.commit()
>>> _ = t2.begin()
>>> r2[4].handle(U1(1))
handle1 U1(1)
handle3 U1(1)
handle2 (U1(1),)
handle4 U1(1)
>>> t2.abort()
>>> db.close()
Subscription to Events in Persistent Registries
===============================================
.. doctest::
>>> import ZODB.MappingStorage
>>> db = ZODB.MappingStorage.DB()
>>> import transaction
>>> t1 = transaction.TransactionManager()
>>> c1 = db.open(transaction_manager=t1)
>>> r1 = c1.root()
>>> t2 = transaction.TransactionManager()
>>> c2 = db.open(transaction_manager=t2)
>>> r2 = c2.root()
>>> from zope.component.persistentregistry import PersistentComponents
>>> _ = t1.begin()
>>> r1[1] = PersistentComponents('1')
>>> r1[1].registerHandler(handle1)
>>> r1[1].registerSubscriptionAdapter(handle1, provided=I2)
>>> _ = r1[1].unregisterHandler(handle1)
>>> _ = r1[1].unregisterSubscriptionAdapter(handle1, provided=I2)
>>> t1.commit()
>>> _ = t1.begin()
>>> r1[1].registerHandler(handle1)
>>> r1[1].registerSubscriptionAdapter(handle1, provided=I2)
>>> t1.commit()
>>> _ = t2.begin()
>>> len(list(r2[1].registeredHandlers()))
1
>>> len(list(r2[1].registeredSubscriptionAdapters()))
1
>>> t2.abort()
Adapter Registrations after Serialization / Deserialization
===========================================================
We want to make sure that we see updates corrextly.
.. doctest::
>>> import persistent
>>> import transaction
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> class IFoo(Interface):
... pass
>>> @implementer(IFoo)
... class Foo(persistent.Persistent):
... name = ''
... def __init__(self, name=''):
... self.name = name
... def __repr__(self):
... return 'Foo(%r)' % self.name
>>> from zope.component.tests.examples import base
>>> from zope.component.tests.examples import clear_base
>>> len(base._v_subregistries)
0
>>> import ZODB.MappingStorage
>>> db = ZODB.MappingStorage.DB()
>>> tm1 = transaction.TransactionManager()
>>> c1 = db.open(transaction_manager=tm1)
>>> from zope.component.persistentregistry import PersistentAdapterRegistry
>>> r1 = PersistentAdapterRegistry((base,))
>>> r2 = PersistentAdapterRegistry((r1,))
>>> c1.root()[1] = r1
>>> c1.root()[2] = r2
>>> tm1.commit()
>>> r1._p_deactivate()
>>> len(base._v_subregistries)
0
>>> tm2 = transaction.TransactionManager()
>>> c2 = db.open(transaction_manager=tm2)
>>> r1 = c2.root()[1]
>>> r2 = c2.root()[2]
>>> r1.lookup((), IFoo, '')
>>> base.register((), IFoo, '', Foo(''))
>>> r1.lookup((), IFoo, '')
Foo('')
>>> r2.lookup((), IFoo, '1')
>>> r1.register((), IFoo, '1', Foo('1'))
>>> r2.lookup((), IFoo, '1')
Foo('1')
>>> r1.lookup((), IFoo, '2')
>>> r2.lookup((), IFoo, '2')
>>> base.register((), IFoo, '2', Foo('2'))
>>> r1.lookup((), IFoo, '2')
Foo('2')
>>> r2.lookup((), IFoo, '2')
Foo('2')
>>> db.close()
>>> clear_base()
.. testcleanup::
from zope.component.testing import tearDown
tearDown()
|