B 4]8@sdZddlZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd l mZdd l mZdd l mZdd l mZddl mZdZddZegZGdddeZee _Zee _GdddeZGdddeZddZddZddZdS)aExtensible class instrumentation. The :mod:`sqlalchemy.ext.instrumentation` package provides for alternate systems of class instrumentation within the ORM. Class instrumentation refers to how the ORM places attributes on the class which maintain data and track changes to that data, as well as event hooks installed on the class. .. note:: The extension package is provided for the benefit of integration with other object management packages, which already perform their own instrumentation. It is not intended for general use. For examples of how the instrumentation extension is used, see the example :ref:`examples_instrumentation`. N)util) attributes)base) collections)exc)instrumentation)_default_dict_getter)_default_manager_getter)_default_state_getter) ClassManager)InstrumentationFactoryZ__sa_instrumentation_manager__cCs t|tdS)z;Find user-specified instrumentation management for a class.N)getattrINSTRUMENTATION_MANAGER)clsrQ/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/ext/instrumentation.py%find_native_user_instrumentation_hook;srcsteZdZdZeZeZeZdZ ddZ ddZ ddZ d d Z fd d Zd dZddZddZZS)ExtendedInstrumentationRegistryzExtends :class:`.InstrumentationFactory` with additional bookkeeping, to accommodate multiple types of class managers. FcCs:x4tD](}||}|dk r|||}||fSqWdSdS)N)NN)instrumentation_finders_extended_class_manager)selfclass_finderfactorymanagerrrr_locate_extended_factory]s    z8ExtendedInstrumentationRegistry._locate_extended_factorycCs0|||g}|r,td|jt|fdS)NzRmultiple instrumentation implementations specified in %s inheritance hierarchy: %r)!_collect_management_factories_for difference TypeError__name__list)rrrZexisting_factoriesrrr_check_conflictsfsz0ExtendedInstrumentationRegistry._check_conflictscCsd||}t|tst||}|tkr6|js6d|_t||j|<||j|<| |j |<|S)NT) isinstancer _ClassInstrumentationAdapter _extended_install_instrumented_lookupsmanager_getter_manager_finders state_getter_state_finders dict_getter _dict_finders)rrrrrrrrqs  z7ExtendedInstrumentationRegistry._extended_class_managercCszt|}t}xZ|D]R}||}|dk r:||jqx"tD]}||}|dk r@Pq@Wd}||qW|d|S)acReturn a collection of factories in play or specified for a hierarchy. Traverses the entire inheritance graph of a cls and returns a collection of instrumentation factories for those classes. Factories are extracted from active ClassManagers, if available, otherwise instrumentation_finders is consulted. N)rZclass_hierarchysetmanager_of_classaddrrdiscard)rrZ hierarchyZ factoriesmemberrrrrrrrs     zAExtendedInstrumentationRegistry._collect_management_factories_forcs6||jkr"|j|=|j|=|j|=tt||dS)N)r(r*r,superr unregister)rr) __class__rrr3s  z*ExtendedInstrumentationRegistry.unregistercCs@|dkr dSy|j|t}Wntk r2dSX||SdS)N)r(getr r)rrrrrrr.sz0ExtendedInstrumentationRegistry.manager_of_classcCs$|dkrtd|j|jt|S)NzNone has no persistent state.)AttributeErrorr*r5r4r )rinstancerrrstate_ofs z(ExtendedInstrumentationRegistry.state_ofcCs$|dkrtd|j|jt|S)NzNone has no persistent state.)r6r,r5r4r )rr7rrrdict_ofs z'ExtendedInstrumentationRegistry.dict_of)r __module__ __qualname____doc__weakrefWeakKeyDictionaryr(r*r,r%rr"rrr3r.r8r9 __classcell__rr)r4rrQs    rc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#Zd$S)%InstrumentationManageraWUser-defined class instrumentation extension. :class:`.InstrumentationManager` can be subclassed in order to change how class instrumentation proceeds. This class exists for the purposes of integration with other object management frameworks which would like to entirely modify the instrumentation methodology of the ORM, and is not intended for regular usage. For interception of class instrumentation events, see :class:`.InstrumentationEvents`. The API for this class should be considered as semi-stable, and may change slightly with new releases. cCsdS)Nr)rrrrr__init__szInstrumentationManager.__init__cCst|d|dS)N_default_class_manager)setattr)rrrrrrmanageszInstrumentationManager.managecCst|ddS)NrB)delattr)rrrrrrdisposeszInstrumentationManager.disposecCs dd}|S)NcSs|jS)N)rB)rrrrr5sz2InstrumentationManager.manager_getter..getr)rrr5rrrr'sz%InstrumentationManager.manager_gettercCsdS)Nr)rrkeyinstrrrinstrument_attributesz+InstrumentationManager.instrument_attributecCsdS)Nr)rrrGrHrrrpost_configure_attributesz/InstrumentationManager.post_configure_attributecCst|||dS)N)rC)rrrGrHrrrinstall_descriptorsz)InstrumentationManager.install_descriptorcCst||dS)N)rE)rrrGrrruninstall_descriptorsz+InstrumentationManager.uninstall_descriptorcCst|||dS)N)rC)rrrGimplementationrrrinstall_membersz%InstrumentationManager.install_membercCst||dS)N)rE)rrrGrrruninstall_membersz'InstrumentationManager.uninstall_membercCs t|S)N)rZprepare_instrumentation)rrrGcollection_classrrrinstrument_collection_classsz2InstrumentationManager.instrument_collection_classcCs|jS)N)__dict__)rrr7rrrget_instance_dictsz(InstrumentationManager.get_instance_dictcCsdS)Nr)rrr7rrrinitialize_instance_dictsz/InstrumentationManager.initialize_instance_dictcCst|d|dS)N_default_state)rC)rrr7staterrr install_statesz$InstrumentationManager.install_statecCst|ddS)NrU)rE)rrr7rrr remove_statesz#InstrumentationManager.remove_statecCsddS)NcSs t|dS)NrU)r)r7rrr z5InstrumentationManager.state_getter..r)rrrrrr) sz#InstrumentationManager.state_gettercsfddS)Ncs |S)N)rS)rH)rrrrrY rZz4InstrumentationManager.dict_getter..r)rrr)rrrr+ sz"InstrumentationManager.dict_getterN)r r:r;r<rArDrFr'rIrJrKrLrNrOrQrSrTrWrXr)r+rrrrr@s$r@cseZdZdZddZddZddZdd Zd*d d Zfd dZ ddZ ddZ ddZ ddZ ddZddZd+ddZddZd,d d!Zd"d#Zd$d%Zd&d'Zd(d)ZZS)-r$z?Adapts a user-defined InstrumentationManager to a ClassManager.cCs2||_|j||_|j||_t||dS)N)_adaptedr) _get_stater+ _get_dictr rA)rroverriderrrrAsz%_ClassInstrumentationAdapter.__init__cCs|j|j|dS)N)r[rDr)rrrrrDsz#_ClassInstrumentationAdapter.managecCs|j|jdS)N)r[rFr)rrrrrFsz$_ClassInstrumentationAdapter.disposecCs|j|jS)N)r[r'r)rrrrr' sz+_ClassInstrumentationAdapter.manager_getterFcCs*t|||||s&|j|j||dS)N)r rIr[r)rrGrHZ propagatedrrrrI#sz1_ClassInstrumentationAdapter.instrument_attributecs*tt|||j|j|||dS)N)r2r$rJr[r)rrG)r4rrrJ(sz5_ClassInstrumentationAdapter.post_configure_attributecCs|j|j||dS)N)r[rKr)rrGrHrrrrK,sz/_ClassInstrumentationAdapter.install_descriptorcCs|j|j|dS)N)r[rLr)rrGrrrrL/sz1_ClassInstrumentationAdapter.uninstall_descriptorcCs|j|j||dS)N)r[rNr)rrGrMrrrrN2sz+_ClassInstrumentationAdapter.install_membercCs|j|j|dS)N)r[rOr)rrGrrrrO5sz-_ClassInstrumentationAdapter.uninstall_membercCs|j|j||S)N)r[rQr)rrGrPrrrrQ8sz8_ClassInstrumentationAdapter.instrument_collection_classcCs2t|jdd}|r||||St||||SdS)Ninitialize_collection)rr[r r_)rrGrVrZdelegaterrrr_=s  z2_ClassInstrumentationAdapter.initialize_collectionNcCs|j|j}||||S)N)r__new__setup_instance)rrVr7rrr new_instanceFs z)_ClassInstrumentationAdapter.new_instancecCs||rdS||SdS)zInstall a default InstanceState if none is present. A private convenience method used by the __init__ decorator. FN) has_statera)rr7rrr_new_state_if_noneKs z/_ClassInstrumentationAdapter._new_state_if_nonecCs:|j|j||dkr$|||}|j|j|||S)N)r[rTrZ_state_constructorrW)rr7rVrrrraUs  z+_ClassInstrumentationAdapter.setup_instancecCs|j|j|dS)N)r[rXr)rr7rrrteardown_instance_sz._ClassInstrumentationAdapter.teardown_instancecCs.y||Wntjk r$dSXdSdS)NFT)r\orm_excZNO_STATE)rr7rrrrcbs z&_ClassInstrumentationAdapter.has_statecCs|jS)N)r\)rrrrr)jsz)_ClassInstrumentationAdapter.state_gettercCs|jS)N)r])rrrrr+msz(_ClassInstrumentationAdapter.dict_getter)F)N)N)r r:r;r<rArDrFr'rIrJrKrLrNrOrQr_rbrdrarercr)r+r?rr)r4rr$s(    r$cCstttjtjtjddS)aReplace global class/object management functions with ExtendedInstrumentationRegistry implementations, which allow multiple types of class managers to be present, at the cost of performance. This function is called only by ExtendedInstrumentationRegistry and unit tests specific to this behavior. The _reinstall_default_lookups() function can be called after this one to re-establish the default functions. )instance_state instance_dictr.N)_install_lookupsdict_instrumentation_factoryr8r9r.rrrrr&qs r&cCstttttddt_dS)zRestore simplified lookups.)rgrhr.FN)rirjr r r rkr%rrrr_reinstall_default_lookupss  rlcCsR|da|da|datt_t_t_tt_t_t_tt_t_t_dS)Nrgrhr.)rgrhr.orm_baserorm_instrumentation)Zlookupsrrrris ri)r<r=rZormrrrmrrrfrrnZorm.instrumentationr r r r r rrrrrkobjectr@r$r&rlrirrrrs.           o La