ó
ú2ec           @   s  d  Z  d d l m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 „  Z	 d d g Z
 d e k r e r d d l m Z e e d d ƒZ e ƒ  Z e j ƒ  Z e j d e j d ƒ ƒ e j ƒ  \ Z Z e e f GHn  d S(   só   
Middleware related to transactions and database connections.

At this time it is very basic; but will eventually sprout all that
two-phase commit goodness that I don't need.

.. note::

   This is experimental, and will change in the future.
iÿÿÿÿ(   t   HTTPException(   t   catch_errorst   TransactionManagerMiddlewarec           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   application(   t   selfR   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   __init__   s    c         C   s@   t  ƒ  | d <} t | d <t |  j | | d | j d | j ƒS(   Ns   paste.transaction_managers   paste.throw_errorst   error_callbackt   ok_callback(   t   Managert   TrueR   R   t   errort   finish(   R   t   environt   start_responset   manager(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   __call__   s
    
	(   t   __name__t
   __module__R   R   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR      s   	R   c           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  |  _ g  |  _ d  S(   N(   t   Falset   abortedt   transactions(   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR   "   s    	c         C   s   t  |  _ d  S(   N(   R	   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   abort&   s    c         C   s   t  |  _ |  j ƒ  d  S(   N(   R	   R   R   (   R   t   exc_info(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR
   )   s    	c         C   s8   x1 |  j  D]& } |  j r& | j ƒ  q
 | j ƒ  q
 Wd  S(   N(   R   R   t   rollbackt   commit(   R   t   trans(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR   -   s    	(   R   R   R   R   R
   R   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR       s   			t   ConnectionFactoryc           B   s#   e  Z d  Z d „  Z d d „ Z RS(   s  
    Provides a callable interface for connecting to ADBAPI databases in
    a WSGI style (using the environment).  More advanced connection
    factories might use the REMOTE_USER and/or other environment
    variables to make the connection returned depend upon the request.
    c         O   sO   | |  _  | |  _ | |  _ d „  |  _ t |  j  d ƒ rK |  j  j |  _ n  d  S(   Nc         S   s   d |  j  d d ƒ S(   Ns   '%s't   's   ''(   t   replace(   t   s(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   <lambda>C   t    t   PgQuoteString(   t   modulet   argst   kwargst   quotet   hasattrR    (   R   R!   R"   R#   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR   <   s    			c         C   s?   |  j  j |  j |  j Ž  } |  j  | j d <|  j | j d <| S(   NR!   R$   (   R!   t   connectR"   R#   t   __dict__R$   (   R   R   t   conn(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR   G   s    N(   R   R   t   __doc__R   t   NoneR   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyR   5   s   	c            s   ‡  ‡ f d †  } | S(   s  
    Provides a simple mechanism for starting a transaction based on the
    factory; and for either committing or rolling back the transaction
    depending on the result.  It checks for the response's current
    status code either through the latest call to start_response; or
    through a HTTPException's code.  If it is a 100, 200, or 300; the
    transaction is committed; otherwise it is rolled back.
    c            s_   ˆ |  ƒ ‰  ˆ  |  d <d g ‰ d  ‡  ‡ f d † } d  ‡ ‡ f d † } t ˆ |  | | | ƒ S(   Ns   paste.connectioniô  c            sy   |  r3 t  |  d t ƒ r3 ˆ j |  d j ƒ q3 n  ˆ j ƒ  d k  rR ˆ  j ƒ  n y ˆ  j ƒ  Wn d  SXˆ  j ƒ  d  S(   Ni   i  (   t
   isinstanceR    t   appendt   codet   popR   R   t   close(   R   (   R(   t   should_commit(    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt	   finalizerZ   s    c            s0   ˆ  j  t |  j d ƒ d ƒ ƒ ˆ |  | | ƒ S(   Nt    i    (   R,   t   intt   split(   t   statust   headersR   (   R0   R   (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   basictrans_start_responseg   s     (   R*   R   (   R   R   R1   R7   (   R   t   factory(   R(   R0   R   s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   basic_transactionV   s    
	(    (   R   R8   R9   (    (   R   R8   s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   BasicTransactionHandlerM   s    	R:   t   __main__(   t   PgSQLt   databaset   testings   SELECT now(), %ss	   B'n\'glesN(   R)   t   paste.httpexceptionsR    t   wsgilibR   t   objectR   R   R   R:   t   __all__R   R   t   pyPgSQLR<   R8   R(   t   cursort   currt   executeR$   t   fetchonet   timet   bing(    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/transaction.pyt   <module>   s   	!	