zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
U
¸ý°d%Íã    @sÊUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm    Z    ddlm
Z
dd    lm Z dd
lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlZddlmZddlmZddlmZddlmZddlmZddl m!Z!ejrŠddl"m#Z#ddl"m$Z$ddl%m&a&ddl%m'a'ddl%m(a(dd l%m)a)dd!l*m+Z+d"d#d$d%d&d%d&d'd(g    Z,e -¡Z.egd)fZ/ed*ed+Z0ed,ed+Z1ed-ed+Z2ed.d/d+Z3ed0d1d+Z4Gd2d3„d3e!ƒZ5Gd4d)„d)e!ƒZ6Gd5d"„d"ƒZ7er6d/d6d7œd8d#„Z8n
e 9d9¡Z8Gd:d6„d6ƒZ:dxd;d<„Z;d=d>d?œd@dA„Z<dBdC„Z=dDdE„Z>dFdG„Z?dHdI„Z@dJdK„ZAdLdM„ZBdydNdO„ZCdPdQ„ZDdRdS„ZEdzdTdU„ZFdVdWœdXdY„ZGdVdWœdZd[„ZHeIeJfZKd\d\d]d^œd_d`„ZLd\d\d]d^œdadb„ZMdVdWœdcdd„ZNGdedf„dfe e0ƒZOGdgdh„dhee0ƒZPGdidj„dje
e1e2fƒZQe ReSeOeIePeTeQi¡ZUdkeVdl<e ReSdmdndodpœeGƒfeIdqdndodpœeNƒfeTdrdsieHƒfi¡ZWdteVdu<dvdw„ZXeXeYƒƒdS){aFSupport for collections of mapped entities.
 
The collections package supplies the machinery used to inform the ORM of
collection membership changes.  An instrumentation via decoration approach is
used, allowing arbitrary types (including built-ins) to be used as entity
collections without requiring inheritance from a base class.
 
Instrumentation decoration relays membership change events to the
:class:`.CollectionAttributeImpl` that is currently managing the collection.
The decorators observe function call arguments and return values, tracking
entities entering or leaving the collection.  Two decorator approaches are
provided.  One is a bundle of generic decorators that map function arguments
and return values to events::
 
  from sqlalchemy.orm.collections import collection
  class MyClass:
      # ...
 
      @collection.adds(1)
      def store(self, item):
          self.data.append(item)
 
      @collection.removes_return()
      def pop(self):
          return self.data.pop()
 
 
The second approach is a bundle of targeted decorators that wrap appropriate
append and remove notifiers around the mutation methods present in the
standard Python ``list``, ``set`` and ``dict`` interfaces.  These could be
specified in terms of generic decorator recipes, but are instead hand-tooled
for increased efficiency.  The targeted decorators occasionally implement
adapter-like behavior, such as mapping bulk-set methods (``extend``,
``update``, ``__setslice__``, etc.) into the series of atomic mutation events
that the ORM requires.
 
The targeted decorators are used internally for automatic instrumentation of
entity collection classes.  Every collection class goes through a
transformation process roughly like so:
 
1. If the class is a built-in, substitute a trivial sub-class
2. Is this class already instrumented?
3. Add in generic decorators
4. Sniff out the collection interface through duck-typing
5. Add targeted decoration to any undecorated interface method
 
This process modifies the class at runtime, decorating methods and adding some
bookkeeping properties.  This isn't possible (or desirable) for built-in
classes like ``list``, so trivial sub-classes are substituted to hold
decoration::
 
  class InstrumentedList(list):
      pass
 
Collection classes can be specified in ``relationship(collection_class=)`` as
types or a function that returns an instance.  Collection classes are
inspected and instrumented during the mapper compilation phase.  The
collection_class callable will be executed once to produce a specimen
instance, and the type of that specimen will be instrumented.  Functions that
return built-in types like ``lists`` will be adapted to produce instrumented
instances.
 
When extending a known type like ``list``, additional decorations are not
generally not needed.  Odds are, the extension method will delegate to a
method that's already instrumented.  For example::
 
  class QueueIsh(list):
     def push(self, item):
         self.append(item)
     def shift(self):
         return self.pop(0)
 
There's no need to decorate these methods.  ``append`` and ``pop`` are already
instrumented as part of the ``list`` interface.  Decorating them would fire
duplicate events, which should be avoided.
 
The targeted decoration tries not to rely on other methods in the underlying
collection class, but some are unavoidable.  Many depend on 'read' methods
being present to properly instrument a 'write', for example, ``__setitem__``
needs ``__getitem__``.  "Bulk" methods like ``update`` and ``extend`` may also
reimplemented in terms of atomic appends and removes, so the ``extend``
decoration will actually perform many ``append`` operations and not call the
underlying method at all.
 
Tight control over bulk operation and the firing of events is also possible by
implementing the instrumentation internally in your methods.  The basic
instrumentation package works under the general assumption that collection
mutation will not raise unusual exceptions.  If you want to closely
orchestrate append and remove events with exception management, internal
instrumentation may be the answer.  Within your method,
``collection_adapter(self)`` will retrieve an object that you can use for
explicit control over triggering append and remove events.
 
The owning object and :class:`.CollectionAttributeImpl` are also reachable
through the adapter, allowing for some very sophisticated behavior.
 
é)Ú annotationsN)ÚAny)ÚCallable)Úcast)Ú
Collection)ÚDict)ÚIterable)ÚList)ÚNoReturn)ÚOptional)ÚSet)ÚTuple)ÚType)Ú TYPE_CHECKING)ÚTypeVar)ÚUnioné)ÚNO_KEYé)Úexc)Úutil©ÚNO_ARG)Úinspect_getfullargspec)ÚProtocol)ÚAttributeEventToken)ÚCollectionAttributeImpl©Úattribute_keyed_dict©Úcolumn_keyed_dict©Úkeyfunc_mapping©Ú KeyFuncDict)Ú InstanceStateÚ
collectionÚcollection_adapterr"r rÚMappedCollectionr$Ú_AdaptedCollectionProtocolÚ_T)ÚboundÚ_KTÚ_VTÚ_COLzCollection[Any]Ú_FNúCallable[..., Any]c@seZdZdddœdd„ZdS)Ú_CollectionConverterProtocolr.©r&ÚreturncCsdS©N©)Úselfr&r5r5úQd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/orm/collections.pyÚ__call__ªsz%_CollectionConverterProtocol.__call__N)Ú__name__Ú
__module__Ú __qualname__r8r5r5r5r7r1©sr1c@s6eZdZUded<ded<ded<ded<ded    <d
S) r)ÚCollectionAdapterÚ _sa_adapterr0Ú _sa_appenderÚ _sa_removerzCallable[..., Iterable[Any]]Ú _sa_iteratorr1Ú _sa_converterN)r9r:r;Ú__annotations__r5r5r5r7r)®s
 
c@sˆeZdZdZedd„ƒZedd„ƒZedd„ƒZedd    „ƒZee     
d
d ¡d d „ƒƒZ edd„ƒZ edd„ƒZ edd„ƒZedd„ƒZdS)r&auDecorators for entity collection classes.
 
    The decorators fall into two groups: annotations and interception recipes.
 
    The annotating decorators (appender, remover, iterator, converter,
    internally_instrumented) indicate the method's purpose and take no
    arguments.  They are not written with parens::
 
        @collection.appender
        def append(self, append): ...
 
    The recipe decorators all require parens, even those that take no
    arguments::
 
        @collection.adds('entity')
        def insert(self, position, entity): ...
 
        @collection.removes_return()
        def popitem(self): ...
 
    cCs
d|_|S)aTag the method as the collection appender.
 
        The appender method is called with one positional argument: the value
        to append. The method will be automatically decorated with 'adds(1)'
        if not already decorated::
 
            @collection.appender
            def add(self, append): ...
 
            # or, equivalently
            @collection.appender
            @collection.adds(1)
            def add(self, append): ...
 
            # for mapping type, an 'append' may kick out a previous value
            # that occupies that slot.  consider d['a'] = 'foo'- any previous
            # value in d['a'] is discarded.
            @collection.appender
            @collection.replaces(1)
            def add(self, entity):
                key = some_key_func(entity)
                previous = None
                if key in self:
                    previous = self[key]
                self[key] = entity
                return previous
 
        If the value to append is not allowed in the collection, you may
        raise an exception.  Something to remember is that the appender
        will be called for each object mapped by a database query.  If the
        database contains rows that violate your collection semantics, you
        will need to get creative to fix the problem, as access via the
        collection will not work.
 
        If the appender method is internally instrumented, you must also
        receive the keyword argument '_sa_initiator' and ensure its
        promulgation to collection events.
 
        Úappender©Ú_sa_instrument_role©Úfnr5r5r7rCÐs)zcollection.appendercCs
d|_|S)aTag the method as the collection remover.
 
        The remover method is called with one positional argument: the value
        to remove. The method will be automatically decorated with
        :meth:`removes_return` if not already decorated::
 
            @collection.remover
            def zap(self, entity): ...
 
            # or, equivalently
            @collection.remover
            @collection.removes_return()
            def zap(self, ): ...
 
        If the value to remove is not present in the collection, you may
        raise an exception or return None to ignore the error.
 
        If the remove method is internally instrumented, you must also
        receive the keyword argument '_sa_initiator' and ensure its
        promulgation to collection events.
 
        ÚremoverrDrFr5r5r7rHüszcollection.removercCs
d|_|S)zÿTag the method as the collection remover.
 
        The iterator method is called with no arguments.  It is expected to
        return an iterator over all collection members::
 
            @collection.iterator
            def __iter__(self): ...
 
        ÚiteratorrDrFr5r5r7rIs zcollection.iteratorcCs
d|_|S)aÈTag the method as instrumented.
 
        This tag will prevent any decoration from being applied to the
        method. Use this if you are orchestrating your own calls to
        :func:`.collection_adapter` in one of the basic SQLAlchemy
        interface methods, or to prevent an automatic ABC method
        decoration from wrapping your implementation::
 
            # normally an 'extend' method on a list-like class would be
            # automatically intercepted and re-implemented in terms of
            # SQLAlchemy events and append().  your implementation will
            # never be called, unless:
            @collection.internally_instrumented
            def extend(self, items): ...
 
        T)Ú_sa_instrumentedrFr5r5r7Úinternally_instrumented%sz"collection.internally_instrumentedz1.3zçThe :meth:`.collection.converter` handler is deprecated and will be removed in a future release.  Please refer to the :class:`.AttributeEvents.bulk_replace` listener interface in conjunction with the :func:`.event.listen` function.cCs
d|_|S)a¡Tag the method as the collection converter.
 
        This optional method will be called when a collection is being
        replaced entirely, as in::
 
            myobj.acollection = [newvalue1, newvalue2]
 
        The converter method will receive the object being assigned and should
        return an iterable of values suitable for use by the ``appender``
        method.  A converter must not assign values or mutate the collection,
        its sole job is to adapt the value the user provides into an iterable
        of values for the ORM's use.
 
        The default converter implementation will use duck-typing to do the
        conversion.  A dict-like collection will be convert into an iterable
        of dictionary values, and other types will simply be iterated::
 
            @collection.converter
            def convert(self, other): ...
 
        If the duck-typing of the object does not match the type of this
        collection, a TypeError is raised.
 
        Supply an implementation of this method if you want to expand the
        range of possible types that can be assigned in bulk or perform
        validation on the values about to be assigned.
 
        Ú    converterrDrFr5r5r7rL:s%zcollection.convertercs‡fdd„}|S)aÛMark the method as adding an entity to the collection.
 
        Adds "add to collection" handling to the method.  The decorator
        argument indicates which method argument holds the SQLAlchemy-relevant
        value.  Arguments can be specified positionally (i.e. integer) or by
        name::
 
            @collection.adds(1)
            def push(self, item): ...
 
            @collection.adds('entity')
            def do_stuff(self, thing, entity=None): ...
 
        csdˆf|_|S)NÚfire_append_event©Ú_sa_instrument_beforerF©Úargr5r7Ú    decoratorss
z"collection.adds.<locals>.decoratorr5©rQrRr5rPr7Úaddsbs zcollection.addscs‡fdd„}|S)aMark the method as replacing an entity in the collection.
 
        Adds "add to collection" and "remove from collection" handling to
        the method.  The decorator argument indicates which method argument
        holds the SQLAlchemy-relevant value to be added, and return value, if
        any will be considered the value to remove.
 
        Arguments can be specified positionally (i.e. integer) or by name::
 
            @collection.replaces(2)
            def __setitem__(self, index, item): ...
 
        csdˆf|_d|_|S)NrMÚfire_remove_event)rOÚ_sa_instrument_afterrFrPr5r7rR‰s
z&collection.replaces.<locals>.decoratorr5rSr5rPr7Úreplacesys zcollection.replacescs‡fdd„}|S)aMark the method as removing an entity in the collection.
 
        Adds "remove from collection" handling to the method.  The decorator
        argument indicates which method argument holds the SQLAlchemy-relevant
        value to be removed. Arguments can be specified positionally (i.e.
        integer) or by name::
 
            @collection.removes(1)
            def zap(self, item): ...
 
        For methods where the value to remove is not known at call-time, use
        collection.removes_return.
 
        csdˆf|_|S©NrUrNrFrPr5r7rR¡s
z%collection.removes.<locals>.decoratorr5rSr5rPr7Úremovess zcollection.removescCs dd„}|S)aµMark the method as removing an entity in the collection.
 
        Adds "remove from collection" handling to the method.  The return
        value of the method, if any, is considered the value to remove.  The
        method arguments are not inspected::
 
            @collection.removes_return()
            def pop(self): ...
 
        For methods where the value to remove is known at call-time, use
        collection.remove.
 
        cSs
d|_|SrX)rVrFr5r5r7rR·sz,collection.removes_return.<locals>.decoratorr5)rRr5r5r7Úremoves_return§szcollection.removes_returnN)r9r:r;Ú__doc__Ú staticmethodrCrHrIrKrÚ
deprecatedrLrTrWrYrZr5r5r5r7r&¶s.
+
 
 
þ  
 
 
r<r2cCsdS)z7Fetch the :class:`.CollectionAdapter` for a collection.Nr5)r&r5r5r7r'Àsr=c@sžeZdZUdZdZded<ded<ded<d    ed
<d ed <d ed<d ed<dd    ddœdd„Zddœdd„Zeddœdd„ƒZ    ed dœdd„ƒZ
dd„Z dOdd dd!œd"d#„Z d$d%„Z ddœd&d'„Zd(dœd)d*„Zddd+œd,d-„Zd.dd/œd0d1„Zd2d3„ZdPdd dd!œd4d5„Zddd+œd6d7„ZdQd dd8œd9d:„Zddœd;d<„Zd=d>„Zd?d@„ZdAdB„ZdefdCdD„ZdefdEdF„ZdefdGdH„ZdefdIdJ„ZdKdL„ZdMdN„Z dS)Rr<aiBridges between the ORM and arbitrary Python collections.
 
    Proxies base-level collection operations (append, remove, iterate)
    to the underlying Python collection, and emits add/remove events for
    entities entering or leaving the collection.
 
    The ORM uses :class:`.CollectionAdapter` exclusively for interaction with
    entity collections.
 
 
    )ÚattrÚ_keyÚ_dataÚ owner_stateÚ
_converterÚ invalidatedÚemptyrr^Ústrr_z)Callable[..., _AdaptedCollectionProtocol]r`zInstanceState[Any]rar1rbÚboolrcrdr))r^raÚdatacCs>||_|j|_t |¡|_||_||_|j|_    d|_
d|_ dS)NF) r^Úkeyr_ÚweakrefÚrefr`rar=rArbrcrd)r6r^rargr5r5r7Ú__init__és zCollectionAdapter.__init__ÚNone©r3cCst d¡dS)Nz%This collection has been invalidated.)rÚwarn©r6r5r5r7Ú_warn_invalidatedÿsz#CollectionAdapter._warn_invalidatedcCs| ¡S)z$The entity collection being adapted.)r`ror5r5r7rgszCollectionAdapter.datacCs|jj|j| ¡kS)zÃreturn True if the owner state still refers to this collection.
 
        This will return False within a bulk replace operation,
        where this collection is the one being replaced.
 
        )raÚdictr_r`ror5r5r7Ú_referenced_by_ownersz&CollectionAdapter._referenced_by_ownercCs
| ¡jSr4©r`r>ror5r5r7Ú bulk_appenderszCollectionAdapter.bulk_appenderNrzOptional[AttributeEventToken])ÚitemÚ    initiatorr3cCs| ¡j||ddS)z8Add an entity to the collection, firing mutation events.©Ú _sa_initiatorNrs©r6rurvr5r5r7Úappend_with_eventsz#CollectionAdapter.append_with_eventcCs&|jrtdƒ‚d|_||jj|j<dS)Nz7This collection adapter is already in the 'empty' stateT)rdÚAssertionErrorraÚ_empty_collectionsr_)r6Ú    user_datar5r5r7Ú
_set_emptys ÿþzCollectionAdapter._set_emptycCs2|jstdƒ‚d|_|jj |j¡|jj|j<dS)Nz3This collection adapter is not in the 'empty' stateF)rdr{rar|Úpopr_rqror5r5r7Ú _reset_empty"sÿþþÿzCollectionAdapter._reset_emptyr
cCst d¡‚dS)NzZThis is a special 'empty' collection which cannot accommodate internal mutation operations)Úsa_excZInvalidRequestErrorror5r5r7Ú _refuse_empty+sÿzCollectionAdapter._refuse_empty)rur3cCs$|jr| ¡| ¡j|dddS©z=Add or restore an entity to the collection, firing no events.FrwN©rdr‚r`r>©r6rur5r5r7Úappend_without_event1sz&CollectionAdapter.append_without_eventz Iterable[Any])Úitemsr3cCs2|jr| ¡| ¡j}|D]}||ddqdSrƒr„)r6r‡rCrur5r5r7Úappend_multiple_without_event8s
 
z/CollectionAdapter.append_multiple_without_eventcCs
| ¡jSr4©r`r?ror5r5r7Ú bulk_remover@szCollectionAdapter.bulk_removercCs| ¡j||ddS)z=Remove an entity from the collection, firing mutation events.rwNr‰ryr5r5r7Úremove_with_eventCsz#CollectionAdapter.remove_with_eventcCs$|jr| ¡| ¡j|dddS)z7Remove an entity from the collection, firing no events.FrwN)rdr‚r`r?r…r5r5r7Úremove_without_eventIsz&CollectionAdapter.remove_without_event)rvr3cCs6|jr| ¡| ¡j}t|ƒD]}|||dq dS)z>Empty the collection, firing a mutation event for each entity.rwN©rdr‚r`r?Úlist)r6rvrHrur5r5r7Úclear_with_eventOs
 
 z"CollectionAdapter.clear_with_eventcCs6|jr| ¡| ¡j}t|ƒD]}||ddq dS)z'Empty the collection, firing no events.FrwNr)r6rHrur5r5r7Úclear_without_eventZs
 
 z%CollectionAdapter.clear_without_eventcCst| ¡ ¡ƒS)z(Iterate over entities in the collection.)Úiterr`r@ror5r5r7Ú__iter__cszCollectionAdapter.__iter__cCstt| ¡ ¡ƒƒS)z!Count entities in the collection.)ÚlenrŽr`r@ror5r5r7Ú__len__hszCollectionAdapter.__len__cCsdS©NTr5ror5r5r7Ú__bool__lszCollectionAdapter.__bool__cCsF|dk    r>|jr| ¡|jr$| ¡|j |j|jj|||¡S|SdS)abNotify that a entity is entering the collection but is already
        present.
 
 
        Initiator is a token owned by the InstrumentedAttribute that
        initiated the membership mutation, and should be left as None
        unless you are passing along an initiator value from a chained
        operation.
 
        .. versionadded:: 1.4.15
 
        FN)rcrprdr€r^Úfire_append_wo_mutation_eventrarq©r6rurvrhr5r5r7r—os ÿz/CollectionAdapter.fire_append_wo_mutation_eventcCsF|dk    r>|jr| ¡|jr$| ¡|j |j|jj|||¡S|SdS)a Notify that a entity has entered the collection.
 
        Initiator is a token owned by the InstrumentedAttribute that
        initiated the membership mutation, and should be left as None
        unless you are passing along an initiator value from a chained
        operation.
 
        FN)rcrprdr€r^rMrarqr˜r5r5r7rM‰s    ÿz#CollectionAdapter.fire_append_eventcCsB|dk    r>|jr| ¡|jr$| ¡|j |j|jj|||¡dS)aNotify that a entity has been removed from the collection.
 
        Initiator is the InstrumentedAttribute that initiated the membership
        mutation, and should be left as None unless you are passing along
        an initiator value from a chained operation.
 
        FN)rcrprdr€r^rUrarqr˜r5r5r7rUŸsÿz#CollectionAdapter.fire_remove_eventcCs,|jr| ¡|jj|j|jj||ddS)z«Notify that an entity is about to be removed from the collection.
 
        Only called if the entity cannot be removed after calling
        fire_remove_event().
 
        )rvrhN)rcrpr^Úfire_pre_remove_eventrarq)r6rvrhr5r5r7r™²süz'CollectionAdapter.fire_pre_remove_eventcCs |j|j|jj|j|j|jdœS)N)rhraÚ    owner_clsrgrcrd)r_raÚclass_rgrcrdror5r5r7Ú __getstate__ÂsúzCollectionAdapter.__getstate__cCsj|d|_|d|_t |d¡|_|dj|_||d_|d|_t    |d|jƒj
