ó
H`¾Tc        	   @   s›  d  Z  d d l m Z m Z d d l m Z m Z m Z m	 Z	 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 Z d
 d l m Z m Z m Z m Z d
 d l m Z d
 d l m Z d „  Z d „  Z d e f d „  ƒ  YZ e d „ Z  d „  Z! d e	 j" e# f d „  ƒ  YZ$ d	 d	 d	 e& d e d	 e d „ Z' d „  Z( d e& f d „  ƒ  YZ) d e) f d „  ƒ  YZ* d e& f d „  ƒ  YZ+ d	 S(   s1   Public API functions and helpers for declarative.i   (   t   Tablet   MetaData(   t   synonymt   mappert   comparable_propertyt
   interfacest
   properties(   t   polymorphic_union(   t   _mapper_or_none(   t   OrderedDict(   t   exciÿÿÿÿNi   (   t   _as_declarativet   _declarative_constructort   _DeferredMapperConfigt   _add_attribute(   t   _class_resolver(   t   clsregistryc         C   sQ   d |  j  k r% t j d |  ƒ ‚ n  | |  _ | |  _ t |  |  j |  j  ƒ d S(   s‹   Given a class, configure the class declaratively,
    using the given registry, which can be any dictionary, and
    MetaData object.

    t   _decl_class_registrys4   Class %r already has been instrumented declarativelyN(   t   __dict__R
   t   InvalidRequestErrorR   t   metadataR   t   __name__(   t   clst   registryR   (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   instrument_declarative   s    		c         C   s8   x1 |  j  d D]" } t | d d ƒ d k	 r t Sq Wt S(   sv   Given a class, return True if any of the classes it inherits from has a
    mapped table, otherwise return False.
    i   t	   __table__N(   t   __mro__t   getattrt   Nonet   Truet   False(   R   t   class_(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   has_inherited_table*   s    t   DeclarativeMetac           B   s   e  Z d  „  Z d „  Z RS(   c         C   s?   d |  j  k r% t |  | |  j  ƒ n  t j |  | | | ƒ d  S(   NR   (   R   R   t   typet   __init__(   R   t	   classnamet   basest   dict_(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR#   5   s    c         C   s   t  |  | | ƒ d  S(   N(   R   (   R   t   keyt   value(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   __setattr__:   s    (   R   t
   __module__R#   R)   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR!   4   s   	c            s   ‡  ‡ f d †  } | S(   s&  Decorator, make a Python @property a query synonym for a column.

    A decorator version of :func:`~sqlalchemy.orm.synonym`. The function being
    decorated is the 'descriptor', otherwise passes its arguments through to
    synonym()::

      @synonym_for('col')
      @property
      def prop(self):
          return 'special sauce'

    The regular ``synonym()`` is also usable directly in a declarative setting
    and may be convenient for read/write properties::

      prop = synonym('col', descriptor=property(_read_prop, _write_prop))

    c            s   t  ˆ d ˆ  d |  ƒS(   Nt
   map_columnt
   descriptor(   t   _orm_synonym(   t   fn(   R+   t   name(    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   decorateP   s    (    (   R/   R+   R0   (    (   R+   R/   sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   synonym_for>   s    c            s   ‡  f d †  } | S(   s(  Decorator, allow a Python @property to be used in query criteria.

    This is a  decorator front end to
    :func:`~sqlalchemy.orm.comparable_property` that passes
    through the comparator_factory and the function being decorated::

      @comparable_using(MyComparatorType)
      @property
      def prop(self):
          return 'special sauce'

    The regular ``comparable_property()`` is also usable directly in a
    declarative setting and may be convenient for read/write properties::

      prop = comparable_property(MyComparatorType)

    c            s   t  ˆ  |  ƒ S(   N(   R   (   R.   (   t   comparator_factory(    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR0   g   s    (    (   R2   R0   (    (   R2   sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   comparable_usingU   s    t   declared_attrc           B   s    e  Z d  Z d „  Z d „  Z RS(   sJ  Mark a class-level method as representing the definition of
    a mapped property or special declarative member name.

    @declared_attr turns the attribute into a scalar-like
    property that can be invoked from the uninstantiated class.
    Declarative treats attributes specifically marked with
    @declared_attr as returning a construct that is specific
    to mapping or declarative table configuration.  The name
    of the attribute is that of what the non-dynamic version
    of the attribute would be.

    @declared_attr is more often than not applicable to mixins,
    to define relationships that are to be applied to different
    implementors of the class::

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

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

    It also can 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__}

    .. versionchanged:: 0.8 :class:`.declared_attr` can be used with
       non-ORM or extension attributes, such as user-defined attributes
       or :func:`.association_proxy` objects, which will be assigned
       to the class at class construction time.


    c         O   s,   t  t |  ƒ j | | | Ž | j |  _ d  S(   N(   t   superR4   R#   t   __doc__(   t   selft   fgett   argt   kw(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR#       s    c         C   s   |  j  | ƒ S(   N(   R8   (   t   descR7   R   (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   __get__¤   s    (   R   R*   R6   R#   R<   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR4   l   s   2	t   Basec         C   s¦   | p t  ƒ  } |  r! |  | _ n  | d k r< t j ƒ  } n  t | t ƒ rU | f pX | }	 t d | d | ƒ }
 | rƒ | |
 d <n  | r– | |
 d <n  | | |	 |
 ƒ S(   s—	  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.

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

    :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:
      Defaults to
      :func:`~sqlalchemy.ext.declarative._declarative_constructor`, an
      __init__ 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:`.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::

        :func:`.as_declarative`

    R   R   R#   t   __mapper_cls__N(   R   t   bindR   t   weakreft   WeakValueDictionaryt
   isinstancet   tuplet   dict(   R?   R   R   R   R/   t   constructort   class_registryt	   metaclasst   lcl_metadataR%   t
   class_dict(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   declarative_base¨   s    ?	c             s   ‡  f d †  } | S(   sø  
    Class decorator for :func:`.declarative_base`.

    Provides a syntactical shortcut to the ``cls`` argument
    sent to :func:`.declarative_base`, allowing the base class
    to be converted in-place to a "declarative" base::

        from sqlalchemy.ext.declarative 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):
            # ...

    All keyword arguments passed to :func:`.as_declarative` are passed
    along to :func:`.declarative_base`.

    .. versionadded:: 0.8.3

    .. seealso::

        :func:`.declarative_base`

    c            s!   |  ˆ  d <|  j  ˆ  d <t ˆ    S(   NR   R/   (   R   RJ   (   R   (   R:   (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR0     s    
(    (   R:   R0   (    (   R:   sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   as_declarativeú   s    t   ConcreteBasec           B   s,   e  Z d  Z e d „  ƒ Z e d „  ƒ Z RS(   sâ  A helper class for 'concrete' declarative mappings.

    :class:`.ConcreteBase` will use the :func:`.polymorphic_union`
    function automatically, against all tables mapped as a subclass
    to this class.   The function is called via the
    ``__declare_last__()`` function, which is essentially
    a hook for the :meth:`.after_configured` event.

    :class:`.ConcreteBase` produces a mapped
    table for the class itself.  Compare to :class:`.AbstractConcreteBase`,
    which does not.

    Example::

        from sqlalchemy.ext.declarative import ConcreteBase

        class Employee(ConcreteBase, Base):
            __tablename__ = 'employee'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            __mapper_args__ = {
                            'polymorphic_identity':'employee',
                            'concrete':True}

        class Manager(Employee):
            __tablename__ = 'manager'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            manager_data = Column(String(40))
            __mapper_args__ = {
                            'polymorphic_identity':'manager',
                            'concrete':True}

    c         C   s    t  t d „  | Dƒ ƒ d d ƒ S(   Nc         s   s!   |  ] } | j  | j f Vq d  S(   N(   t   polymorphic_identityt   local_table(   t   .0t   mp(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pys	   <genexpr>G  s   R"   t   pjoin(   R   R	   (   R   t   mappers(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   _create_polymorphic_unionD  s    
c         C   s^   |  j  } | j r d  St | j ƒ } |  j | ƒ } | j d | f ƒ | j | j j ƒ d  S(   Nt   *(	   t
   __mapper__t   with_polymorphict   listt   self_and_descendantsRS   t   _set_with_polymorphict   _set_polymorphic_ont   cR"   (   R   t   mRR   RQ   (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   __declare_first__K  s    		(   R   R*   R6   t   classmethodRS   R]   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyRL      s   "t   AbstractConcreteBasec           B   s#   e  Z d  Z e Z e d „  ƒ Z RS(   sú  A helper class for 'concrete' declarative mappings.

    :class:`.AbstractConcreteBase` will use the :func:`.polymorphic_union`
    function automatically, against all tables mapped as a subclass
    to this class.   The function is called via the
    ``__declare_last__()`` function, which is essentially
    a hook for the :meth:`.after_configured` event.

    :class:`.AbstractConcreteBase` does not produce a mapped
    table for the class itself.  Compare to :class:`.ConcreteBase`,
    which does.

    Example::

        from sqlalchemy.ext.declarative import AbstractConcreteBase

        class Employee(AbstractConcreteBase, Base):
            pass

        class Manager(Employee):
            __tablename__ = 'manager'
            employee_id = Column(Integer, primary_key=True)
            name = Column(String(50))
            manager_data = Column(String(40))
            __mapper_args__ = {
                            'polymorphic_identity':'manager',
                            'concrete':True}

    c   	      C   s  t  |  d ƒ r d  St j |  j |  ƒ g  } t |  j ƒ  ƒ } xQ | r‘ | j ƒ  } | j | j ƒ  ƒ t | ƒ } | d  k	 rA | j
 | ƒ qA qA W|  j | ƒ } t |  | d | j j ƒ|  _ } xN |  j ƒ  D]@ } t | ƒ } | rÐ | j rÐ |  | j k rÐ | j | ƒ qÐ qÐ Wd  S(   NRU   t   polymorphic_on(   t   hasattrR   t	   add_classR   RW   t   __subclasses__t   popt   extendR   R   t   appendRS   R   R[   R"   RU   t   concretet	   __bases__t   _set_concrete_base(	   R   RR   t   stackt   klasst   mnRQ   R\   t   sclst   sm(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR]   x  s"    	"(   R   R*   R6   R   t   __abstract__R^   R]   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyR_   W  s   t   DeferredReflectionc           B   sJ   e  Z d  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   s¯	  A helper class for construction of mappings based on
    a deferred reflection step.

    Normally, declarative can be used with reflection by
    setting a :class:`.Table` object using autoload=True
    as the ``__table__`` attribute on a declarative class.
    The caveat is that the :class:`.Table` must be fully
    reflected, or at the very least have a primary key column,
    at the point at which a normal declarative mapping is
    constructed, meaning the :class:`.Engine` must be available
    at class declaration time.

    The :class:`.DeferredReflection` mixin moves the construction
    of mappers to be at a later point, after a specific
    method is called which first reflects all :class:`.Table`
    objects created so far.   Classes can define it as such::

        from sqlalchemy.ext.declarative import declarative_base
        from sqlalchemy.ext.declarative import DeferredReflection
        Base = declarative_base()

        class MyClass(DeferredReflection, Base):
            __tablename__ = 'mytable'

    Above, ``MyClass`` is not yet mapped.   After a series of
    classes have been defined in the above fashion, all tables
    can be reflected and mappings created using
    :meth:`.prepare`::

        engine = create_engine("someengine://...")
        DeferredReflection.prepare(engine)

    The :class:`.DeferredReflection` mixin can be applied to individual
    classes, used as the base for the declarative base itself,
    or used in a custom abstract class.   Using an abstract base
    allows that only a subset of classes to be prepared for a
    particular prepare step, which is necessary for applications
    that use more than one engine.  For example, if an application
    has two engines, you might use two bases, and prepare each
    separately, e.g.::

        class ReflectedOne(DeferredReflection, Base):
            __abstract__ = True

        class ReflectedTwo(DeferredReflection, Base):
            __abstract__ = True

        class MyClass(ReflectedOne):
            __tablename__ = 'mytable'

        class MyOtherClass(ReflectedOne):
            __tablename__ = 'myothertable'

        class YetAnotherClass(ReflectedTwo):
            __tablename__ = 'yetanothertable'

        # ... etc.

    Above, the class hierarchies for ``ReflectedOne`` and
    ``ReflectedTwo`` can be configured separately::

        ReflectedOne.prepare(engine_one)
        ReflectedTwo.prepare(engine_two)

    .. versionadded:: 0.8

    c         C   sõ   t  j |  ƒ } xß | D]× } |  j | j | ƒ | j ƒ  | j j } | j j } x™ | j	 j
 ƒ  D]ˆ } t | t j ƒ ra | j d k	 ra t | j t ƒ r° |  j | j | ƒ qé t | j t ƒ ré | j j |  j | | ƒ f 7_ qé qa qa Wq Wd S(   sc   Reflect all :class:`.Table` objects for all current
        :class:`.DeferredReflection` subclassesN(   R   t   classes_for_baset   _sa_decl_prepareRN   t   mapR   RU   R   R   t   _propst   valuesRB   R   t   RelationshipPropertyt	   secondaryR   R    t   _reflect_tableR   t
   _resolverst   _sa_deferred_table_resolver(   R   t   enginet   to_mapt   thingyR   R   t   rel(    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   prepare×  s    

c            s   ‡  ‡ ‡ f d †  } | S(   Nc            s#   t  |  ˆ ƒ } ˆ j | ˆ  ƒ | S(   N(   R    Rx   (   R'   t   t1(   R{   R   R   (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   _resolveî  s    (    (   R   R{   R   R   (    (   R{   R   R   sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyRz   ì  s    c         C   s#   | d  k	 r |  j | | ƒ n  d  S(   N(   R   Rx   (   R   RN   R{   (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyRr   ô  s    c         C   s8   t  | j | j d t d t d t d | d | j ƒd  S(   Nt   extend_existingt   autoload_replacet   autoloadt   autoload_witht   schema(   R    R/   R   R   R   R†   (   R   t   tableR{   (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyRx   ý  s    		(   R   R*   R6   R^   R   Rz   Rr   Rx   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyRp   “  s
   C	(,   R6   R†   R    R   t   ormR   R-   R   R   R   R   t   orm.utilR   t   orm.baseR   t   utilR	   t    R
   R@   t   baseR   R   R   R   R   R   R   R    R"   R!   R   R1   R3   t   _MappedAttributet   propertyR4   R   t   objectRJ   RK   RL   R_   Rp   (    (    (    sd   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.pyt   <module>   s0   ("		

	<O	&7<