$21 GRAYBYTE WORDPRESS FILE MANAGER $20

SERVER : vnpttt-amd7f72-h1.vietnix.vn #1 SMP Fri May 24 12:42:50 UTC 2024
SERVER IP : 103.200.23.149 | ADMIN IP 216.73.216.22
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/lib64/python2.7/site-packages/sqlalchemy/orm/

HOME
Current File : /lib64/python2.7/site-packages/sqlalchemy/orm//scoping.py
# orm/scoping.py
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
# <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: https://www.opensource.org/licenses/mit-license.php

from . import class_mapper
from . import exc as orm_exc
from .session import Session
from .. import exc as sa_exc
from ..util import create_proxy_methods
from ..util import ScopedRegistry
from ..util import ThreadLocalRegistry
from ..util import warn
from ..util import warn_deprecated

__all__ = ["scoped_session", "ScopedSessionMixin"]


class ScopedSessionMixin(object):
    @property
    def _proxied(self):
        return self.registry()

    def __call__(self, **kw):
        r"""Return 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.

        """
        if kw:
            if self.registry.has():
                raise sa_exc.InvalidRequestError(
                    "Scoped session is already present; "
                    "no new arguments may be specified."
                )
            else:
                sess = self.session_factory(**kw)
                self.registry.set(sess)
        else:
            sess = self.registry()
        if not self._support_async and sess._is_asyncio:
            warn_deprecated(
                "Using `scoped_session` with asyncio is deprecated and "
                "will raise an error in a future version. "
                "Please use `async_scoped_session` instead.",
                "1.4.23",
            )
        return sess

    def configure(self, **kwargs):
        """reconfigure the :class:`.sessionmaker` used by this
        :class:`.scoped_session`.

        See :meth:`.sessionmaker.configure`.

        """

        if self.registry.has():
            warn(
                "At least one scoped session is already present. "
                " configure() can not affect sessions that have "
                "already been created."
            )

        self.session_factory.configure(**kwargs)


@create_proxy_methods(
    Session,
    ":class:`_orm.Session`",
    ":class:`_orm.scoping.scoped_session`",
    classmethods=["close_all", "object_session", "identity_key"],
    methods=[
        "__contains__",
        "__iter__",
        "add",
        "add_all",
        "begin",
        "begin_nested",
        "close",
        "commit",
        "connection",
        "delete",
        "execute",
        "expire",
        "expire_all",
        "expunge",
        "expunge_all",
        "flush",
        "get",
        "get_bind",
        "is_modified",
        "bulk_save_objects",
        "bulk_insert_mappings",
        "bulk_update_mappings",
        "merge",
        "query",
        "refresh",
        "rollback",
        "scalar",
        "scalars",
    ],
    attributes=[
        "bind",
        "dirty",
        "deleted",
        "new",
        "identity_map",
        "is_active",
        "autoflush",
        "no_autoflush",
        "info",
        "autocommit",
    ],
)
class scoped_session(ScopedSessionMixin):
    """Provides scoped management of :class:`.Session` objects.

    See :ref:`unitofwork_contextual` for a tutorial.

    .. note::

       When using :ref:`asyncio_toplevel`, the async-compatible
       :class:`_asyncio.async_scoped_session` class should be
       used in place of :class:`.scoped_session`.

    """

    _support_async = False

    session_factory = None
    """The `session_factory` provided to `__init__` is stored in this
    attribute and may be accessed at a later time.  This can be useful when
    a new non-scoped :class:`.Session` or :class:`_engine.Connection` to the
    database is needed."""

    def __init__(self, session_factory, scopefunc=None):
        """Construct 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`.

        """
        self.session_factory = session_factory

        if scopefunc:
            self.registry = ScopedRegistry(session_factory, scopefunc)
        else:
            self.registry = ThreadLocalRegistry(session_factory)

    def remove(self):
        """Dispose 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.

        """

        if self.registry.has():
            self.registry().close()
        self.registry.clear()

    def query_property(self, query_cls=None):
        """return a class property which produces a :class:`_query.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.

        """

        class query(object):
            def __get__(s, instance, owner):
                try:
                    mapper = class_mapper(owner)
                    if mapper:
                        if query_cls:
                            # custom query class
                            return query_cls(mapper, session=self.registry())
                        else:
                            # session's configured query class
                            return self.registry().query(mapper)
                except orm_exc.UnmappedClassError:
                    return None

        return query()