|_ |  dd¡|_ dS)NrhrargrcršrdF)r_rarirjr`rArbr=rcÚgetattrÚimplr^Úgetrd)r6Údr5r5r7Ú __setstate__Ìs
 
 
 
zCollectionAdapter.__setstate__)N)N)N)!r9r:r;r[Ú    __slots__rBrkrpÚpropertyrgrrrtrzr~r€r‚r†rˆrŠr‹rŒrrr’r”r–rr—rMrUr™rœr¡r5r5r5r7r<ÇsN
 
    ÿ    ÿÿ     
c Csªt|tƒst‚tj}||pdƒ}| |p*d¡}||p6dƒ |¡}| |¡}| ¡}    |pXdD].}
|
|krt|    |
|dqZ|
|krZ|    |
ddqZ|r¦|D]}
|j|
|dq’dS)aFLoad a new collection, firing events based on prior like membership.
 
    Appends instances in ``values`` onto the ``new_adapter``. Events will be
    fired for any instance not present in the ``existing_adapter``.  Any
    instances in ``existing_adapter`` not present in ``values`` will have
    remove events fired upon them.
 
    :param values: An iterable of collection member instances
 
    :param existing_adapter: A :class:`.CollectionAdapter` of
     instances to be replaced
 
    :param new_adapter: An empty :class:`.CollectionAdapter`
     to load with ``values``
 
 
    r5rwF)rvN)    Ú
isinstancerŽr{rZ IdentitySetÚ intersectionÚ
differencertrU) ÚvaluesZexisting_adapterZ new_adapterrvZidsetZexisting_idsetÚ    constantsZ    additionsZremovalsrCÚmemberr5r5r7Ú bulk_replaceÚs 
 rªz4Union[Type[Collection[Any]], _CollectionFactoryType]Ú_CollectionFactoryType)Úfactoryr3cCsx|tkrt|}n
tt|ƒ}t|ƒƒ}|tkr@t|}t|ƒƒ}t ¡rtz t|ddƒt|ƒkrft    |ƒW5t ¡X|S)aoPrepare a callable for future use as a collection class factory.
 
    Given a collection class factory (either a type or no-arg callable),
    return another factory that will produce compatible instances when
    called.
 
    This function is responsible for converting collection_class=list
    into the run-time behavior of collection_class=InstrumentedList.
 
    rJN)
Ú__canned_instrumentationrr«ÚtypeÚ__instrumentation_mutexÚacquireÚreleaserÚidÚ_instrument_class)r¬Z impl_factoryÚclsr5r5r7Úprepare_instrumentations
 
 
 
 
rµcCsH|jdkrt d¡‚t|ƒ\}}t|||ƒt|||ƒt|||ƒdS)z6Modify methods in a class and install instrumentation.Ú __builtin__zGCan not instrument a built-in type. Use a subclass, even a trivial one.N)r:rÚ ArgumentErrorÚ_locate_roles_and_methodsÚ_setup_canned_rolesÚ_assert_required_rolesÚ_set_collection_attributes©r´ÚrolesÚmethodsr5r5r7r³3s
ÿ   r³c CsÚi}i}|jD]Â}t|ƒ ¡D]°\}}t|ƒs0qt|dƒrX|j}|dksLt‚| ||¡d}d}t|dƒrˆ|j\}    }
|    dks€t‚|    |
f}t|dƒr¨|j    }    |    dks¤t‚|    }|r¼||f||<q|rdd|f||<qq||fS)zgsearch for _sa_instrument_role-decorated methods in
    method resolution order, assign to roles.
 
    rE)rCrHrIrLNrO)rMrUrV)
Ú__mro__Úvarsr‡ÚcallableÚhasattrrEr{Ú
setdefaultrOrV) r´r½r¾ZsuperclsÚnameÚmethodÚroleÚbeforeÚafterÚopÚargumentr5r5r7r¸Hs2
 
 
 
 
 r¸c Cst |¡}|tkrŒ|dk    st‚t|\}}| ¡D]\}}| ||¡q2| ¡D]:\}}    t||dƒ}
|
rP||krPt|
dƒsPt|||    |
ƒƒqPdS)zÇsee if this class has "canned" roles based on a known
    collection type (dict, set, list).  Apply those roles
    as needed to the "roles" dictionary, and also
    prepare "decorator" methods
 
    NrJ)    rÚduck_type_collectionÚ __interfacesr{r‡rÃrrÂÚsetattr) r´r½r¾Zcollection_typeZ canned_rolesZ
decoratorsrÆrÄrÅrRrGr5r5r7r¹us
   ÿþýr¹cCsÒd|kst||dƒs(t d|j¡‚n,|d|krTtt||dƒdƒsTd||d<d|ksjt||dƒs|t d|j¡‚n,|d|kr¨tt||dƒdƒs¨d||d<d|ks¾t||dƒsÎt d    |j¡‚d
S) zTensure all roles are present, and apply implicit instrumentation if
    needed
 
    rCz>Type %s must elect an appender method to be a collection classrJ)rMrNrHz<Type %s must elect a remover method to be a collection class)rUrNrIz>Type %s must elect an iterator method to be a collection classN)rÂrr·r9rr¼r5r5r7rºŽs8ÿÿ ÿ ÿÿ ÿ ÿÿrºc    Cs€| ¡D]*\}\}}}t||tt||ƒ|||ƒƒq| ¡D]\}}t|d|t||ƒƒq<d|_t|dƒsrd|_t|ƒ|_dS)zkapply ad-hoc instrumentation from decorators, class-level defaults
    and implicit role declarations
 
    z_sa_%sNrA)    r‡rÍÚ_instrument_membership_mutatorrr=rÂrAr²rJ)r´r½r¾Ú method_namerÇrÊrÈrÆr5r5r7r»®s"ÿý
r»cs¢ˆr`tt tˆƒd¡ƒ}tˆtƒrBˆ‰t|ƒˆkr<|ˆp>d‰nˆ|krV| ˆ¡‰nd‰ˆ‰~‡‡‡‡‡‡fdd„}d|_t    ˆdƒrŽˆj
|_
ˆj |_ ˆj |_ |S)zIRoute method args and/or return value through the collection
    adapter.rNcsڈrbˆdkr,ˆ|kr"t dˆ¡‚|ˆ}n6t|ƒˆkrB|ˆ}n ˆ|krT|ˆ}nt dˆ¡‚| dd¡}|dkr|d}n
|dj}ˆrž|ržt|ˆƒ||ƒˆr¦|s°ˆ||ŽSˆ||Ž}|dk    rÒt|ˆƒ||ƒ|SdS)NzMissing argument %srxFr)rr·r“rr=r)ÚargsÚkwÚvaluervÚexecutorÚres©rÈrÊrÇrÅZ    named_argZpos_argr5r7ÚwrapperÙs4ÿ
 
 
ÿ 
 
 
z/_instrument_membership_mutator.<locals>.wrapperTrE) rŽrZflatten_iteratorrr¤Úintr“ÚindexrJrÂrEr9r[)rÅrÇrÊrÈZfn_argsrÖr5rÕr7rÎÆs&ÿ
 #
rÎcCs&|dk    r"|j}|r"|j||dddS)zERun set wo mutation events.
 
    The collection is not mutated.
 
    FN©rh)r=r—)r&rurxrÓr5r5r7Ú__set_wo_mutationsÿrÚcCs&|dk    r"|j}|r"|j|||d}|S)z^Run set events.
 
    This event always occurs before the collection is actually mutated.
 
    FrÙ)r=rM©r&rurxrhrÓr5r5r7Ú__sets
rÜcCs&|dk    r"|j}|r"|j|||ddS)aRun del events.
 
    This event occurs before the collection is actually mutated, *except*
    in the case of a pop operation, in which case it occurs afterwards.
    For pop operations, the __before_pop hook is called before the
    operation occurs.
 
    FrÙN)r=rUrÛr5r5r7Ú__del s    rÝcCs|j}|r| |¡dS)z;An event which occurs on a before a pop() operation occurs.N)r=r™)r&rxrÓr5r5r7Ú __before_pop/srÞzDict[str, Callable[[_FN], _FN]]rmc
