
G`Tc           @  sh  d  Z  d d l m Z m Z d d l Z d d l Z d d l 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 m Z d d l m Z d d l m Z d d	 l m Z d d
 l	 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! m" Z" m# Z# d d l$ m% Z% m& Z& m' Z' m( Z( e j) d k rse j* Z+ n	 e j Z+ y d d l m, Z, Wn e- k
 rd Z, n Xd Z/ e, pe Z0 e j1 d  Z2 e   Z3 e3 j4 d  Z5 e3 j4 d  Z6 d   Z7 d   Z8 d   Z9 d   Z: d e; f d     YZ< d   Z= d e f d     YZ> d  e? f d!     YZ@ d" e? f d#     YZA d$ e? f d%     YZB d&   ZC d' e? f d(     YZD d) e jE f d*     YZF d+ e? f d,     YZG d-   ZH d. e? f d/     YZI d0   ZJ d1 e# f d2     YZK d3   ZL d4 e? f d5     YZM d6 e? f d7     YZN d8 e? f d9     YZO d S(:   s   
    flaskext.sqlalchemy
    ~~~~~~~~~~~~~~~~~~~

    Adds basic SQLAlchemy support to your application.

    :copyright: (c) 2014 by Armin Ronacher, Daniel Neuhäuser.
    :license: BSD, see LICENSE for more details.
i    (   t   with_statementt   absolute_importN(   t   ceil(   t   partial(   t   _request_ctx_stackt   abort(   t	   Namespace(   t
   itemgetter(   t   Lock(   t   ormt   event(   t   UnmappedClassError(   t   Session(   t   listen(   t   make_url(   t   declarative_baset   DeclarativeMeta(   t	   iteritemst
   itervaluest   xranget   string_typest   win32(   t   _app_ctx_stacks   2.0s   ([A-Z]+)(?=[a-z0-9])s   models-committeds   before-models-committedc           s     f d   } | S(   Nc            s   t  |   d k rF t |  d   j  rF |  d   j f |  d }  n  | j d d   p[ i  } | j d d   | | d <t j |  |   S(   Ni   i    t   infot   bind_key(	   t   lent
   isinstancet   Columnt   metadatat   popt   Nonet
   setdefaultt
   sqlalchemyt   Table(   t   argst   kwargsR   (   t   db(    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _make_table=   s    (
(    (   R$   R%   (    (   R$   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR%   <   s    c         C  s   d |  k r t  |  d <n  d  S(   Nt   query_class(   t	   BaseQuery(   t   d(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _set_default_query_classG   s    c           s"   t  j      f d    } | S(   Nc            s\   t  |  d | k rO | d } t | t  r> | i  f } n  t  | d  n    |  |   S(   Nt   backrefi   (   R)   R   R   (   R"   R#   R*   (   t   fn(    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   newfnM   s    

(   t	   functoolst   wraps(   R+   R,   (    (   R+   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _wrap_with_default_query_classL   s    	c         C  s   xV t  t  j f D]E } x< | j D]1 } t |  |  s  t |  | t | |   q  q  Wq Wt |   |  _ t |  j	  |  _	 t |  j
  |  _
 t |  j  |  _ t |  _ d  S(   N(   R    R	   t   __all__t   hasattrt   setattrt   getattrR%   R!   R/   t   relationshipt   relationt   dynamic_loaderR
   (   t   objt   modulet   key(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _include_sqlalchemyY   s    $t   _DebugQueryTuplec           B  sz   e  Z e e d    Z e e d   Z e e d   Z e e d   Z e e d   Z e d    Z	 d   Z
 RS(   i    i   i   i   i   c         C  s   |  j  |  j S(   N(   t   end_timet
   start_time(   t   self(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   durationm   s    c         C  s   d |  j  |  j |  j f S(   Ns3   <query statement="%s" parameters=%r duration=%.03f>(   t	   statementt
   parametersR?   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   __repr__q   s    (   t   __name__t
   __module__t   propertyR   R@   RA   R=   R<   t   contextR?   RB   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR;   f   s   c         C  s   t  j d  } xy | j d  k	 r | j j d  } | r~ | |  k sX | j |  d  r~ | j j } d | j j	 | j
 | f S| j } q Wd S(   Ni   RC   t   .s
   %s:%s (%s)s	   <unknown>(   t   syst	   _getframet   f_backR   t	   f_globalst   gett
   startswitht   f_codet   co_namet   co_filenamet   f_lineno(   t   app_patht   frmt   namet   funcname(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _calling_contexty   s    %	t   SignallingSessionc           B  s)   e  Z d  Z e e d  Z d d  Z RS(   s8  The signalling session is the default session that Flask-SQLAlchemy
    uses.  It extends the default session system with bind selection and
    modification tracking.

    If you want to use a different session you can override the
    :meth:`SQLAlchemy.create_session` function.

    .. versionadded:: 2.0
    c         K  s~   | j    |  _ i  |  _ |  j j d |  _ | j d d   pC | j } t j	 |  d | d | d | d | j
 |  j  | d  S(   Nt   SQLALCHEMY_TRACK_MODIFICATIONSt   bindt
   autocommitt	   autoflusht   binds(   t   get_appt   appt   _model_changest   configt   emit_modification_signalsR   R   t   enginet   SessionBaset   __init__t	   get_binds(   R>   R$   RZ   R[   t   optionsRY   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd      s    	c         C  sz   | d  k	 rg t | j d i   } | j d  } | d  k	 rg t |  j  } | j j |  j d | Sn  t j	 |  | |  S(   NR   R   RY   (
   R   R3   t   mapped_tableRL   t	   get_stateR^   R$   t
   get_engineRc   t   get_bind(   R>   t   mappert   clauseR   R   t   state(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRj      s    N(   RC   RD   t   __doc__t   Falset   TrueRd   R   Rj   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRW      s   	t   _SessionSignalEventsc           B  s>   e  Z d    Z e d    Z e d    Z e d    Z RS(   c         C  s=   t  t d |  j  t  t d |  j  t  t d |  j  d  S(   Nt   before_committ   after_committ   after_rollback(   R   Rc   t   session_signal_before_committ   session_signal_after_committ   session_signal_after_rollback(   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   register   s    c         C  sE   t  |  t  s d  S|  j } | rA t j |  j d | j   n  d  S(   Nt   changes(   R   RW   R_   t   before_models_committedt   sendR^   t   values(   t   sessionR(   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRu      s
    	c         C  sU   t  |  t  s d  S|  j } | rQ t j |  j d t | j    | j   n  d  S(   NRy   (	   R   RW   R_   t   models_committedR{   R^   t   listR|   t   clear(   R}   R(   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRv      s    	"c         C  s$   t  |  t  s d  S|  j j   d  S(   N(   R   RW   R_   R   (   R}   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRw      s    (   RC   RD   Rx   t   staticmethodRu   Rv   Rw   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRq      s   		t   _MapperSignalEventsc           B  sD   e  Z d    Z d   Z d   Z d   Z d   Z e d    Z RS(   c         C  s   | |  _  d  S(   N(   Rk   (   R>   Rk   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd      s    c         C  sF   t  |  j d |  j  t  |  j d |  j  t  |  j d |  j  d  S(   Nt   after_deletet   after_insertt   after_update(   R   Rk   t   mapper_signal_after_deletet   mapper_signal_after_insertt   mapper_signal_after_update(   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRx      s    c         C  s   |  j  | | d  d  S(   Nt   delete(   t   _record(   R>   Rk   t
   connectiont   target(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    c         C  s   |  j  | | d  d  S(   Nt   insert(   R   (   R>   Rk   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    c         C  s   |  j  | | d  d  S(   Nt   update(   R   (   R>   Rk   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    c         C  sV   t  j |  } t | t  rR | j rR t |  j |   } | | f | j | <n  d  S(   N(   R	   t   object_sessionR   RW   Ra   t   tuplet   primary_key_from_instanceR_   (   Rk   R   t	   operationt   st   pk(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    (	   RC   RD   Rd   Rx   R   R   R   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s   					t   _EngineDebuggingSignalEventsc           B  s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sP   Sets up handlers for two events that let us track the execution time of queries.c         C  s   | |  _  | |  _ d  S(   N(   Rb   t   app_package(   R>   Rb   t   import_name(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd      s    	c         C  s0   t  |  j d |  j  t  |  j d |  j  d  S(   Nt   before_cursor_executet   after_cursor_execute(   R   Rb   R   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRx      s    c         C  s"   t  j d  k	 r t   | _ n  d  S(   N(   t   connection_stackt   topR   t   _timert   _query_start_time(   R>   t   connt   cursorR@   RA   RF   t   executemany(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    c   	      C  s   t  j } | d  k	 r t | d d   } | d  k rL g  } t | d |  n  | j t | | | j t   t	 |  j
  f   n  d  S(   Nt   sqlalchemy_queries(   R   R   R   R3   R2   t   appendR;   R   R   RV   R   (	   R>   R   R   R@   RA   RF   R   t   ctxt   queries(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s    		(   RC   RD   Rn   Rd   Rx   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR      s
   			c           C  s   t  t j d g   S(   s  In debug mode Flask-SQLAlchemy will log all the SQL queries sent
    to the database.  This information is available until the end of request
    which makes it possible to easily ensure that the SQL generated is the
    one expected on errors or in unittesting.  If you don't want to enable
    the DEBUG mode for your unittests you can also enable the query
    recording by setting the ``'SQLALCHEMY_RECORD_QUERIES'`` config variable
    to `True`.  This is automatically enabled if Flask is in testing mode.

    The value returned will be a list of named tuples with the following
    attributes:

    `statement`
        The SQL statement issued

    `parameters`
        The parameters for the SQL statement

    `start_time` / `end_time`
        Time the query started / the results arrived.  Please keep in mind
        that the timer function used depends on your platform. These
        values are only useful for sorting or comparing.  They do not
        necessarily represent an absolute timestamp.

    `duration`
        Time the query took in seconds

    `context`
        A string giving a rough estimation of where in your application
        query was issued.  The exact format is undefined so don't try
        to reconstruct filename or function name.
    R   (   R3   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   get_debug_queries  s     t
   Paginationc           B  s   e  Z d  Z d   Z e d    Z e d  Z e d    Z e d    Z	 e d  Z
 e d    Z e d    Z d	 d	 d
 d	 d  Z RS(   s>  Internal helper class returned by :meth:`BaseQuery.paginate`.  You
    can also construct it from any other SQLAlchemy query object if you are
    working with other libraries.  Additionally it is possible to pass `None`
    as query object in which case the :meth:`prev` and :meth:`next` will
    no longer work.
    c         C  s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   queryt   paget   per_paget   totalt   items(   R>   R   R   R   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd   0  s
    				c         C  s>   |  j  d k r d } n" t t |  j t |  j     } | S(   s   The total number of pagesi    (   R   t   intR   R   t   float(   R>   t   pages(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR   =  s    	"c         C  s;   |  j  d k	 s t d   |  j  j |  j d |  j |  S(   s;   Returns a :class:`Pagination` object for the previous page.s2   a query object is required for this method to worki   N(   R   R   t   AssertionErrort   paginateR   R   (   R>   t	   error_out(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   prevF  s    c         C  s   |  j  d S(   s   Number of the previous page.i   (   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   prev_numL  s    c         C  s   |  j  d k S(   s   True if a previous page existsi   (   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   has_prevQ  s    c         C  s;   |  j  d k	 s t d   |  j  j |  j d |  j |  S(   s7   Returns a :class:`Pagination` object for the next page.s2   a query object is required for this method to worki   N(   R   R   R   R   R   R   (   R>   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   nextV  s    c         C  s   |  j  |  j k  S(   s   True if a next page exists.(   R   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   has_next\  s    c         C  s   |  j  d S(   s   Number of the next pagei   (   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   next_numa  s    i   i   c         c  s   d } x t  d |  j d  D]u } | | k sl | |  j | d k rY | |  j | k  sl | |  j | k r | d | k r d Vn  | V| } q q Wd S(   s  Iterates over the page numbers in the pagination.  The four
        parameters control the thresholds how many numbers should be produced
        from the sides.  Skipped page numbers are represented as `None`.
        This is how you could render such a pagination in the templates:

        .. sourcecode:: html+jinja

            {% macro render_pagination(pagination, endpoint) %}
              <div class=pagination>
              {%- for page in pagination.iter_pages() %}
                {% if page %}
                  {% if page != pagination.page %}
                    <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
                  {% else %}
                    <strong>{{ page }}</strong>
                  {% endif %}
                {% else %}
                  <span class=ellipsis>…</span>
                {% endif %}
              {%- endfor %}
              </div>
            {% endmacro %}
        i    i   N(   R   R   R   R   (   R>   t	   left_edget   left_currentt   right_currentt
   right_edget   lastt   num(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt
   iter_pagesf  s    (   RC   RD   Rn   Rd   RE   R   Ro   R   R   R   R   R   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR   (  s   		R'   c           B  s/   e  Z d  Z d   Z d   Z d e d  Z RS(   sh  The default query object used for models, and exposed as
    :attr:`~SQLAlchemy.Query`. This can be subclassed and
    replaced for individual models by setting the :attr:`~Model.query_class`
    attribute.  This is a subclass of a standard SQLAlchemy
    :class:`~sqlalchemy.orm.query.Query` class and has all the methods of a
    standard query as well.
    c         C  s,   |  j  |  } | d k r( t d  n  | S(   s_   Like :meth:`get` but aborts with 404 if not found instead of
        returning `None`.
        i  N(   RL   R   R   (   R>   t   identt   rv(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt
   get_or_404  s    c         C  s)   |  j    } | d k r% t d  n  | S(   sa   Like :meth:`first` but aborts with 404 if not found instead of
        returning `None`.
        i  N(   t   firstR   R   (   R>   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   first_or_404  s    i   c      
   C  s   | r | d k  r t  d  n  |  j |  j | d |  j   } | rk | d k rk | rk t  d  n  | d k r t |  | k  r t |  } n |  j d  j   } t |  | | | |  S(   s  Returns `per_page` items from page `page`.  By default it will
        abort with 404 if no items were found and the page was larger than
        1.  This behavor can be disabled by setting `error_out` to `False`.

        Returns an :class:`Pagination` object.
        i   i  N(	   R   t   limitt   offsett   allR   t   order_byR   t   countR   (   R>   R   R   R   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s    &(   RC   RD   Rn   R   R   Rp   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR'     s   				t   _QueryPropertyc           B  s   e  Z d    Z d   Z RS(   c         C  s   | |  _  d  S(   N(   t   sa(   R>   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd     s    c         C  sN   y5 t  j |  } | r4 | j | d |  j j   SWn t k
 rI d  SXd  S(   NR}   (   R	   t   class_mapperR&   R   R}   R   R   (   R>   R7   t   typeRk   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   __get__  s     (   RC   RD   Rd   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s   	c         C  s@   |  j  r t S|  j d } | d  k	 r* | St |  j j d   S(   Nt   SQLALCHEMY_RECORD_QUERIESt   TESTING(   t   debugRp   R`   R   t   boolRL   (   R^   t   rq(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _record_queries  s    	t   _EngineConnectorc           B  s&   e  Z d d   Z d   Z d   Z RS(   c         C  s=   | |  _  | |  _ d  |  _ d  |  _ | |  _ t   |  _ d  S(   N(   t   _sat   _appR   t   _enginet   _connected_fort   _bindR   t   _lock(   R>   R   R^   RY   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd     s    					c         C  se   |  j  d  k r |  j j d S|  j j j d  p5 d } |  j  | k sZ t d |  j    | |  j  S(   Nt   SQLALCHEMY_DATABASE_URIt   SQLALCHEMY_BINDSsP   Bind %r is not specified.  Set it in the SQLALCHEMY_BINDS configuration variable(    (   R   R   R   R`   RL   R   (   R>   R\   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   get_uri  s    c         C  s  |  j   |  j   } |  j j d } | | f |  j k rB |  j St |  } i t d 6} |  j j	 |  j |  |  j j
 |  j | |  | r t | d <n  t j | |  |  _ } t |  j  r t |  j |  j j  j   n  | | f |  _ | SWd  QXd  S(   Nt   SQLALCHEMY_ECHOt   convert_unicodet   echo(   R   R   R   R`   R   R   R   Rp   R   t   apply_pool_defaultst   apply_driver_hacksR    t   create_engineR   R   R   Rx   (   R>   t   uriR   R   Rf   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRi     s"    
	N(   RC   RD   R   Rd   R   Ri   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s   		c         C  s   t  d   t |   D  S(   sA   Figures out if the given dictionary defines a primary key column.c         s  s0   |  ]& \ } } t  | t j  r | j Vq d  S(   N(   R   R    R   t   primary_key(   t   .0t   kt   v(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pys	   <genexpr>  s    	(   t   anyR   (   R(   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _defines_primary_key  s    t   _BoundDeclarativeMetac           B  s   e  Z d    Z d   Z RS(   c         C  sx   | j  d  } | rb | j  d  d  k rb t |  rb d   } t j | |  j d  | d <n  t j |  | | |  S(   Nt   __tablename__t	   __table__c         S  sH   |  j    } t |  d k r: d | d  | d f j   Sd | j   S(   Ni   s   _%s_%sit   _(   t   groupR   t   lower(   t   matcht   word(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _join  s    R   (   RL   R   R   t   _camelcase_ret   subt   lstripR   t   __new__(   t   clsRT   t   basesR(   t	   tablenameR   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s    	"c         C  sK   | j  d d   } t j |  | | |  | d  k	 rG | |  j j d <n  d  S(   Nt   __bind_key__R   (   R   R   R   Rd   R   R   (   R>   RT   R   R(   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd     s    (   RC   RD   R   Rd   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s   	c         C  s&   d |  j  k s t d   |  j  d S(   s"   Gets the state for the applicationR    ss   The sqlalchemy extension was not registered to the current application.  Please make sure to call init_app() first.(   t
   extensionsR   (   R^   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRh     s    	t   _SQLAlchemyStatec           B  s   e  Z d  Z d   Z RS(   s0   Remembers configuration for the (db, app) tuple.c         C  s   | |  _  | |  _ i  |  _ d  S(   N(   R$   R^   t
   connectors(   R>   R$   R^   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd   (  s    		(   RC   RD   Rn   Rd   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR   %  s   t   Modelc           B  s   e  Z d  Z e Z d Z RS(   s!   Baseclass for custom user models.N(   RC   RD   Rn   R'   R&   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR   .  s   t
   SQLAlchemyc           B  s   e  Z d  Z d e d d  Z e d    Z d d  Z d   Z	 d   Z
 d   Z d   Z d   Z e d	    Z d d
  Z d d  Z d d  Z d d  Z d d  Z e d  Z d d d  Z d d d  Z d d d  Z d   Z RS(   s  This class is used to control the SQLAlchemy integration to one
    or more Flask applications.  Depending on how you initialize the
    object it is usable right away or will attach as needed to a
    Flask application.

    There are two usage modes which work very similar.  One is binding
    the instance to a very specific Flask application::

        app = Flask(__name__)
        db = SQLAlchemy(app)

    The second possibility is to create the object once and configure the
    application later to support it::

        db = SQLAlchemy()

        def create_app():
            app = Flask(__name__)
            db.init_app(app)
            return app

    The difference between the two is that in the first case methods like
    :meth:`create_all` and :meth:`drop_all` will work all the time but in
    the second case a :meth:`flask.Flask.app_context` has to exist.

    By default Flask-SQLAlchemy will apply some backend-specific settings
    to improve your experience with them.  As of SQLAlchemy 0.6 SQLAlchemy
    will probe the library for native unicode support.  If it detects
    unicode it will let the library handle that, otherwise do that itself.
    Sometimes this detection can fail in which case you might want to set
    `use_native_unicode` (or the ``SQLALCHEMY_NATIVE_UNICODE`` configuration
    key) to `False`.  Note that the configuration key overrides the
    value you pass to the constructor.

    This class also provides access to all the SQLAlchemy functions and classes
    from the :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` modules.  So you can
    declare models like this::

        class User(db.Model):
            username = db.Column(db.String(80), unique=True)
            pw_hash = db.Column(db.String(80))

    You can still use :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` directly, but
    note that Flask-SQLAlchemy customizations are available only through an
    instance of this :class:`SQLAlchemy` class.  Query classes default to
    :class:`BaseQuery` for `db.Query`, `db.Model.query_class`, and the default
    query_class for `db.relationship` and `db.backref`.  If you use these
    interfaces through :mod:`sqlalchemy` and :mod:`sqlalchemy.orm` directly,
    the default query class will be that of :mod:`sqlalchemy`.

    .. admonition:: Check types carefully

       Don't perform type or `isinstance` checks against `db.Table`, which
       emulates `Table` behavior but is not a class. `db.Table` exposes the
       `Table` interface, but is a function which allows omission of metadata.

    You may also define your own SessionExtension instances as well when
    defining your SQLAlchemy class instance. You may pass your custom instances
    to the `session_extensions` keyword. This can be either a single
    SessionExtension instance, or a list of SessionExtension instances. In the
    following use case we use the VersionedListener from the SQLAlchemy
    versioning examples.::

        from history_meta import VersionedMeta, VersionedListener

        app = Flask(__name__)
        db = SQLAlchemy(app, session_extensions=[VersionedListener()])

        class User(db.Model):
            __metaclass__ = VersionedMeta
            username = db.Column(db.String(80), unique=True)
            pw_hash = db.Column(db.String(80))

    The `session_options` parameter can be used to override session
    options.  If provided it's a dict of parameters passed to the
    session's constructor.

    .. versionadded:: 0.10
       The `session_options` parameter was added.

    .. versionadded:: 0.16
       `scopefunc` is now accepted on `session_options`. It allows specifying
        a custom function which will define the SQLAlchemy session's scoping.
    c         C  s   | |  _  | d  k r i  } n  | j d t j  |  j |  |  _ |  j   |  _ t	   |  _
 | d  k	 r | |  _ |  j |  n	 d  |  _ t |   t |  j  j   t   j   t |  _ d  S(   Nt	   scopefunc(   t   use_native_unicodeR   R   R   t   __ident_func__t   create_scoped_sessionR}   t   make_declarative_baseR   R   t   _engine_lockR^   t   init_appR:   R   Rk   Rx   Rq   R'   t   Query(   R>   R^   R  t   session_options(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRd     s     				
c         C  s
   |  j  j S(   s   Returns the metadata(   R   R   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s    c         C  sF   | d k r i  } n  | j d d  } t j t |  j |  d | S(   sr   Helper factory method that creates a scoped session.  It
        internally calls :meth:`create_session`.
        R  N(   R   R   R	   t   scoped_sessionR   t   create_session(   R>   Rf   R  (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR    s
    	c         C  s   t  |  |  S(   s   Creates the session.  The default implementation returns a
        :class:`SignallingSession`.

        .. versionadded:: 2.0
        (   RW   (   R>   Rf   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR    s    c         C  s.   t  d t d d d t  } t |   | _ | S(   s   Creates the declarative base.R   RT   R   t	   metaclass(   R   R   R   R   R   (   R>   t   base(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR    s    	c           sy   j  j d d   j  j d d   j  j d d   j  j d t   j  j d d   j  j d d   j  j d d   j  j d	 d   j  j d
 d   j  j d t   j  j d t  t  d  s i   _ n  t      j d <t  d  r j } n@ t  d  r8 j	 } n%  j  d rTt
 d   n   j } |    f d    } d S(   s   This callback can be used to initialize an application for the
        use with this database setup.  Never use a database in the context
        of an application not initialized that way or connections will
        leak.
        R   s	   sqlite://R   t   SQLALCHEMY_NATIVE_UNICODER   R   t   SQLALCHEMY_POOL_SIZEt   SQLALCHEMY_POOL_TIMEOUTt   SQLALCHEMY_POOL_RECYCLEt   SQLALCHEMY_MAX_OVERFLOWt   SQLALCHEMY_COMMIT_ON_TEARDOWNRX   R   R    t   teardown_appcontextt   teardown_requests(   Commit on teardown requires Flask >= 0.7c           s=    j  d r, |  d  k r,   j j   q, n    j j   |  S(   NR  (   R`   R   R}   t   committ   remove(   t   response_or_exc(   R>   R^   (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   shutdown_session  s
    N(   R`   R   R   Ro   Rp   R1   R   R   R  R  t   RuntimeErrort   after_request(   R>   R^   t   teardownR  (    (   R>   R^   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR    s,    	c           sJ      f d   } | d d  | d d  | d d  | d d	  d  S(
   Nc           s*     j  | } | d  k	 r& |  |  <n  d  S(   N(   R`   R   (   t	   optionkeyt	   configkeyt   value(   R^   Rf   (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _setdefault  s    t	   pool_sizeR  t   pool_timeoutR  t   pool_recycleR  t   max_overflowR  (    (   R>   R^   Rf   R   (    (   R^   Rf   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s
    c         C  s:  | j  j d  rZ | j j d d  | j  d k r | j d d  | j d d  q n | j  d	 k r | j d  } t } | j d k r t } | d k r t	 d   q n# | s d d l
 m } | | d <n  | s t j j | j | j  | _ q n  | j d } | d k r#|  j } n  | s6t | d <n  d S(   s  This method is called before engine creation and used to inject
        driver specific hacks into the options.  The `options` parameter is
        a dictionary of keyword arguments that will then be used to call
        the :func:`sqlalchemy.create_engine` function.

        The default implementation provides some saner defaults for things
        like pool sizes for MySQL and sqlite.  Also it injects the setting of
        `SQLALCHEMY_NATIVE_UNICODE`.
        t   mysqlt   charsett   utf8s   mysql+gaerdbmsR!  i
   R#  i   t   sqlitet    s   :memory:i    sL   SQLite in memory database with an empty queue not possible due to data loss.(   t   NullPoolt	   poolclassR  R  N(   NR)  s   :memory:(   t
   drivernameRM   R   R   RL   Ro   t   databaseR   Rp   R  t   sqlalchemy.poolR*  t   ost   patht   joint	   root_pathR`   R  (   R>   R^   R   Rf   R!  t   detected_in_memoryR*  t   unu(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR     s,    
$c         C  s   |  j  |  j    S(   sU  Gives access to the engine.  If the database configuration is bound
        to a specific application (initialized with an application) this will
        always return a database connection.  If however the current application
        is used this might raise a :exc:`RuntimeError` if no application is
        active at the moment.
        (   Ri   R]   (   R>   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRb   (  s    c         C  s   t  |  | |  S(   s1   Creates the connector for a given state and bind.(   R   (   R>   R^   RY   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   make_connector2  s    c         C  sj   |  j  [ t |  } | j j |  } | d k rV |  j | |  } | | j | <n  | j   SWd QXd S(   sC   Returns a specific engine.

        .. versionadded:: 0.12
        N(   R  Rh   R   RL   R   R5  Ri   (   R>   R^   RY   Rm   t	   connector(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRi   6  s    
c         C  sR   | d k	 r | S|  j d k	 r& |  j St j } | d k	 rB | j St d   d S(   sK   Helper method that implements the logic to look up an application.
        sU   application not registered on db instance and no application bound to current contextN(   R   R^   R   R   R  (   R>   t   reference_appR   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR]   C  s    	c         C  sR   g  } xE t  |  j j j  D]. } | j j d  | k r | j |  q q W| S(   s1   Returns a list of all tables relevant for a bind.R   (   R   R   R   t   tablesR   RL   R   (   R>   RY   t   resultt   table(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   get_tables_for_bindQ  s
    c           s   |  j  |  } d g t | j j d  p- d  } i  } xR | D]J } |  j | |    |  j |  } | j t   f d   | D   qA W| S(   s   Returns a dictionary with a table->engine mapping.

        This is suitable for use of sessionmaker(binds=db.get_binds(app)).
        R   c         3  s   |  ] } |   f Vq d  S(   N(    (   R   R:  (   Rb   (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pys	   <genexpr>d  s    N(    (	   R]   R   R   R`   RL   Ri   R;  R   t   dict(   R>   R^   R\   t   retvalRY   R8  (    (   Rb   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRe   Y  s    %'c   	      C  s   |  j  |  } | d k rC d  g t | j j d  p9 d  } n- t | t  s^ | d  k rj | g } n | } xg | D]_ } i  } | s |  j |  } | | d <n  t |  j	 j
 |  } | d |  j | |  |  qw Wd  S(   NR0   R   R8  RY   (    (   R]   R   R   R`   RL   R   R   R;  R3   R   R   Ri   (	   R>   R^   RY   R   t   skip_tablesR\   t   extraR8  t   op(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   _execute_for_all_tablesg  s    (R0   c         C  s   |  j  | | d  d S(   s_   Creates all tables.

        .. versionchanged:: 0.12
           Parameters were added
        t
   create_allN(   RA  (   R>   RY   R^   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRB  y  s    c         C  s   |  j  | | d  d S(   s]   Drops all tables.

        .. versionchanged:: 0.12
           Parameters were added
        t   drop_allN(   RA  (   R>   RY   R^   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRC    s    c         C  s   |  j  | | d d t d S(   sn   Reflects tables from the database.

        .. versionchanged:: 0.12
           Parameters were added
        t   reflectR>  N(   RA  Rp   (   R>   RY   R^   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRD    s    c         C  si   d  } |  j d  k	 r! |  j } n! t j } | d  k	 rB | j } n  d |  j j | ra | j d pd d  f S(   Ns   <%s engine=%r>R   (   R   R^   R   R   t	   __class__RC   R`   (   R>   R^   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyRB     s    		N(   RC   RD   Rn   R   Rp   Rd   RE   R   R  R  R  R  R   R   Rb   R5  Ri   R]   R;  Re   Ro   RA  RB  RC  RD  RB   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyR   :  s,   T
			*	
	+
(P   Rn   t
   __future__R    R   R/  t   reRH   t   timeR-   R    t   mathR   R   t   flaskR   R   t   flask.signalsR   t   operatorR   t	   threadingR   R	   R
   t   sqlalchemy.orm.excR   t   sqlalchemy.orm.sessionR   Rc   t   sqlalchemy.eventR   t   sqlalchemy.engine.urlR   t   sqlalchemy.ext.declarativeR   R   t   flask.ext.sqlalchemy._compatR   R   R   R   t   platformt   clockR   R   t   ImportErrorR   t   __version__R   t   compileR   t   _signalst   signalR~   Rz   R%   R)   R/   R:   R   R;   RV   RW   t   objectRq   R   R   R   R   R  R'   R   R   R   R   R   Rh   R   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask_sqlalchemy/__init__.pyt   <module>
   sn   "	
						%	#c2		'			