ScopedSession = scoped_session
"""Old name for backwards compatibility."""

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
23 Sep 2024 10.41 AM
root / root
0755
__init__.py
10.707 KB
5 Sep 2024 10.50 PM
root / root
0644
__init__.pyc
12.471 KB
23 Sep 2024 10.41 AM
root / root
0644
attributes.py
75.885 KB
5 Sep 2024 10.50 PM
root / root
0644
attributes.pyc
68.515 KB
23 Sep 2024 10.41 AM
root / root
0644
base.py
14.881 KB
5 Sep 2024 10.50 PM
root / root
0644
base.pyc
14.961 KB
23 Sep 2024 10.41 AM
root / root
0644
clsregistry.py
12.987 KB
5 Sep 2024 10.50 PM
root / root
0644
clsregistry.pyc
14.702 KB
23 Sep 2024 10.41 AM
root / root
0644
collections.py
53.44 KB
5 Sep 2024 10.50 PM
root / root
0644
collections.pyc
61.913 KB
23 Sep 2024 10.41 AM
root / root
0644
context.py
108.652 KB
5 Sep 2024 10.50 PM
root / root
0644
context.pyc
61.606 KB
23 Sep 2024 10.41 AM
root / root
0644
decl_api.py
34.724 KB
5 Sep 2024 10.50 PM
root / root
0644
decl_api.pyc
37.08 KB
23 Sep 2024 10.41 AM
root / root
0644
decl_base.py
43.69 KB
5 Sep 2024 10.50 PM
root / root
0644
decl_base.pyc
30.072 KB
23 Sep 2024 10.41 AM
root / root
0644
dependency.py
45.886 KB
5 Sep 2024 10.50 PM
root / root
0644
dependency.pyc
28.217 KB
23 Sep 2024 10.41 AM
root / root
0644
descriptor_props.py
25.378 KB
5 Sep 2024 10.50 PM
root / root
0644
descriptor_props.pyc
27.322 KB
23 Sep 2024 10.41 AM
root / root
0644
dynamic.py
15.638 KB
5 Sep 2024 10.50 PM
root / root
0644
dynamic.pyc
16.155 KB
23 Sep 2024 10.41 AM
root / root
0644
evaluator.py
7.756 KB
5 Sep 2024 10.50 PM
root / root
0644
evaluator.pyc
10.283 KB
23 Sep 2024 10.41 AM
root / root
0644
events.py
109.648 KB
5 Sep 2024 10.50 PM
root / root
0644
events.pyc
116.313 KB
23 Sep 2024 10.41 AM
root / root
0644
exc.py
6.379 KB
5 Sep 2024 10.50 PM
root / root
0644
exc.pyc
8.012 KB
23 Sep 2024 10.41 AM
root / root
0644
identity.py
7.063 KB
5 Sep 2024 10.50 PM
root / root
0644
identity.pyc
9.846 KB
23 Sep 2024 10.41 AM
root / root
0644
instrumentation.py
19.914 KB
5 Sep 2024 10.50 PM
root / root
0644
instrumentation.pyc
21.94 KB
23 Sep 2024 10.41 AM
root / root
0644
interfaces.py
31.586 KB
5 Sep 2024 10.50 PM
root / root
0644
interfaces.pyc
36.536 KB
23 Sep 2024 10.41 AM
root / root
0644
loading.py
48.161 KB
5 Sep 2024 10.50 PM
root / root
0644
loading.pyc
28.739 KB
23 Sep 2024 10.41 AM
root / root
0644
mapper.py
140.959 KB
5 Sep 2024 10.50 PM
root / root
0644
mapper.pyc
106.941 KB
23 Sep 2024 10.41 AM
root / root
0644
path_registry.py
16.008 KB
5 Sep 2024 10.50 PM
root / root
0644
path_registry.pyc
17.739 KB
23 Sep 2024 10.41 AM
root / root
0644
persistence.py
82.275 KB
5 Sep 2024 10.50 PM
root / root
0644
persistence.pyc
48.377 KB
23 Sep 2024 10.41 AM
root / root
0644
properties.py
14.438 KB
5 Sep 2024 10.50 PM
root / root
0644
properties.pyc
14.746 KB
23 Sep 2024 10.41 AM
root / root
0644
query.py
122.999 KB
5 Sep 2024 10.50 PM
root / root
0644
query.pyc
119.512 KB
23 Sep 2024 10.41 AM
root / root
0644
relationships.py
140.622 KB
5 Sep 2024 10.50 PM
root / root
0644
relationships.pyc
114.873 KB
23 Sep 2024 10.41 AM
root / root
0644
scoping.py
7.087 KB
5 Sep 2024 10.50 PM
root / root
0644
scoping.pyc
7.317 KB
23 Sep 2024 10.41 AM
root / root
0644
session.py
158.795 KB
5 Sep 2024 10.50 PM
root / root
0644
session.pyc
142.412 KB
23 Sep 2024 10.41 AM
root / root
0644
state.py
32.738 KB
5 Sep 2024 10.50 PM
root / root
0644
state.pyc
32.091 KB
23 Sep 2024 10.41 AM
root / root
0644
strategies.py
105.8 KB
5 Sep 2024 10.50 PM
root / root
0644
strategies.pyc
65.906 KB
23 Sep 2024 10.41 AM
root / root
0644
strategy_options.py
66.687 KB
5 Sep 2024 10.50 PM
root / root
0644
strategy_options.pyc
58.673 KB
23 Sep 2024 10.41 AM
root / root
0644
sync.py
5.688 KB
5 Sep 2024 10.50 PM
root / root
0644
sync.pyc
4.58 KB
23 Sep 2024 10.41 AM
root / root
0644
unitofwork.py
26.455 KB
5 Sep 2024 10.50 PM
root / root
0644
unitofwork.pyc
24.882 KB
23 Sep 2024 10.41 AM
root / root
0644
util.py
74.473 KB
5 Sep 2024 10.50 PM
root / root
0644
util.pyc
69.456 KB
23 Sep 2024 10.41 AM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF