
    +h                     0   d Z ddlmZ ddlZddlZddlZddlmZ ddlmZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  d Z! G d de"      Z#d*dZ$ G d dejJ                  e&      Z' G d d e'      Z(d! Z)ddde*d"ede#fd#Z+ G d$ d%e*      Z, e,       e_-         ej\                  d&'      d(        Z/ ej`                  e#      d)        Z1y)+z1Public API functions and helpers for declarative.    )absolute_importN   )
attributes)clsregistry)exc)instrumentation)
interfacesmapper)_inspect_mapped_class_add_attribute)_as_declarative)_declarative_constructor)_DeferredMapperConfig_del_attribute_mapper)SynonymProperty   )
inspection)util)MetaData)hybridmethod)hybridpropertyc                 J    | j                   dd D ]  }t        |dd       y y)aK  Given a class, return True if any of the classes it inherits from has a
    mapped table, otherwise return False.

    This is used in declarative mixins to build attributes that behave
    differently for the base class vs. a subclass in an inheritance
    hierarchy.

    .. seealso::

        :ref:`decl_mixin_inheritance`

    r   N	__table__TF)__mro__getattr)clsclass_s     J/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/orm/decl_api.pyhas_inherited_tabler$   $   s4     ++ab/ 6;-9     c                       e Zd Zd Zd Zd Zy)DeclarativeMetac                 8   | j                   }t        | dd       }|>|j                  dd       }t        |t              st        j                  d      || _        | j                   j                  dd      st        || |       t        j                  | |||       y )N_sa_registryregistryziDeclarative base class has no 'registry' attribute, or registry is not a sqlalchemy.orm.registry() object__abstract__F)__dict__r    get
isinstancer*   r   InvalidRequestErrorr)   r   type__init__)r!   	classnamebasesdict_kwregs         r#   r1   zDeclarativeMeta.__init__8   s     
 c>40;))J-Cc8,--L 
 $' ||6Ce,c9eU3r%   c                     t        | ||       y Nr   )r!   keyvalues      r#   __setattr__zDeclarativeMeta.__setattr__O   s    sC'r%   c                     t        | |       y r8   r   r!   r9   s     r#   __delattr__zDeclarativeMeta.__delattr__R   s    sC r%   N)__name__
__module____qualname__r1   r;   r>    r%   r#   r'   r'   7   s    4.(!r%   r'   c                       fd}|S )a  Decorator that produces an :func:`_orm.synonym`
    attribute in conjunction with a Python descriptor.

    The function being decorated is passed to :func:`_orm.synonym` as the
    :paramref:`.orm.synonym.descriptor` parameter::

        class MyClass(Base):
            __tablename__ = 'my_table'

            id = Column(Integer, primary_key=True)
            _job_status = Column("job_status", String(50))

            @synonym_for("job_status")
            @property
            def job_status(self):
                return "Status: %s" % self._job_status

    The :ref:`hybrid properties <mapper_hybrids>` feature of SQLAlchemy
    is typically preferred instead of synonyms, which is a more legacy
    feature.

    .. seealso::

        :ref:`synonyms` - Overview of synonyms

        :func:`_orm.synonym` - the mapper-level function

        :ref:`mapper_hybrids` - The Hybrid Attribute extension provides an
        updated approach to augmenting attribute behavior more flexibly than
        can be achieved with synonyms.

    c                      t        |       S )N)
map_column
descriptor)_orm_synonym)fnrE   names    r#   decoratezsynonym_for.<locals>.decoratex   s    DZBGGr%   rB   )rI   rE   rJ   s   `` r#   synonym_forrK   V   s    DH Or%   c                   J     e Zd ZdZd fd	Zd Zed        Zed        Z	 xZ
S )declared_attraa  Mark a class-level method as representing the definition of
    a mapped property or special declarative member name.

    :class:`_orm.declared_attr` is typically applied as a decorator to a class
    level method, turning the attribute into a scalar-like property that can be
    invoked from the uninstantiated class. The Declarative mapping process
    looks for these :class:`_orm.declared_attr` callables as it scans classes,
    and assumes any attribute marked with :class:`_orm.declared_attr` will be a
    callable that will produce an object specific to the Declarative mapping or
    table configuration.

    :class:`_orm.declared_attr` is usually applicable to mixins, to define
    relationships that are to be applied to different implementors of the
    class. It is also used to define :class:`_schema.Column` objects that
    include the :class:`_schema.ForeignKey` construct, as these cannot be
    easily reused across different mappings.  The example below illustrates
    both::

        class ProvidesUser(object):
            "A mixin that adds a 'user' relationship to classes."

            @declared_attr
            def user_id(self):
                return Column(ForeignKey("user_account.id"))

            @declared_attr
            def user(self):
                return relationship("User")

    :class:`_orm.declared_attr` can also be applied to mapped classes, such as
    to provide a "polymorphic" scheme for inheritance::

        class Employee(Base):
            id = Column(Integer, primary_key=True)
            type = Column(String(50), nullable=False)

            @declared_attr
            def __tablename__(cls):
                return cls.__name__.lower()

            @declared_attr
            def __mapper_args__(cls):
                if cls.__name__ == 'Employee':
                    return {
                            "polymorphic_on":cls.type,
                            "polymorphic_identity":"Employee"
                    }
                else:
                    return {"polymorphic_identity":cls.__name__}

    To use :class:`_orm.declared_attr` inside of a Python dataclass
    as discussed at :ref:`orm_declarative_dataclasses_declarative_table`,
    it may be placed directly inside the field metadata using a lambda::

        @dataclass
        class AddressMixin:
            __sa_dataclass_metadata_key__ = "sa"

            user_id: int = field(
                init=False, metadata={"sa": declared_attr(lambda: Column(ForeignKey("user.id")))}
            )
            user: User = field(
                init=False, metadata={"sa": declared_attr(lambda: relationship(User))}
            )

    :class:`_orm.declared_attr` also may be omitted from this form using a
    lambda directly, as in::

        user: User = field(
            init=False, metadata={"sa": lambda: relationship(User)}
        )

    .. seealso::

        :ref:`orm_mixins_toplevel` - illustrates how to use Declarative Mixins
        which is the primary use case for :class:`_orm.declared_attr`

        :ref:`orm_declarative_dataclasses_mixin` - illustrates special forms
        for use with Python dataclasses

    c                 \    t         t        |   |       |j                  | _        || _        y r8   )superrM   r1   __doc__
_cascading)selffget	cascading	__class__s      r#   r1   zdeclared_attr.__init__   s$    mT+D1||#r%   c                    t        j                  |      }|tt        j                  d| j                  j
                        s9t        j                  d| j                  j
                  d|j
                         | j	                  |      S |j                  r| j	                  |      S |j                         }|J |j                  }| |v r||    S | j	                  |      x|| <   }|S )Nz^__.+__$z*Unmanaged access of declarative attribute z from non-mapped class )r   manager_of_classrematchrS   r?   r   warn	is_mappeddeclarative_scandeclared_attr_reg)descrR   r!   managerr\   r6   objs          r#   __get__zdeclared_attr.__get__   s     --c2?88K););< 		-1YY-?-?O 99S>! 99S>! #335+++003;t9"iin,CIJr%   c                     t        di |S NrB   )_stateful_declared_attr)r!   r5   s     r#   	_statefulzdeclared_attr._stateful   s    &,,,r%   c                 &    | j                  d      S )a  Mark a :class:`.declared_attr` as cascading.

        This is a special-use modifier which indicates that a column
        or MapperProperty-based declared attribute should be configured
        distinctly per mapped subclass, within a mapped-inheritance scenario.

        .. warning::

            The :attr:`.declared_attr.cascading` modifier has several
            limitations:

            * The flag **only** applies to the use of :class:`.declared_attr`
              on declarative mixin classes and ``__abstract__`` classes; it
              currently has no effect when used on a mapped class directly.

            * The flag **only** applies to normally-named attributes, e.g.
              not any special underscore attributes such as ``__tablename__``.
              On these attributes it has **no** effect.

            * The flag currently **does not allow further overrides** down
              the class hierarchy; if a subclass tries to override the
              attribute, a warning is emitted and the overridden attribute
              is skipped.  This is a limitation that it is hoped will be
              resolved at some point.

        Below, both MyClass as well as MySubClass will have a distinct
        ``id`` Column object established::

            class HasIdMixin(object):
                @declared_attr.cascading
                def id(cls):
                    if has_inherited_table(cls):
                        return Column(
                            ForeignKey('myclass.id'), primary_key=True
                        )
                    else:
                        return Column(Integer, primary_key=True)

            class MyClass(HasIdMixin, Base):
                __tablename__ = 'myclass'
                # ...

            class MySubClass(MyClass):
                ""
                # ...

        The behavior of the above configuration is that ``MySubClass``
        will refer to both its own ``id`` column as well as that of
        ``MyClass`` underneath the attribute named ``some_id``.

        .. seealso::

            :ref:`declarative_inheritance`

            :ref:`mixin_inheritance_columns`


        T)rT   )re   r!   s    r#   rT   zdeclared_attr.cascading   s    x }}t},,r%   F)r?   r@   rA   rP   r1   ra   r   re   r   rT   __classcell__)rU   s   @r#   rM   rM   ~   s<    Pd$
> - - ;- ;-r%   rM   c                       e Zd Zd Zd Zd Zy)rd   c                     || _         y r8   )r5   )rR   r5   s     r#   r1   z _stateful_declared_attr.__init__9  s	    r%   c                 n    | j                   j                         }|j                  |       t        di |S rc   )r5   copyupdaterd   )rR   r5   new_kws      r#   re   z!_stateful_declared_attr._stateful<  s+    b&000r%   c                 .    t        |fi | j                  S r8   )rM   r5   )rR   rH   s     r#   __call__z _stateful_declared_attr.__call__A  s    R+477++r%   N)r?   r@   rA   r1   re   rq   rB   r%   r#   rd   rd   8  s    1
,r%   rd   c                     | S )a,  Mark a class as providing the feature of "declarative mixin".

    E.g.::

        from sqlalchemy.orm import declared_attr
        from sqlalchemy.orm import declarative_mixin

        @declarative_mixin
        class MyMixin:

            @declared_attr
            def __tablename__(cls):
                return cls.__name__.lower()

            __table_args__ = {'mysql_engine': 'InnoDB'}
            __mapper_args__= {'always_refresh': True}

            id =  Column(Integer, primary_key=True)

        class MyModel(MyMixin, Base):
            name = Column(String(1000))

    The :func:`_orm.declarative_mixin` decorator currently does not modify
    the given class in any way; it's current purpose is strictly to assist
    the :ref:`Mypy plugin <mypy_toplevel>` in being able to identify
    SQLAlchemy declarative mixin classes when no other context is present.

    .. versionadded:: 1.4.6

    .. seealso::

        :ref:`orm_mixins_toplevel`

        :ref:`mypy_declarative_mixins` - in the
        :ref:`Mypy plugin documentation <mypy_toplevel>`

    rB   rg   s    r#   declarative_mixinrs   E  s
    N Jr%   Basec                 t    | t        j                  d       t        | |||      j                  ||||      S )aZ  Construct a base class for declarative class definitions.

    The new base class will be given a metaclass that produces
    appropriate :class:`~sqlalchemy.schema.Table` objects and makes
    the appropriate :func:`~sqlalchemy.orm.mapper` calls based on the
    information provided declaratively in the class and any subclasses
    of the class.

    The :func:`_orm.declarative_base` function is a shorthand version
    of using the :meth:`_orm.registry.generate_base`
    method.  That is, the following::

        from sqlalchemy.orm import declarative_base

        Base = declarative_base()

    Is equivalent to::

        from sqlalchemy.orm import registry

        mapper_registry = registry()
        Base = mapper_registry.generate_base()

    See the docstring for :class:`_orm.registry`
    and :meth:`_orm.registry.generate_base`
    for more details.

    .. versionchanged:: 1.4  The :func:`_orm.declarative_base`
       function is now a specialization of the more generic
       :class:`_orm.registry` class.  The function also moves to the
       ``sqlalchemy.orm`` package from the ``declarative.ext`` package.


    :param bind: An optional
      :class:`~sqlalchemy.engine.Connectable`, will be assigned
      the ``bind`` attribute on the :class:`~sqlalchemy.schema.MetaData`
      instance.

      .. deprecated:: 1.4  The "bind" argument to declarative_base is
         deprecated and will be removed in SQLAlchemy 2.0.

    :param metadata:
      An optional :class:`~sqlalchemy.schema.MetaData` instance.  All
      :class:`~sqlalchemy.schema.Table` objects implicitly declared by
      subclasses of the base will share this MetaData.  A MetaData instance
      will be created if none is provided.  The
      :class:`~sqlalchemy.schema.MetaData` instance will be available via the
      ``metadata`` attribute of the generated declarative base class.

    :param mapper:
      An optional callable, defaults to :func:`~sqlalchemy.orm.mapper`. Will
      be used to map subclasses to their Tables.

    :param cls:
      Defaults to :class:`object`. A type to use as the base for the generated
      declarative base class. May be a class or tuple of classes.

    :param name:
      Defaults to ``Base``.  The display name for the generated
      class.  Customizing this is not required, but can improve clarity in
      tracebacks and debugging.

    :param constructor:
      Specify the implementation for the ``__init__`` function on a mapped
      class that has no ``__init__`` of its own.  Defaults to an
      implementation that assigns \**kwargs for declared
      fields and relationships to an instance.  If ``None`` is supplied,
      no __init__ will be provided and construction will fall back to
      cls.__init__ by way of the normal Python semantics.

    :param class_registry: optional dictionary that will serve as the
      registry of class names-> mapped classes when string names
      are used to identify classes inside of :func:`_orm.relationship`
      and others.  Allows two or more declarative base classes
      to share the same registry of class names for simplified
      inter-base relationships.

    :param metaclass:
      Defaults to :class:`.DeclarativeMeta`.  A metaclass or __metaclass__
      compatible callable to use as the meta type of the generated
      declarative base class.

    .. seealso::

        :class:`_orm.registry`

    z^The ``bind`` argument to declarative_base is deprecated and will be removed in SQLAlchemy 2.0.)_bindmetadataclass_registryconstructor)r   r!   rI   	metaclass)r   warn_deprecated_20r*   generate_base)bindrw   r   r!   rI   ry   rx   rz   s           r#   declarative_baser~   o  sW    D @	

 %	
 m	  
