o znh@sddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dgZ Gd dde Z e Z d d ZejD] Zee eeeq?d d ZdD] Zee eeeqPddZdD] Zee eeeqadS)) class_mapper)exc)Session)ScopedRegistry)ThreadLocalRegistry)warnscoped_sessionc@sBeZdZdZdZ d ddZddZddZd d Zd d d Z dS)r ztProvides scoped management of :class:`.Session` objects. See :ref:`unitofwork_contextual` for a tutorial. NcCs(||_|r t|||_dSt||_dS)aConstruct a new :class:`.scoped_session`. :param session_factory: a factory to create new :class:`.Session` instances. This is usually, but not necessarily, an instance of :class:`.sessionmaker`. :param scopefunc: optional function which defines the current scope. If not passed, the :class:`.scoped_session` object assumes "thread-local" scope, and will use a Python ``threading.local()`` in order to maintain the current :class:`.Session`. If passed, the function should return a hashable token; this token will be used as the key in a dictionary in order to store and retrieve the current :class:`.Session`. N)session_factoryrregistryr)selfr scopefuncrA/usr/local/lib/python3.10/dist-packages/sqlalchemy/orm/scoping.py__init__!szscoped_session.__init__cKs@|r|jr td|jdi|}|j||S|S)aReturn the current :class:`.Session`, creating it using the :attr:`.scoped_session.session_factory` if not present. :param \**kw: Keyword arguments will be passed to the :attr:`.scoped_session.session_factory` callable, if an existing :class:`.Session` is not present. If the :class:`.Session` is present and keyword arguments have been passed, :exc:`~sqlalchemy.exc.InvalidRequestError` is raised. zEScoped session is already present; no new arguments may be specified.Nr)r hassa_excInvalidRequestErrorr set)r kwsessrrr__call__8s  zscoped_session.__call__cCs$|jr ||jdS)aDispose of the current :class:`.Session`, if present. This will first call :meth:`.Session.close` method on the current :class:`.Session`, which releases any existing transactional/connection resources still being held; transactions specifically are rolled back. The :class:`.Session` is then discarded. Upon next usage within the same scope, the :class:`.scoped_session` will produce a new :class:`.Session` object. N)r rcloseclearr rrrremovePs zscoped_session.removecKs(|jr td|jjdi|dS)zreconfigure the :class:`.sessionmaker` used by this :class:`.scoped_session`. See :meth:`.sessionmaker.configure`. ztAt least one scoped session is already present. configure() can not affect sessions that have already been created.Nr)r rrr configure)r kwargsrrrras zscoped_session.configurecsGfdddt}|S)a"return a class property which produces a :class:`.Query` object against the class and the current :class:`.Session` when called. e.g.:: Session = scoped_session(sessionmaker()) class MyClass(object): query = Session.query_property() # after mappers are defined result = MyClass.query.filter(MyClass.name=='foo').all() Produces instances of the session's configured query class by default. To override and use a custom implementation, provide a ``query_cls`` callable. The callable will be invoked with the class's mapper as a positional argument and a session keyword argument. There is no limit to the number of query properties placed on a class. cseZdZfddZdS)z,scoped_session.query_property..querycsPzt|}|rr|dWS|WSWdStjy'YdSw)N)session)rr queryorm_excUnmappedClassError)sinstanceownermapper query_clsr rr__get__sz4scoped_session.query_property..query.__get__N)__name__ __module__ __qualname__r(rr&rrrsr)object)r r'rrr&rquery_propertyrszscoped_session.query_propertyN) r)r*r+__doc__r rrrrr-rrrrr s csfdd}|S)Ncst||i|Sr.getattrr )r argsrnamerrdoszinstrument..dorr4r5rr3r instruments r7cs"fdd}fdd}t||S)Ncst||dSr.)setattrr )r attrr3rrset_zmakeprop..set_cst|Sr.r0rr3rrgetszmakeprop..get)property)r4r:r<rr3rmakeprops   r>) binddirtydeletednew identity_map is_active autoflush no_autoflushinfo autocommitcsfdd}t|S)Ncstt|i|Sr.)r1r)clsr2rr3rrr5r;zclslevel..do) classmethodr6rr3rclslevels rK) close_allobject_session identity_keyN)rrr rrrutilrrr__all__r,r ScopedSessionr7public_methodsmethr8r>proprKrrrrs,