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
U
¸ý°dÛã@sÐdZddlmZddlZddlZddlmZddlmZddlm    Z    ddl
m Z dd    l m Z dd
lmZdd lmZdd lmZdd lmZddddgZdd„Ze d¡Zddd„Zejfdd„Zddd„ZdS)aNSerializer/Deserializer objects for usage with SQLAlchemy query structures,
allowing "contextual" deserialization.
 
.. legacy::
 
    The serializer extension is **legacy** and should not be used for
    new development.
 
Any SQLAlchemy query structure, either based on sqlalchemy.sql.*
or sqlalchemy.orm.* can be used.  The mappers, Tables, Columns, Session
etc. which are referenced by the structure are not persisted in serialized
form, but are instead re-associated with the query structure
when it is deserialized.
 
.. warning:: The serializer extension uses pickle to serialize and
   deserialize objects, so the same security consideration mentioned
   in the `python documentation
   <https://docs.python.org/3/library/pickle.html>`_ apply.
 
Usage is nearly the same as that of the standard Python pickle module::
 
    from sqlalchemy.ext.serializer import loads, dumps
    metadata = MetaData(bind=some_engine)
    Session = scoped_session(sessionmaker())
 
    # ... define mappers
 
    query = Session.query(MyClass).
        filter(MyClass.somedata=='foo').order_by(MyClass.sortkey)
 
    # pickle the query
    serialized = dumps(query)
 
    # unpickle.  Pass in metadata + scoped_session
    query2 = loads(serialized, metadata, Session)
 
    print query2.all()
 
Similar restrictions as when using raw pickle apply; mapped classes must be
themselves be pickleable, meaning they are importable from a module-level
namespace.
 
The serializer module is only appropriate for query structures.  It is not
needed for:
 
* instances of user-defined classes.   These contain no references to engines,
  sessions or expression constructs in the typical case and can be serialized
  directly.
 
* Table metadata that is to be loaded entirely from the serialized structure
  (i.e. is not already declared in the application).   Regular
  pickle.loads()/dumps() can be used to fully dump any ``MetaData`` object,
  typically one which was reflected from an existing database at some previous
  point in time.  The serializer module is specifically for the opposite case,
  where the Table metadata is already present in memory.
 
é)ÚBytesIONé)ÚColumn)ÚTable)ÚEngine)Ú class_mapper)ÚMapperProperty)ÚMapper)ÚSession)Ú    b64decode)Ú    b64encodeÚ
SerializerÚ DeserializerÚdumpsÚloadscOstj||Ž}dd„}||_|S)NcSsît|tƒr&|js&dtt |j¡ƒ}nÄt|tƒrZ|jjsZdtt |jj¡ƒd|j    }nt|t
ƒr˜d|j krŠdtt |j dj¡ƒ}qêd|j    ›}nRt|t ƒrÆt|j t
ƒrÆd|j j    ›d|j    ›}n$t|tƒrÖd}nt|tƒræd    }ndS|S)
Nzmapper:z mapperprop:ú:Z parententityzmapper_selectable:ztable:zcolumn:zsession:zengine:)Ú
isinstancer    Z non_primaryr ÚpicklerÚclass_rÚparentÚkeyrZ _annotationsrÚtabler
r)ÚobjÚid_©rúPd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/ext/serializer.pyÚ persistent_idXs2ÿþýÿ
 
ÿ
 
z!Serializer.<locals>.persistent_id)rÚPicklerr)ÚargsÚkwÚpicklerrrrrr Us zP(mapperprop|mapper|mapper_selectable|table|column|session|attribute|engine):(.*)cs4t |¡}‡‡‡fdd„‰‡‡‡fdd„}||_|S)Ncs4ˆrˆSˆrˆƒjrˆƒjSˆr,ˆjr,ˆjSdSdS©N)Úbindr)ÚengineÚmetadataÚscoped_sessionrrÚ
get_engines 
z Deserializer.<locals>.get_enginec s0t t|ƒ¡}|sdS| dd¡\}}|dkrT| d¡\}}t t|ƒ¡}t||ƒS|dkrrt t|ƒ¡}t    |ƒS|dkr”t t|ƒ¡}t    |ƒ 
¡S|dkrÆ| d¡\}}t t|ƒ¡}t    |ƒj |S|dkr؈j |S|d    kr| d¡\}    }
ˆj |    j |
S|d
krˆƒS|d kr ˆƒStd |ƒ‚dS) NérÚ    attributerÚmapperZmapper_selectableZ
mapperproprÚcolumnÚsessionr#zUnknown token: %s)Úour_idsÚmatchÚstrÚgroupÚsplitrrr ÚgetattrrZ__clause_element__ÚattrsZtablesÚcÚ    Exception) rÚmÚtype_rrZclsargÚclsr)ÚkeynamerZcolname)r&r$r%rrÚpersistent_load‹s8
 
 
 
 
z%Deserializer.<locals>.persistent_load)rÚ    Unpicklerr9)Úfiler$r%r#Ú    unpicklerr9r)r#r&r$r%rr~s
 
 
 cCs"tƒ}t||ƒ}| |¡| ¡Sr!)rr ÚdumpÚgetvalue)rÚprotocolÚbufr rrrr¯s
 
cCst|ƒ}t||||ƒ}| ¡Sr!)rrÚload)Údatar$r%r#r@r<rrrr¶s)NNN)NNN)Ú__doc__ÚiorrÚreÚrrr#rZormrZorm.interfacesrZ
orm.mapperr    Z orm.sessionr
Úutilr r Ú__all__r Úcompiler,rÚHIGHEST_PROTOCOLrrrrrrÚ<module>    s(:           #ÿ
1