r%   c                       e Zd ZdZddedfdZed        Zd Zd Z	e
d        Ze
d        Zd	 Zd
 Zd Zd ZddZddZd ZdedefdZd Zd Zd ZddZy)r*   a  Generalized registry for mapping classes.

    The :class:`_orm.registry` serves as the basis for maintaining a collection
    of mappings, and provides configurational hooks used to map classes.

    The three general kinds of mappings supported are Declarative Base,
    Declarative Decorator, and Imperative Mapping.   All of these mapping
    styles may be used interchangeably:

    * :meth:`_orm.registry.generate_base` returns a new declarative base
      class, and is the underlying implementation of the
      :func:`_orm.declarative_base` function.

    * :meth:`_orm.registry.mapped` provides a class decorator that will
      apply declarative mapping to a class without the use of a declarative
      base class.

    * :meth:`_orm.registry.map_imperatively` will produce a
      :class:`_orm.Mapper` for a class without scanning the class for
      declarative class attributes. This method suits the use case historically
      provided by the
      :func:`_orm.mapper` classical mapping function.

    .. versionadded:: 1.4

    .. seealso::

        :ref:`orm_mapping_classes_toplevel` - overview of class mapping
        styles.

    Nc                    |xs
 t               }|r||_        |t        j                         }|| _        t        j
                         | _        t        j
                         | _        || _        || _	        t               | _        t               | _        d| _        t        j                  5  dt        j                   | <   ddd       y# 1 sw Y   yxY w)a  Construct a new :class:`_orm.registry`

        :param metadata:
          An optional :class:`_schema.MetaData` instance.  All
          :class:`_schema.Table` objects generated using declarative
          table mapping will make use of this :class:`_schema.MetaData`
          collection.  If this argument is left at its default of ``None``,
          a blank :class:`_schema.MetaData` collection is created.

        :param constructor:
          Specify the implementation for the ``__init__`` function on a mapped
          class that has no ``__init__`` of its own.  Defaults to an
          implementation that assigns \**kwargs for declared
          fields and relationships to an instance.  If ``None`` is supplied,
          no __init__ will be provided and construction will fall back to
          cls.__init__ by way of the normal Python semantics.

        :param class_registry: optional dictionary that will serve as the
          registry of class names-> mapped classes when string names
          are used to identify classes inside of :func:`_orm.relationship`
          and others.  Allows two or more declarative base classes
          to share the same registry of class names for simplified
          inter-base relationships.

        NFT)r   r}   weakrefWeakValueDictionary_class_registryWeakKeyDictionary	_managers_non_primary_mappersrw   ry   set_dependents_dependencies_new_mappers	mapperlib_CONFIGURE_MUTEX_mapper_registries)rR   rw   rx   ry   rv   lcl_metadatas         r#   r1   zregistry.__init__  s    @  -8: %L!$88:N- 224$+$=$=$?!$&5 U!'' 	615I((.	6 	6 	6s   *CCc                 l    t        d | j                  D              j                  | j                        S )z9read only collection of all :class:`_orm.Mapper` objects.c              3   4   K   | ]  }|j                     y wr8   r
   .0r_   s     r#   	<genexpr>z#registry.mappers.<locals>.<genexpr>?  s     FGFs   )	frozensetr   unionr   rR   s    r#   mapperszregistry.mappers;  s/     Ft~~FFLL%%
 	
r%   c                 z    || u ry |j                   j                  |        | j                  j                  |       y r8   )r   addr   )rR   r*   s     r#   _set_depends_onzregistry._set_depends_onC  s5    t  &x(r%   c                 h    d|_         | j                  ry | j                  | h      D ]	  }d|_         y NT)_ready_for_configurer   _recurse_with_dependents)rR   r   r6   s      r#   _flag_new_mapperzregistry._flag_new_mapperI  s:    &*#00$8 	$C#C	$r%   c              #   "  K   |}t               }|r}|j                         }|j                  |       |j                  |j                  j                  |             | |j                  |j                  j                  |             |r|y y wr8   )r   popr   rn   r   
differencer!   
registriestododoner6   s        r#   r   z!registry._recurse_with_dependentsQ  so     u((*CHHSM KK22489I KK22489    B
BBc              #   "  K   |}t               }|r}|j                         }|j                  |       |j                  |j                  j                  |             | |j                  |j                  j                  |             |r|y y wr8   )r   r   r   rn   r   r   r   s        r#   _recurse_with_dependenciesz#registry._recurse_with_dependenciesb  ss     u((*CHHSM KK))44T:;I KK))44T:; r   c                     t        j                  d t        | j                        D        d t        | j                        D              S )Nc              3      K   | ]H  }|j                   r:|j                  j                  s$|j                  j                  r|j                   J y wr8   )r[   r   
configuredr   r   s     r#   r   z1registry._mappers_to_configure.<locals>.<genexpr>v  s?      $$11NN77	 s   AAc              3   P   K   | ]  }|j                   s|j                  r|   y wr8   )r   r   )r   npms     r#   r   z1registry._mappers_to_configure.<locals>.<genexpr>}  s'      ~~#*B*B s   $&)	itertoolschainlistr   r   r   s    r#   _mappers_to_configurezregistry._mappers_to_configuret  sA    #DNN3 9 9:
 	
r%   c                 "    d| j                   |<   y r   )r   )rR   	np_mappers     r#   _add_non_primary_mapperz registry._add_non_primary_mapper  s    /3!!),r%   c                 Z    t        j                  |j                  || j                         y r8   )r   remove_classr?   r   rR   r!   s     r#   _dispose_clszregistry._dispose_cls  s      sD4H4HIr%   c                     d| j                   |<   |j                  .|j                  r"t        j                  d|j
                  z        | |_        y )NTz1Class '%s' already has a primary mapper defined. )r   r*   r[   r   ArgumentErrorr"   )rR   r_   s     r#   _add_managerzregistry._add_manager  sP    "&w'G,=,=##C..!   r%   c                 4    t        j                  | h|       y)a]  Configure all as-yet unconfigured mappers in this
        :class:`_orm.registry`.

        The configure step is used to reconcile and initialize the
        :func:`_orm.relationship` linkages between mapped classes, as well as
        to invoke configuration events such as the
        :meth:`_orm.MapperEvents.before_configured` and
        :meth:`_orm.MapperEvents.after_configured`, which may be used by ORM
        extensions or user-defined extension hooks.

        If one or more mappers in this registry contain
        :func:`_orm.relationship` constructs that refer to mapped classes in
        other registries, this registry is said to be *dependent* on those
        registries. In order to configure those dependent registries
        automatically, the :paramref:`_orm.registry.configure.cascade` flag
        should be set to ``True``. Otherwise, if they are not configured, an
        exception will be raised.  The rationale behind this behavior is to
        allow an application to programmatically invoke configuration of
        registries while controlling whether or not the process implicitly
        reaches other registries.

        As an alternative to invoking :meth:`_orm.registry.configure`, the ORM
        function :func:`_orm.configure_mappers` function may be used to ensure
        configuration is complete for all :class:`_orm.registry` objects in
        memory. This is generally simpler to use and also predates the usage of
        :class:`_orm.registry` objects overall. However, this function will
        impact all mappings throughout the running Python process and may be
        more memory/time consuming for an application that has many registries
        in use for different purposes that may not be needed immediately.

        .. seealso::

            :func:`_orm.configure_mappers`


        .. versionadded:: 1.4.0b2

        cascadeN)r   _configure_registriesrR   r   s     r#   	configurezregistry.configure  s    N 	''@r%   c                 4    t        j                  | h|       y)a  Dispose of all mappers in this :class:`_orm.registry`.

        After invocation, all the classes that were mapped within this registry
        will no longer have class instrumentation associated with them. This
        method is the per-:class:`_orm.registry` analogue to the
        application-wide :func:`_orm.clear_mappers` function.

        If this registry contains mappers that are dependencies of other
        registries, typically via :func:`_orm.relationship` links, then those
        registries must be disposed as well. When such registries exist in
        relation to this one, their :meth:`_orm.registry.dispose` method will
        also be called, if the :paramref:`_orm.registry.dispose.cascade` flag
        is set to ``True``; otherwise, an error is raised if those registries
        were not already disposed.

        .. versionadded:: 1.4.0b2

        .. seealso::

            :func:`_orm.clear_mappers`

        r   N)r   _dispose_registriesr   s     r#   disposezregistry.dispose  s    0 	%%tfg>r%   c                     d|j                   v r|j                  }|j                          |j                  }| j	                  |       t
        j                  j                  |       y )Nr   )r,   r   _set_dispose_flagsr"   r   r   _instrumentation_factory
unregister)rR   r_   r   r"   s       r#   _dispose_manager_and_mapperz$registry._dispose_manager_and_mapper  sP    w'''^^F%%'&!00;;FCr%   rt   c                 0   | j                   }t        |t               xr |fxs |}t        | |      }t        |t              r|j
                  |d<   | j                  r| j                  |d<   d|d<   |r||d<   t        |d      rd }||d<    ||||      S )	aa  Generate a declarative base class.

        Classes that inherit from the returned class object will be
        automatically mapped using declarative mapping.

        E.g.::

            from sqlalchemy.orm import registry

            mapper_registry = registry()

            Base = mapper_registry.generate_base()

            class MyClass(Base):
                __tablename__ = "my_table"
                id = Column(Integer, primary_key=True)

        The above dynamically generated class is equivalent to the
        non-dynamic example below::

            from sqlalchemy.orm import registry
            from sqlalchemy.orm.decl_api import DeclarativeMeta

            mapper_registry = registry()

            class Base(metaclass=DeclarativeMeta):
                __abstract__ = True
                registry = mapper_registry
                metadata = mapper_registry.metadata

                __init__ = mapper_registry.constructor

        The :meth:`_orm.registry.generate_base` method provides the
        implementation for the :func:`_orm.declarative_base` function, which
        creates the :class:`_orm.registry` and base class all at once.

        See the section :ref:`orm_declarative_mapping` for background and
        examples.

        :param mapper:
          An optional callable, defaults to :func:`~sqlalchemy.orm.mapper`.
          This function is used to generate new :class:`_orm.Mapper` objects.

        :param cls:
          Defaults to :class:`object`. A type to use as the base for the
          generated declarative base class. May be a class or tuple of classes.

        :param name:
          Defaults to ``Base``.  The display name for the generated
          class.  Customizing this is not required, but can improve clarity in
          tracebacks and debugging.

        :param metaclass:
          Defaults to :class:`.DeclarativeMeta`.  A metaclass or __metaclass__
          compatible callable to use as the meta type of the generated
          declarative base class.

        .. seealso::

            :ref:`orm_declarative_mapping`

            :func:`_orm.declarative_base`

        )r*   rw   rP   r1   Tr+   __mapper_cls____class_getitem__c                     | S r8   rB   r=   s     r#   r   z1registry.generate_base.<locals>.__class_getitem__8  s    
r%   )rw   r.   tupledictr0   rP   ry   hasattr)	rR   r   r!   rI   rz   rw   r3   
class_dictr   s	            r#   r|   zregistry.generate_base  s    N ==sE**5v<4(;
c4 $'KKJy!%)%5%5Jz"%)
>"+1J'(3+, /@J*+uj11r%   c                 4    t        | ||j                         |S )aL  Class decorator that will apply the Declarative mapping process
        to a given class.

        E.g.::

            from sqlalchemy.orm import registry

            mapper_registry = registry()

            @mapper_registry.mapped
            class Foo:
                __tablename__ = 'some_table'

                id = Column(Integer, primary_key=True)
                name = Column(String)

        See the section :ref:`orm_declarative_mapping` for complete
        details and examples.

        :param cls: class to be mapped.

        :return: the class that was passed.

        .. seealso::

            :ref:`orm_declarative_mapping`

            :meth:`_orm.registry.generate_base` - generates a base class
            that will apply Declarative mapping to subclasses automatically
            using a Python metaclass.

        r   r,   r   s     r#   mappedzregistry.mapped@  s    B 	c3<<0
r%   c                       fd}|S )a  
        Class decorator which will invoke
        :meth:`_orm.registry.generate_base`
        for a given base class.

        E.g.::

            from sqlalchemy.orm import registry

            mapper_registry = registry()

            @mapper_registry.as_declarative_base()
            class Base(object):
                @declared_attr
                def __tablename__(cls):
                    return cls.__name__.lower()
                id = Column(Integer, primary_key=True)

            class MyMappedClass(Base):
                # ...

        All keyword arguments passed to
        :meth:`_orm.registry.as_declarative_base` are passed
        along to :meth:`_orm.registry.generate_base`.

        c                 P    | d<   | j                   d<    j                  di S )Nr!   rI   rB   )r?   r|   )r!   r5   rR   s    r#   rJ   z.registry.as_declarative_base.<locals>.decorate  s/    BuIBvJ%4%%+++r%   rB   )rR   r5   rJ   s   `` r#   as_declarative_basezregistry.as_declarative_based  s    8	,
 r%   c                 0    t        | ||j                        S )a  Map a class declaratively.

        In this form of mapping, the class is scanned for mapping information,
        including for columns to be associated with a table, and/or an
        actual table object.

        Returns the :class:`_orm.Mapper` object.

        E.g.::

            from sqlalchemy.orm import registry

            mapper_registry = registry()

            class Foo:
                __tablename__ = 'some_table'

                id = Column(Integer, primary_key=True)
                name = Column(String)

            mapper = mapper_registry.map_declaratively(Foo)

        This function is more conveniently invoked indirectly via either the
        :meth:`_orm.registry.mapped` class decorator or by subclassing a
        declarative metaclass generated from
        :meth:`_orm.registry.generate_base`.

        See the section :ref:`orm_declarative_mapping` for complete
        details and examples.

        :param cls: class to be mapped.

        :return: a :class:`_orm.Mapper` object.

        .. seealso::

            :ref:`orm_declarative_mapping`

            :meth:`_orm.registry.mapped` - more common decorator interface
            to this function.

            :meth:`_orm.registry.map_imperatively`

        r   r   s     r#   map_declarativelyzregistry.map_declaratively  s    Z tS#,,77r%   c                     t        | |||      S )a  Map a class imperatively.

        In this form of mapping, the class is not scanned for any mapping
        information.  Instead, all mapping constructs are passed as
        arguments.

        This method is intended to be fully equivalent to the classic
        SQLAlchemy :func:`_orm.mapper` function, except that it's in terms of
        a particular registry.

        E.g.::

            from sqlalchemy.orm import registry

            mapper_registry = registry()

            my_table = Table(
                "my_table",
                mapper_registry.metadata,
                Column('id', Integer, primary_key=True)
            )

            class MyClass:
                pass

            mapper_registry.map_imperatively(MyClass, my_table)

        See the section :ref:`orm_imperative_mapping` for complete background
        and usage examples.

        :param class\_: The class to be mapped.  Corresponds to the
         :paramref:`_orm.mapper.class_` parameter.

        :param local_table: the :class:`_schema.Table` or other
         :class:`_sql.FromClause` object that is the subject of the mapping.
         Corresponds to the
         :paramref:`_orm.mapper.local_table` parameter.

        :param \**kw: all other keyword arguments are passed to the
         :func:`_orm.mapper` function directly.

        .. seealso::

            :ref:`orm_imperative_mapping`

            :ref:`orm_declarative_mapping`

        r   )rR   r"   local_tabler5   s       r#   map_imperativelyzregistry.map_imperatively  s    b tV["55r%   rh   r8   )r?   r@   rA   rP   r   r1   propertyr   r   r   classmethodr   r   r   r   r   r   r   r   r   objectr'   r|   r   r   r   r   rB   r%   r#   r*   r*     s    D ,36j 
 
)$ : :  < <"
 4J 'AR?4D !^2@"H!F-8^16r%   r*   )z2.0z\The ``bind`` argument to as_declarative is deprecated and will be removed in SQLAlchemy 2.0.)r}   c                      | j                  dd      | j                  dd      | j                  dd      }}} t        |||      j                  di | S )a  
    Class decorator which will adapt a given class into a
    :func:`_orm.declarative_base`.

    This function makes use of the :meth:`_orm.registry.as_declarative_base`
    method, by first creating a :class:`_orm.registry` automatically
    and then invoking the decorator.

    E.g.::

        from sqlalchemy.orm import as_declarative

        @as_declarative()
        class Base(object):
            @declared_attr
            def __tablename__(cls):
                return cls.__name__.lower()
            id = Column(Integer, primary_key=True)

        class MyMappedClass(Base):
            # ...

    .. seealso::

        :meth:`_orm.registry.as_declarative_base`

    r}   Nrw   rx   )rv   rw   rx   rB   )r   r*   r   )r5   r}   rw   rx   s       r#   as_declarativer     sd    H 	vt
z4 
& #(D8Xn    r%   c                     t        |       }|Wt        j                  |       rBt        j                  |        t	        j
                  | dt	        j                  |       z        |S )NzOClass %s has a deferred mapping on it.  It is not yet usable as a mapped class.)msg)r   r   has_clsraise_unmapped_for_clsorm_excUnmappedClassError_safe_cls_name)r!   mps     r#   _inspect_decl_metar     sc    	s	#B	z ((-!88=,,,.5.D.DS.IJ 
 Ir%   rh   )2rP   
__future__r   r   rX   r    r   r   r   r   r   r	   r   r   baser   	decl_baser   r   r   r   r   r   descriptor_propsr   rG   r   r   
sql.schemar   r   r   r$   r0   r'   rK   _MappedAttributer   rM   rd   rs   r   r~   r*   _legacy_registrydeprecated_paramsr   	_inspectsr   rB   r%   r#   <module>r      s   8 &  	       ! ' % & / , %  =    !  !&!d !>%Pw-J// w-t
,m 
,'V 
	(slB6v B6J &Z	  
$ $ N o&
 '
r%   