sŒdd„‰‡fdd„}‡fdd„}‡fdd„}‡fd    d
„}‡fd d „}‡fd d„}‡fdd„}‡fdd„}‡fdd„}tƒ ¡}    |     d¡|    S)z:Tailored instrumentation wrappers for any list-like class.cSsd|_tt|jƒj|_dSr•)rJrrŽr9r[rFr5r5r7Ú_tidy9sz_list_decorators.<locals>._tidycsd‡fdd„    }ˆ|ƒ|S)Ncst|||tƒ}ˆ||ƒdSr4)rÜr)r6rurxrFr5r7Úappend>sz0_list_decorators.<locals>.append.<locals>.append)Nr5)rGrà©rßrFr7rà=sz _list_decorators.<locals>.appendcsd‡fdd„    }ˆ|ƒ|S)Ncst|||tƒˆ||ƒdSr4©rÝr©r6rÒrxrFr5r7ÚremoveFsz0_list_decorators.<locals>.remove.<locals>.remove)Nr5©rGrärárFr7räEsz _list_decorators.<locals>.removecs‡fdd„}ˆ|ƒ|S)Ncst||d|ƒ}ˆ|||ƒdSr4)rÜ)r6rØrÒrFr5r7ÚinsertOsz0_list_decorators.<locals>.insert.<locals>.insertr5)rGrærárFr7ræNs z _list_decorators.<locals>.insertcs‡fdd„}ˆ|ƒ|S)Nc
sXt|tƒsF||}|dk    r(t||d|ƒt||d|ƒ}ˆ|||ƒn|jpNd}|jpXd}|dkrn|t|ƒ7}|jdk    r€|j}nt|ƒ}|dkrœ|t|ƒ7}|dkrø||kr°dSt|||ƒD]}t|ƒ|kr¼||=q¼t    |ƒD]\}}| 
|||¡qÜn\t t|||ƒƒ}    t|ƒt|    ƒkr2t dt|ƒt|    ƒfƒ‚t |    |ƒD]\}}| ||¡q<dS)NrrzBattempt to assign sequence of size %s to extended slice of size %s)r¤ÚslicerÝrÜÚstepÚstartr“ÚstopÚrangeÚ    enumeraterærŽÚ
ValueErrorÚzipÚ __setitem__)
r6rØrÒÚexistingrèrérêÚiruÚrngrFr5r7rïWs@
 
 
 
  þÿz:_list_decorators.<locals>.__setitem__.<locals>.__setitem__r5©rGrïrárFr7rïVs (z%_list_decorators.<locals>.__setitem__cs‡fdd„}ˆ|ƒ|S)NcsVt|tƒs,||}t||d|ƒˆ||ƒn&||D]}t||d|ƒq4ˆ||ƒdSr4)r¤rçrÝ©r6rØrurFr5r7Ú __delitem__ƒs
  z:_list_decorators.<locals>.__delitem__.<locals>.__delitem__r5©rGrõrárFr7rõ‚s z%_list_decorators.<locals>.__delitem__csdd„}ˆ|ƒ|S)NcSst|ƒD]}| |¡qdSr4©rŽrà©r6ÚiterablerÒr5r5r7Úextend”s z0_list_decorators.<locals>.extend.<locals>.extendr5)rGrúrár5r7rú“sz _list_decorators.<locals>.extendcsdd„}ˆ|ƒ|S)NcSst|ƒD]}| |¡q|Sr4r÷rør5r5r7Ú__iadd__œs  z4_list_decorators.<locals>.__iadd__.<locals>.__iadd__r5)rGrûrár5r7rû›sz"_list_decorators.<locals>.__iadd__csd‡fdd„    }ˆ|ƒ|S)Néÿÿÿÿcs$t|ƒˆ||ƒ}t||d|ƒ|Sr4©rÞrÝrôrFr5r7r§s
z*_list_decorators.<locals>.pop.<locals>.pop)rür5©rGrrárFr7r¦sz_list_decorators.<locals>.popcsd‡fdd„    }ˆ|ƒ|S)Nrücs$|D]}t||d|ƒqˆ|ƒdSr4©rÝrôrFr5r7Úclear±sz._list_decorators.<locals>.clear.<locals>.clear)rür5©rGrrárFr7r°sz_list_decorators.<locals>.clearrß©ÚlocalsÚcopyr)
ràrärærïrõrúrûrrÚlr5rár7Ú_list_decorators6s        ,  
 
 
rcstdd„‰‡fdd„}‡fdd„}‡fdd„}‡fd    d
„}‡fd d „}‡fd d„}‡fdd„}tƒ ¡}| d¡|S)zBTailored instrumentation wrappers for any dict-like mapping class.cSsd|_tt|jƒj|_dSr•)rJrrqr9r[rFr5r5r7r߯sz_dict_decorators.<locals>._tidycsd‡fdd„    }ˆ|ƒ|S)Ncs8||krt|||||ƒt||||ƒ}ˆ|||ƒdSr4)rÝrÜ)r6rhrÒrxrFr5r7rïËsz:_dict_decorators.<locals>.__setitem__.<locals>.__setitem__)Nr5rórárFr7rïÊsz%_dict_decorators.<locals>.__setitem__csd‡fdd„    }ˆ|ƒ|S)Ncs(||krt|||||ƒˆ||ƒdSr4rÿ)r6rhrxrFr5r7rõÕsz:_dict_decorators.<locals>.__delitem__.<locals>.__delitem__)Nr5rörárFr7rõÔsz%_dict_decorators.<locals>.__delitem__cs‡fdd„}ˆ|ƒ|S)Ncs(|D]}t|||d|ƒqˆ|ƒdSr4rÿ)r6rhrFr5r7rÞsz._dict_decorators.<locals>.clear.<locals>.clearr5rrárFr7rÝs z_dict_decorators.<locals>.clearcstf‡fdd„    }ˆ|ƒ|S)NcsFt|ƒ||k}|tkr$ˆ||ƒ}n ˆ|||ƒ}|rBt||d|ƒ|Sr4)rÞrrÝ)r6rhÚdefaultZ_to_delrurFr5r7rçs  z*_dict_decorators.<locals>.pop.<locals>.poprrþrárFr7ræs z_dict_decorators.<locals>.popcs‡fdd„}ˆ|ƒ|S)Ncs&t|ƒˆ|ƒ}t||dddƒ|S)Nrrýr…rFr5r7Úpopitemösz2_dict_decorators.<locals>.popitem.<locals>.popitemr5)rGrrárFr7rõs z!_dict_decorators.<locals>.popitemcsddd„}ˆ|ƒ|S)NcSs>||kr| ||¡|S| |¡}||kr6t||dƒ|SdSr4)rïÚ __getitem__rÚ)r6rhrrÒr5r5r7rÃs 
 z8_dict_decorators.<locals>.setdefault.<locals>.setdefault)Nr5)rGrÃrár5r7rÃÿs
z$_dict_decorators.<locals>.setdefaultcstfdd„}ˆ|ƒ|S)Nc[sÔ|tk    rt|dƒrXt|ƒD]:}||ks6||||k    rD||||<qt|||dƒqn8|D]2\}}||ksx|||k    r‚|||<q\t||dƒq\|D]:}||ks°||||k    r¾||||<q”t|||dƒq”dS)NÚkeys)rrÂrŽrÚ)r6Z__otherrÑrhrÒr5r5r7Úupdates
 
z0_dict_decorators.<locals>.update.<locals>.updater©rGr rár5r7r s z _dict_decorators.<locals>.updaterßr)rïrõrrrrÃr rr5rár7Ú_dict_decoratorsÃs
          
 
 
r rrf)r6Úobjr3cCst|t|jfƒS)zKAllow only set, frozenset and self.__class__-derived
    objects in binops.)r¤Ú_set_binop_basesÚ    __class__©r6rr5r5r7Ú_set_binops_check_strict.srcCs t|t|jfƒpt |¡tkS)z5Allow anything set-like to participate in set binops.)r¤rrrrËÚsetrr5r5r7Ú_set_binops_check_loose4s þrcs¼dd„‰‡fdd„}‡fdd„}‡fdd„}‡fd    d
„}‡fd d „}‡fd d„}‡fdd„}‡fdd„}‡fdd„}‡fdd„}    ‡fdd„}
‡fdd„} ‡fdd„} tƒ ¡} |  d¡| S)z9Tailored instrumentation wrappers for any set-like class.cSsd|_tt|jƒj|_dSr•)rJrrr9r[rFr5r5r7rß?sz_set_decorators.<locals>._tidycsd‡fdd„    }ˆ|ƒ|S)Ncs2||krt|||tƒ}n t|||ƒˆ||ƒdSr4)rÜrrÚrãrFr5r7ÚaddDs z)_set_decorators.<locals>.add.<locals>.add)Nr5)rGrrárFr7rCsz_set_decorators.<locals>.addcsd‡fdd„    }ˆ|ƒ|S)Ncs$||krt|||tƒˆ||ƒdSr4rârãrFr5r7ÚdiscardPsz1_set_decorators.<locals>.discard.<locals>.discard)Nr5)rGrrárFr7rOsz _set_decorators.<locals>.discardcsd‡fdd„    }ˆ|ƒ|S)Ncs$||krt|||tƒˆ||ƒdSr4rârãrFr5r7rä[sz/_set_decorators.<locals>.remove.<locals>.remove)Nr5rårárFr7räZsz_set_decorators.<locals>.removecs‡fdd„}ˆ|ƒ|S)Ncs"t|ƒˆ|ƒ}t||dtƒ|Sr4)rÞrÝrr…rFr5r7rfsz)_set_decorators.<locals>.pop.<locals>.popr5rþrárFr7res z_set_decorators.<locals>.popcsdd„}ˆ|ƒ|S)NcSst|ƒD]}| |¡qdSr4)rŽrär…r5r5r7rrs z-_set_decorators.<locals>.clear.<locals>.clearr5rrár5r7rqsz_set_decorators.<locals>.clearcsdd„}ˆ|ƒ|S)NcSs|D]}| |¡qdSr4)r©r6rÒrur5r5r7r zsz/_set_decorators.<locals>.update.<locals>.updater5r rár5r7r ysz_set_decorators.<locals>.updatecsdd„}ˆ|ƒ|S)NcSs&t||ƒstS|D]}| |¡q|Sr4)rÚNotImplementedrrr5r5r7Ú__ior__‚s
 
 z1_set_decorators.<locals>.__ior__.<locals>.__ior__r5)rGrrár5r7rsz _set_decorators.<locals>.__ior__csdd„}ˆ|ƒ|S)NcSs|D]}| |¡qdSr4)rrr5r5r7Údifference_updateszE_set_decorators.<locals>.difference_update.<locals>.difference_updater5)rGrrár5r7rŒsz*_set_decorators.<locals>.difference_updatecsdd„}ˆ|ƒ|S)NcSs&t||ƒstS|D]}| |¡q|Sr4)rrrrr5r5r7Ú__isub__•s
 
 z3_set_decorators.<locals>.__isub__.<locals>.__isub__r5)rGrrár5r7r”sz!_set_decorators.<locals>.__isub__csdd„}ˆ|ƒ|S)NcSsR| |¡t|ƒ}}||||}}|D]}| |¡q*|D]}| |¡q>dSr4)r¥rrär©r6ÚotherZwantZhaverärrur5r5r7Úintersection_update s  zI_set_decorators.<locals>.intersection_update.<locals>.intersection_updater5)rGrrár5r7rŸs    z,_set_decorators.<locals>.intersection_updatecsdd„}ˆ|ƒ|S)NcSs`t||ƒstS| |¡t|ƒ}}||||}}|D]}| |¡q8|D]}| |¡qL|Sr4)rrr¥rrärrr5r5r7Ú__iand__­s
  z3_set_decorators.<locals>.__iand__.<locals>.__iand__r5)rGrrár5r7r¬s z!_set_decorators.<locals>.__iand__csdd„}ˆ|ƒ|S)NcSsR| |¡t|ƒ}}||||}}|D]}| |¡q*|D]}| |¡q>dSr4)Úsymmetric_differencerrärrr5r5r7Úsymmetric_difference_update½s  zY_set_decorators.<locals>.symmetric_difference_update.<locals>.symmetric_difference_updater5)rGr!rár5r7r!¼s    z4_set_decorators.<locals>.symmetric_difference_updatecsdd„}ˆ|ƒ|S)NcSs`t||ƒstS| |¡t|ƒ}}||||}}|D]}| |¡q8|D]}| |¡qL|Sr4)rrr rrärrr5r5r7Ú__ixor__Ês
  z3_set_decorators.<locals>.__ixor__.<locals>.__ixor__r5)rGr"rár5r7r"És z!_set_decorators.<locals>.__ixor__rßr)rrrärrr rrrrrr!r"rr5rár7Ú_set_decorators<s"     
 
