ó
[³XMc           @   sü   d  Z  d d l Z d d l m Z d d l m Z m Z d d l m Z d d l	 m
 Z
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d f  d „  ƒ  YZ d f  d „  ƒ  YZ e
 d d d d ƒ Z e e ƒ Z d „  Z d „  Z e e ƒ Z d d d g Z d S(   sg   
An asynchronous mapping to U{DB-API 2.0<http://www.python.org/topics/database/DatabaseAPI-2.0.html>}.
iÿÿÿÿN(   t   threads(   t   reflectt   log(   t
   deprecated(   t   Versiont   ConnectionLostc           B   s   e  Z d  Z RS(   sb   
    This exception means that a db connection has been lost.  Client code may
    try again.
    (   t   __name__t
   __module__t   __doc__(    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR      s   t
   Connectionc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s
  
    A wrapper for a DB-API connection instance.

    The wrapper passes almost everything to the wrapped connection and so has
    the same API. However, the Connection knows about its pool and also
    handle reconnecting should when the real connection dies.
    c         C   s    | |  _  d  |  _ |  j ƒ  d  S(   N(   t   _poolt   Nonet   _connectiont	   reconnect(   t   selft   pool(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   __init__#   s    		c         C   s   d  S(   N(    (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   close(   s    c         C   sÁ   |  j  j s |  j j ƒ  d  SyN |  j j ƒ  |  j j ƒ  } | j |  j  j ƒ | j ƒ  |  j j ƒ  d  SWn t	 j
 d  d ƒ n X|  j  j |  j ƒ |  j  j r´ t	 j d ƒ n  t ƒ  ‚ d  S(   Ns   Rollback faileds   Connection lost.(   R
   R   R   t   rollbackt   cursort   executet   good_sqlR   t   commitR   t   errR   t
   disconnectt   noisyt   msgR   (   R   t   curs(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   2   s     
c         C   s;   |  j  d  k	 r% |  j j |  j  ƒ n  |  j j ƒ  |  _  d  S(   N(   R   R   R
   R   t   connect(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   H   s    c         C   s   t  |  j | ƒ S(   N(   t   getattrR   (   R   t   name(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   __getattr__M   s    (   R   R   R   R   R   R   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR	      s   		
		t   Transactionc           B   sA   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s  A lightweight wrapper for a DB-API 'cursor' object.

    Relays attribute access to the DB cursor. That is, you can call
    execute(), fetchall(), etc., and they will be called on the
    underlying DB-API cursor object. Attributes will also be
    retrieved from there.
    c         C   s    | |  _  | |  _ |  j ƒ  d  S(   N(   R
   R   t   reopen(   R   R   t
   connection(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   [   s    		c         C   s    |  j  } d  |  _  | j ƒ  d  S(   N(   t   _cursorR   R   (   R   R#   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   `   s    		c         C   sž   |  j  d  k	 r |  j ƒ  n  y |  j j ƒ  |  _  d  SWn) |  j j sN ‚  qb t j d  d ƒ n X|  j j	 r~ t j
 d ƒ n  |  j ƒ  |  j j ƒ  |  _  d  S(   Ns   Cursor creation faileds   Connection lost, reconnecting(   R#   R   R   R   R   R
   R   R   R   R   R   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR!   e   s    
c         C   s   |  j  j ƒ  d  |  _ d  S(   N(   R   R   R   R#   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   x   s    c         C   s   t  |  j | ƒ S(   N(   R   R#   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   |   s    N(
   R   R   R   R   R#   R   R   R!   R   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR    Q   s   				t   ConnectionPoolc           B   sþ   e  Z d  Z d j ƒ  Z e Z d Z d Z d Z
 d Z e Z d Z e Z e Z e Z d Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% RS(   sŠ  
    Represent a pool of connections to a DB-API 2.0 compliant database.

    @ivar connectionFactory: factory for connections, default to L{Connection}.
    @type connectionFactory: any callable.

    @ivar transactionFactory: factory for transactions, default to
        L{Transaction}.
    @type transactionFactory: any callable

    @ivar shutdownID: C{None} or a handle on the shutdown event trigger
        which will be used to stop the connection pool workers when the
        reactor stops.

    @ivar _reactor: The reactor which will be used to schedule startup and
        shutdown events.
    @type _reactor: L{IReactorCore} provider
    s-   min max name noisy openfun reconnect good_sqli   i   s   select 1c   	      O   s   | |  _  t j | ƒ |  _ t |  j d d ƒ d k rF t j d ƒ n  t |  j d d ƒ d k  rq t j d ƒ n  | j d d ƒ } | d k r¢ d	 d
 l	 m
 } n  | |  _ | |  _ | |  _ xH |  j D]= } d | } | j | ƒ rÇ t |  | | | ƒ | | =qÇ qÇ Wt |  j |  j ƒ |  _ t |  j |  j ƒ |  _ i  |  _ d	 d l m } d	 d l } | j |  _ | j |  j |  j ƒ |  _ |  j j |  j ƒ |  _ d S(   sC  Create a new ConnectionPool.

        Any positional or keyword arguments other than those documented here
        are passed to the DB-API object when connecting. Use these arguments to
        pass database names, usernames, passwords, etc.

        @param dbapiName: an import string to use to obtain a DB-API compatible
                          module (e.g. 'pyPgSQL.PgSQL')

        @param cp_min: the minimum number of connections in pool (default 3)

        @param cp_max: the maximum number of connections in pool (default 5)

        @param cp_noisy: generate informational log messages during operation
                         (default False)

        @param cp_openfun: a callback invoked after every connect() on the
                           underlying DB-API object. The callback is passed a
                           new DB-API connection object.  This callback can
                           setup per-connection state such as charset,
                           timezone, etc.

        @param cp_reconnect: detect connections which have failed and reconnect
                             (default False). Failed connections may result in
                             ConnectionLost exceptions, which indicate the
                             query may need to be re-sent.

        @param cp_good_sql: an sql query which should always succeed and change
                            no state (default 'select 1')

        @param cp_reactor: use this reactor instead of the global reactor
            (added in Twisted 10.2).
        @type cp_reactor: L{IReactorCore} provider
        t   apilevels   2.0s'   DB API module not DB API 2.0 compliant.t   threadsafetyi    i   s+   DB API module not sufficiently thread-safe.t
   cp_reactoriÿÿÿÿ(   t   reactors   cp_%s(   t
   threadpoolN(   t	   dbapiNameR   t   namedModulet   dbapiR   R   R   R   t   popt   twisted.internetR(   t   _reactort   connargst   connkwt   CP_ARGSt   has_keyt   setattrt   mint   maxt   connectionst   twisted.pythonR)   t   threadt	   get_identt   threadIDt
   ThreadPoolt   callWhenRunningt   _startt   startID(	   R   R*   R0   R1   R(   t   argt   cp_argR)   R9   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   ¦   s2    $				
	c         C   s   d  |  _ |  j ƒ  S(   N(   R   R?   t   start(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR>   ï   s    	c         C   sD   |  j  s@ |  j j ƒ  |  j j d d |  j ƒ |  _ t |  _  n  d S(   s   
        Start the connection pool.

        If you are using the reactor normally, this function does *not*
        need to be called.
        t   duringt   shutdownN(   t   runningR)   RB   R/   t   addSystemEventTriggert
   finalCloset
   shutdownIDt   True(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRB   ô   s
    		c         O   s2   d d l  m } t j | |  j |  j | | | Ž S(   s×  
        Execute a function with a database connection and return the result.

        @param func: A callable object of one argument which will be executed
            in a thread with a connection from the pool.  It will be passed as
            its first argument a L{Connection} instance (whose interface is
            mostly identical to that of a connection object for your DB-API
            module of choice), and its results will be returned as a Deferred.
            If the method raises an exception the transaction will be rolled
            back.  Otherwise, the transaction will be committed.  B{Note} that
            this function is B{not} run in the main thread: it must be
            threadsafe.

        @param *args: positional arguments to be passed to func

        @param **kw: keyword arguments to be passed to func

        @return: a Deferred which will fire the return value of
            C{func(Transaction(...), *args, **kw)}, or a Failure.
        iÿÿÿÿ(   R(   (   R.   R(   R    t   deferToThreadPoolR)   t   _runWithConnection(   R   t   funct   argst   kwR(   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   runWithConnection  s    c   	      O   sŠ   |  j  |  ƒ } y$ | | | | Ž } | j ƒ  | SWnP t j ƒ  \ } } } y | j ƒ  Wn t j d  d ƒ n X| | | ‚ n Xd  S(   Ns   Rollback failed(   t   connectionFactoryR   t   syst   exc_infoR   R   R   R   (	   R   RL   RM   RN   t   connt   resultt   excTypet   excValuet   excTraceback(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRK     s    
c         O   s2   d d l  m } t j | |  j |  j | | | Ž S(   s±  
        Interact with the database and return the result.

        The 'interaction' is a callable object which will be executed
        in a thread using a pooled connection. It will be passed an
        L{Transaction} object as an argument (whose interface is
        identical to that of the database cursor for your DB-API
        module of choice), and its results will be returned as a
        Deferred. If running the method raises an exception, the
        transaction will be rolled back. If the method returns a
        value, the transaction will be committed.

        NOTE that the function you pass is *not* run in the main
        thread: you may have to worry about thread-safety in the
        function you pass to this if it tries to use non-local
        objects.

        @param interaction: a callable object whose first argument
            is an L{adbapi.Transaction}.

        @param *args: additional positional arguments to be passed
            to interaction

        @param **kw: keyword arguments to be passed to interaction

        @return: a Deferred which will fire the return value of
            'interaction(Transaction(...), *args, **kw)', or a Failure.
        iÿÿÿÿ(   R(   (   R.   R(   R    RJ   R)   t   _runInteraction(   R   t   interactionRM   RN   R(   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   runInteraction,  s    c         O   s   |  j  |  j | | Ž S(   s  Execute an SQL query and return the result.

        A DB-API cursor will will be invoked with cursor.execute(*args, **kw).
        The exact nature of the arguments will depend on the specific flavor
        of DB-API being used, but the first argument in *args be an SQL
        statement. The result of a subsequent cursor.fetchall() will be
        fired to the Deferred which is returned. If either the 'execute' or
        'fetchall' methods raise an exception, the transaction will be rolled
        back and a Failure returned.

        The  *args and **kw arguments will be passed to the DB-API cursor's
        'execute' method.

        @return: a Deferred which will fire the return value of a DB-API
        cursor's 'fetchall' method, or a Failure.
        (   RZ   t	   _runQuery(   R   RM   RN   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   runQueryO  s    c         O   s   |  j  |  j | | Ž S(   sí  Execute an SQL query and return None.

        A DB-API cursor will will be invoked with cursor.execute(*args, **kw).
        The exact nature of the arguments will depend on the specific flavor
        of DB-API being used, but the first argument in *args will be an SQL
        statement. This method will not attempt to fetch any results from the
        query and is thus suitable for INSERT, DELETE, and other SQL statements
        which do not return values. If the 'execute' method raises an
        exception, the transaction will be rolled back and a Failure returned.

        The args and kw arguments will be passed to the DB-API cursor's
        'execute' method.

        return: a Deferred which will fire None or a Failure.
        (   RZ   t   _runOperation(   R   RM   RN   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   runOperationc  s    c         C   s^   |  j  r( |  j j |  j  ƒ d |  _  n  |  j rP |  j j |  j ƒ d |  _ n  |  j ƒ  d S(   sC   
        Close all pool connections and shutdown the pool.
        N(   RH   R/   t   removeSystemEventTriggerR   R?   RG   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   v  s    		c         C   sW   d |  _ |  j j ƒ  t |  _ x$ |  j j ƒ  D] } |  j | ƒ q/ W|  j j	 ƒ  d S(   s3   This should only be called by the shutdown trigger.N(
   R   RH   R)   t   stopt   FalseRE   R7   t   valuest   _closet   clear(   R   RS   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRG   ‚  s    		c         C   s³   |  j  ƒ  } |  j j | ƒ } | d k r¯ |  j re t j d |  j |  j pN d |  j	 pZ d f ƒ n  |  j
 j |  j |  j	 Ž  } |  j d k rŸ |  j | ƒ n  | |  j | <n  | S(   sŒ  Return a database connection when one becomes available.

        This method blocks and should be run in a thread from the internal
        threadpool. Don't call this method directly from non-threaded code.
        Using this method outside the external threadpool may exceed the
        maximum number of connections in the pool.

        @return: a database connection from the pool.
        s   adbapi connecting: %s %s%st    N(   R;   R7   t   getR   R   R   R   R*   R0   R1   R,   R   t   openfun(   R   t   tidRS   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   Œ  s    	c         C   s]   |  j  ƒ  } | |  j j | ƒ k	 r3 t d ƒ ‚ n  | d k	 rY |  j | ƒ |  j | =n  d S(   s  Disconnect a database connection associated with this pool.

        Note: This function should only be used by the same thread which
        called connect(). As with connect(), this function is not used
        in normal non-threaded twisted code.
        s   wrong connection for threadN(   R;   R7   Rf   t	   ExceptionR   Rc   (   R   RS   Rh   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR   ¤  s    c         C   sO   |  j  r# t j d |  j f ƒ n  y | j ƒ  Wn t j d  d ƒ n Xd  S(   Ns   adbapi closing: %ss   Connection close failed(   R   R   R   R*   R   R   R   (   R   RS   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRc   ³  s    	c   
      O   s¦   |  j  |  ƒ } |  j |  | ƒ } y. | | | | Ž } | j ƒ  | j ƒ  | SWnP t j ƒ  \ } } }	 y | j ƒ  Wn t j d  d ƒ n X| | |	 ‚ n Xd  S(   Ns   Rollback failed(
   RP   t   transactionFactoryR   R   RQ   RR   R   R   R   R   (
   R   RY   RM   RN   RS   t   transRT   RU   RV   RW   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRX   ¼  s    

c         O   s   | j  | | Ž  | j ƒ  S(   N(   R   t   fetchall(   R   Rk   RM   RN   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR[   Í  s    c         O   s   | j  | | Ž  d  S(   N(   R   (   R   Rk   RM   RN   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR]   Ñ  s    c         C   sT   i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6S(	   NR*   R5   R6   R   R   R   R0   R1   (   R*   R5   R6   R   R   R   R0   R1   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   __getstate__Ô  s    





c         C   s)   | |  _  |  j |  j |  j |  j Ž d  S(   N(   t   __dict__R   R*   R0   R1   (   R   t   state(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   __setstate__Þ  s    	N(&   R   R   R   t   splitR2   Ra   R   R5   R6   R   R   Rg   R   R   RE   R	   RP   R    Rj   RH   R   R>   RB   RO   RK   RZ   R\   R^   R   RG   R   R   Rc   RX   R[   R]   Rm   Rp   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR$   €   s>   	I					#				
								
t   Twistedi   i    c         C   s   |  j  d d ƒ j  d d ƒ S(   sK   
    Something really stupid that replaces quotes with escaped quotes.
    t   's   ''s   \s   \\(   t   replace(   t   text(    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   _safeê  s    c         C   s
   t  |  ƒ S(   s<   
    Make a string safe to include in an SQL statement.
    (   Rv   (   Ru   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   safeò  s    Rw   (   R   RQ   R.   R    R8   R   R   t   twisted.python.deprecateR   t   twisted.python.versionsR   Ri   R   t   objectR	   R    R$   t   _unreleasedVersiont   _unreleasedDeprecationRv   Rw   t   __all__(    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt   <module>   s    7/ÿ f		