B 4]@sddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dgZ Gd dde Z e Zd d ZxejD]Zee eeeq~Wd d ZxdD]Zee eeeqWddZxdD]Zee eeeqWdS)) class_mapper)exc)Session)ScopedRegistry)ThreadLocalRegistry)warnscoped_sessionc@s@eZdZdZdZd ddZddZddZd d Zdd d Z dS)r ztProvides scoped management of :class:`.Session` objects. See :ref:`unitofwork_contextual` for a tutorial. NcCs&||_|rt|||_n t||_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 Z scopefuncr I/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/orm/scoping.py__init__!szscoped_session.__init__cKsD|r8|jrtdq@|jf|}|j||Sn|SdS)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.N)r hassa_excZInvalidRequestErrorr set)r kwZsessr r r__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 rcloseclear)r r r rremovePs  zscoped_session.removecKs$|jrtd|jjf|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.N)r rrr configure)r kwargsr r rras 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..querycsNy2t|}|r0r"|dS|SWntjk rHdSXdS)N)session)rr queryorm_excZUnmappedClassError)sinstanceownerZmapper) query_clsr r r__get__sz4scoped_session.query_property..query.__get__N)__name__ __module__ __qualname__r!r )r r r rrsr)object)r r rr )r r rquery_propertyrszscoped_session.query_property)N)N) r"r#r$__doc__r rrrrr&r r r rr s csfdd}|S)Ncst|||S)N)getattrr )r argsr)namer rdoszinstrument..dor )r*r+r )r*r instruments r,cs"fdd}fdd}t||S)Ncst||dS)N)setattrr )r attr)r*r rset_szmakeprop..set_cst|S)N)r(r )r )r*r rgetszmakeprop..get)property)r*r/r0r )r*rmakeprops  r2) ZbindZdirtyZdeletednewZ identity_mapZ is_activeZ autoflushZ no_autoflushinfoZ autocommitcsfdd}t|S)Ncstt||S)N)r(r)clsr)r)r*r rr+szclslevel..do) classmethod)r*r+r )r*rclslevels r7)Z close_allZobject_sessionZ identity_keyN)rrrrrrutilrrr__all__r%r Z ScopedSessionr,Zpublic_methodsZmethr-r2Zpropr7r r r rs(