U kf@sdZddlmZddlZddlZddlmZddlmZddlm Z ddl m Z dd l m Z dd lmZdd lmZdd lmZdd lmZddddgZGdddejZedZGdddejZejfddZdddZdS)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 `_ 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 Deserializerdumpsloadsc@seZdZddZdS)r cCst|tr&|js&dtt|j}nt|trZ|jjsZdtt|jjd|j }nt|t rd|j krdtt|j dj}qd|j }nRt|t rt|j t rd|j j d|j }n$t|trd}nt|trd }ndS|S) Nzmapper:z mapperprop::Z parententityzmapper_selectable:ztable:zcolumn:zsession:zengine:) isinstancer Z non_primaryr picklerclass_rparentkeyrZ _annotationsrtabler r)selfobjid_rI/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/serializer.py persistent_idWs2    zSerializer.persistent_idN)__name__ __module__ __qualname__rrrrrr UszP(mapperprop|mapper|mapper_selectable|table|column|session|attribute|engine):(.*)cs.eZdZdfdd ZddZddZZS) rNcs"t|||_||_||_dSN)super__init__metadatascoped_sessionengine)rfiler$r%r& __class__rrr#|s zDeserializer.__init__cCs.|jr |jS|jr&|jr&|jSdSdSr!)r&r%bind)rrrr get_engines  zDeserializer.get_enginec Cs8tt|}|sdS|dd\}}|dkrT|d\}}tt|}t||S|dkrrtt|}t |S|dkrtt|}t | S|dkr|d\}} tt|}t |j | S|dkr|j j |S|d kr|d\} } |j j | j| S|d kr|S|d kr(|Std |dS) Nr attributermapperZmapper_selectableZ mapperproprcolumnsessionr&zUnknown token: %s)our_idsmatchstrgroupsplitrrr getattrrZ__clause_element__attrsr$Ztablescr%r+ Exception) rrmtype_argsrZclsargclsr.ZkeynamerZcolnamerrrpersistent_loads8      zDeserializer.persistent_load)NNN)rrr r#r+r> __classcell__rrr(rrzscCs"t}t||}|||Sr!)rr dumpgetvalue)rprotocolbufZpicklerrrrrs  cCst|}t||||}|Sr!)rrload)datar$r%r&rCZ unpicklerrrrrs)NNN)__doc__iorrrerrr&rZormrZorm.interfacesrZ orm.mapperr Z orm.sessionr utilr r __all__Picklerr compiler1 UnpicklerrHIGHEST_PROTOCOLrrrrrr s(:           1