r#c@seZdZdZdS)ÚInstrumentedListz-An instrumented version of the built-in list.N©r9r:r;r[r5r5r5r7r$Þsr$c@seZdZdZdS)ÚInstrumentedSetz,An instrumented version of the built-in set.Nr%r5r5r5r7r&âsr&c@seZdZdZdS)ÚInstrumentedDictz-An instrumented version of the built-in dict.Nr%r5r5r5r7r'æsr'z/util.immutabledict[Any, _CollectionFactoryType]r­ràrär’)rCrHrIrrIr§zMutil.immutabledict[Any, Tuple[Dict[str, str], Dict[str, Callable[..., Any]]]]rÌcCs|ddlmaddlmaddlmaddlmaddlmaddlmaddlmadd    lmatt    ƒtt
ƒttƒdS)
Nrr!rrr#)Úmapped_collection)Úcolumn_mapped_collection)Úattribute_mapped_collection)r() r(r"r rr$r)r*r(r³r$r&)Zlclsr5r5r7Ú__gos        r+)N)N)N)Zr[Ú
__future__rÚoperatorÚ    threadingÚtypingrrrrrrr    r
r r r rrrrriÚbaserÚrrrZsql.baserZ util.compatrZ util.typingrÚ
attributesrrr(rr r"r$Ústater%Ú__all__ÚLockr¯r«r*r,r-r.r/r1r)r&r'Ú
attrgetterr<rªrµr³r¸r¹rºr»rÎrÚrÜrÝrÞrr rÚ    frozensetrrrr#r$r&r'Z immutabledictrŽrqr­rBrÌr+rr5r5r5r7Ú<module>    säa                             ÷       
 

(1- >
 
h#ýÿþ
ýú
þ òÿú