
{Jc        ,   @   s5  d  Z  d d k Z d d k Z d d k Z d d k Z d d k l Z d d k l Z y, d d k Z d d k	 Z d d k l
 Z
 Wn e j
 o d Z n Xy< d d k Z d d k l Z l Z l Z d d k l Z Wn e j
 o d Z n Xd d k l Z l Z l Z d d	 k l Z d d
 k l Z e i d  Z e a  e o d d  Z! d d  Z" d   Z# d   Z$ e i% i& e$  Z' e( e' d  p e' i) e' _* n e' d  Z+ e+   Z, e( e' d  o e' i- Z- n e, Z- h  Z. e
   e. d <e. d Z/ y# d d k0 Z0 e/ e' e0 _/ e0 _' Wqne j
 o qnXn3 d   Z! d   Z" d   Z# d   Z$ d Z' Z/ Z- e# Z1 e2   Z3 e4   Z5 e o9 d   Z6 d e f d     YZ7 d e8 f d     YZ9 n- d e8 f d     YZ7 d e8 f d     YZ9 d d  Z: d   Z; d    Z< d!   Z= e   d"    Z> e   d#    Z? d d$  Z@ e e> d%  d&    ZA e e? d%  d'    ZB d(   ZC e e> d)  d*    ZD e e? d)  d+    ZE d,   ZF d-   ZG d d.  ZH d d/  ZI d0 e f d1     YZJ d d2 d3 d4 d5 d6 d0 d7 d8 d d9 d d: d; d< d= d> d? d@ g ZK d S(A   sA   Convenient access to an SQLObject or SQLAlchemy managed database.iN(   t   request(   t
   BaseFilter(   t   MetaData(   t   ConnectionHubt   Transactiont   TheURIOpener(   t   local(   t   abstractt   whent   NoApplicableMethods(   t   config(   t   remove_keyss   turbogears.databasec         C   s   t    t |   i S(   s7   Retrieve the engine based on the current configuration.(   t   bind_metadatat   get_metadatat   bind(   t   pkg(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt
   get_engine$   s    c         C   s8   y t  |  SWn% t j
 o t   t  |  <t  |  SXd S(   s0   Retrieve the metadata for the specified package.N(   t
   _metadatast   KeyErrorR   (   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR   )   s
    c          C   s  t  i   o d St   }  xL t i i d i   D]4 \ } } d | j o | |  | i d  d <q2 q2 W|  i d  } | p t d   n t	 i
 | |   t  _ t a xj t i i d i   D]R \ } } d | j o9 d	 | j o, t	 i
 | |   t | i d  d
  _ q q Wd S(   s1   Connect SQLAlchemy to the configured database(s).Nt   globalt
   sqlalchemyt   .it   dburis$   No sqlalchemy database config found!s   .dburis   sqlalchemy.i    (   t   metadatat   is_boundt   dictR
   t	   configMapt   itemst   splitt   popR   R   t   create_engineR   t   Truet	   _using_saR   (   t	   alch_argst   kt   vR   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR   1   s     	  c           C   s%   t  i   p t   n t i i   S(   s   Create a session that uses the engine from thread-local metadata.

        The session by default does not begin a transaction, and requires that
        flush() be called explicitly in order to persist results to the database.

        (   R   R   R   R   t   ormt   create_session(    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR%   G   s    t   addc            s     f d   } | S(   Nc            s{   | i  d t   | i  d t    |  i t i j o    f d   } | |  _ n  i   |  _ t i i	 |  | |  S(   Nt   validatet   autoaddc            sx   xY | i    D]K \ } }  o( t |  |  p t d |   qH n t |  | |  q W  o t i |   n d  S(   Ns   Invalid __init__ argument: '%s'(   R   t   hasattrt	   TypeErrort   setattrt   sessionR&   (   t   selft   kwargst   keyt   value(   R(   R'   (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   __init__g   s     (
   R   t   FalseR   R1   t   objectt   query_propertyt   queryR   R$   t   mapper(   t   clst   argt   kwR1   (   t   scoped_session(   R(   R'   sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR6   b   s    	(    (   R:   R6   (    (   R:   sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   create_session_mappera   s    R6   c           C   s   d  S(   N(    (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR      s    c           C   s   d  S(   N(    (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR      s    c           C   s   d  S(   N(    (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR      s    c           C   s   d  S(   N(    (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR%      s    c         C   sK   |  d d j o t  i t  i |  d   Sd d k } | i i |   Sd S(   s   Convert a MySQL TIMESTAMP to a floating point number representing
        the seconds since the Un*x Epoch. It uses custom code the input seems
        to be the new (MySQL 4.1+) timestamp format, otherwise code from the
        MySQLdb module is used.i   t   -s   %Y-%m-%d %H:%M:%SiN(   t   timet   mktimet   strptimet   MySQLdb.converterst
   converterst   mysql_timestamp_converter(   t   rawt   MySQLdb(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   _mysql_timestamp_converter   s    t   AutoConnectHubc           B   st   e  Z d  Z d
 Z h  Z d
 e d  Z d   Z d   Z	 d   Z
 d   Z d
 d  Z d   Z d   Z d	   Z RS(   s   Connects to the database once per thread. The AutoConnectHub also
        provides convenient methods for managing transactions.c         C   sJ   | p t  i d  } n | |  _ | |  _ t i |   t i |   d  S(   Ns   sqlobject.dburi(   R
   t   gett   urit   supports_transactionst   hub_registryR&   R   R1   (   R-   RH   RI   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR1      s    		c         C   sT   d d k  } | i d d !} | d } | d } | d j  p | d j o
 | d j  S(   s.   Return True only if version of MySQLdb <= 1.0.iNi    i   i   (   RD   t   version_info(   R-   RD   t   module_versiont   majort   minor(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   _is_interesting_version   s
    

c         C   s_   d d k  } d d k } |  i   o6 | i i i   } t | | i i i	 <| | i
 d <n d S(   s   Enable a workaround for an incompatible timestamp format change
            in MySQL 4.1 when using an old version of MySQLdb. See trac ticket
            #1235 - http://trac.turbogears.org/ticket/1235 for details.iNt   conv(   RD   R@   RO   RA   t   conversionst   copyRE   t	   constantst
   FIELD_TYPEt	   TIMESTAMPR9   (   R-   t
   connectionRD   RQ   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   _enable_timestamp_workaround   s    c         C   s   y |  i  i } |  i |  SWn t j
 o |  i o t i |  i  } |  i i d  o h  t _	 n8 |  i i d  o$ t
 i d t  o |  i |  n | |  i  _ |  i |  St d   n Xd  S(   Nt   sqlitet   mysqls.   turbogears.enable_mysql41_timestamp_workarounds9   No connection has been defined for this thread or process(   t   threadingLocalRV   t   begint   AttributeErrorRH   t	   sqlobjectt   connectionForURIt
   startswithR   t
   cachedURIsR
   RG   R2   RW   (   R-   t   conn(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   getConnection   s    
c         C   s   t    |  _ d S(   s]   Used for testing purposes. This drops all of the connections
            that are being held.N(   t   threading_localRZ   (   R-   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   reset   s    c         C   s{   |  i  p | S| p |  i   } n t | t  o | i o | i   n | S| |  i _ | i   } | |  i _	 | S(   s   Start a transaction.(
   RI   Rb   t
   isinstanceR   t	   _obsoleteR[   RZ   t   old_connt   transactionRV   (   R-   Ra   t   trans(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR[      s    

c         C   s^   |  i  p d Sy |  i i } Wn t j
 o d SXt | t  o |  i i i   n d S(   s   Commit the current transaction.N(   RI   RZ   RV   R\   Re   R   t   commit(   R-   Ra   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRj      s    
c         C   si   |  i  p d Sy |  i i } Wn t j
 o d SXt | t  o | i o |  i i i   n d S(   s!   Rollback the current transaction.N(   RI   RZ   RV   R\   Re   R   Rf   t   rollback(   R-   Ra   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRk      s    
c         C   s   |  i  p d Sy |  i i } Wn t j
 o d SXt | t  p d S| i p | i   n |  i i |  i _ |  i ` |  i i i	   d S(   s8   End the transaction, returning to a standard connection.N(
   RI   RZ   RV   R\   Re   R   Rf   Rk   Rg   t	   expireAll(   R-   Ra   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   end
  s    

	N(   t   __name__t
   __module__t   __doc__t   NoneRH   t   paramsR   R1   RO   RW   Rb   Rd   R[   Rj   Rk   Rm   (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRF      s   							t
   PackageHubc           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s  Transparently proxies to an AutoConnectHub for the URI
        that is appropriate for this package. A package URI is
        configured via "packagename.dburi" in the TurboGears config
        settings. If there is no package DB URI configured, the
        default (provided by "sqlobject.dburi") is used.

        The hub is not instantiated until an attempt is made to
        use the database.
        c         C   s   | |  _  d  |  _ d  S(   N(   t   packagenameRq   t   hub(   R-   Rt   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR1   $  s    	c         C   s&   |  i  o |  i  i | |  S|  Sd  S(   N(   Ru   t   __get__(   R-   t   objt   type(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRv   (  s    
c         C   s+   |  i  p |  i   n |  i  i | |  S(   N(   Ru   t   set_hubt   __set__(   R-   Rw   Rx   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRz   .  s    
c         C   sY   |  i  p |  i   n y t |  i  |  SWn& t j
 o t |  i  i   |  SXd  S(   N(   Ru   Ry   t   getattrR\   Rb   (   R-   t   name(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   __getattr__3  s    
c         C   s   t  i d |  i d   } | p t  i d d   } n | p t d  n | i d  o | d } t } n t } t i | d   } | p  t	 | d | } | t | <n | |  _
 d  S(   Ns   %s.dburis   sqlobject.dburis    No database configuration found!t   notrans_i   RI   (   R
   RG   Rt   Rq   R   R_   R2   R   t   _hubsRF   Ru   (   R-   R   Ri   Ru   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRy   ;  s    

(   Rn   Ro   Rp   R1   Rv   Rz   R}   Ry   (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRs     s   					c           B   s   e  Z RS(    (   Rn   Ro   (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRF   L  s   c           B   s   e  Z RS(    (   Rn   Ro   (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyRs   O  s   c         C   sI   | o# t  i h h |  d | 6d 6 n t  i h h |  d 6d 6 d S(   s  Sets the database URI to use either globally or for a specific
    package. Note that once the database is accessed, calling
    setDBUri will have no effect.

    @param dburi: database URI to use
    @param package: package name this applies to, or None to set the default.
    s   %s.dburiR   s   sqlobject.dburiN(   R
   t   update(   R   t   package(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt
   set_db_uriR  s
    		c          C   s   x t  D] }  |  i   q Wd S(   sA   Commit the transactions in all registered hubs (for this thread).N(   RJ   Rj   (   Ru   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt
   commit_allc  s     c          C   s   x t  D] }  |  i   q Wd S(   sC   Rollback the transactions in all registered hubs (for this thread).N(   RJ   Rk   (   Ru   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   rollback_allh  s     c          C   s   x t  D] }  |  i   q Wd S(   s>   End the transactions in all registered hubs (for this thread).N(   RJ   Rm   (   Ru   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   end_allm  s     c         O   s   d  S(   N(    (   t   funct   argsR9   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   run_with_transactionr  s    c         C   s   d  S(   N(    (   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   restart_transactionv  s    c         C   s   t  S(   N(   R    (   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   _use_saz  s    s   not _use_sa(args)c      	   O   s   t  i d  zt y |  | |   } t   | SWnO t i j
 o t     n1 t i j
 o t     n t     n XWd  t   Xd  S(   Ns   Starting SQLObject transaction(   t   logt   debugR   t   cherrypyt   HTTPRedirectt   InternalRedirectR   R   (   R   R   R9   t   retval(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   so_rwt~  s     c         C   s   d  S(   N(    (   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   so_restart_transaction  s    c         C   s   d d k  l } | d  \ } } } } | d } t i   \ } }	 }
 t | d  y | | | d  |  | |  } Wn t j
 o | |	 |
  n	 X~
 | Sd  S(   Ni(   t   dispatch_errori   t	   tg_sourcet	   tg_errorst   tg_exceptions(   R   R   R   (   t   turbogears.errorhandlingR   t   syst   exc_infoR   Rq   R	   (   t	   exceptionR   R9   R   t	   real_funct   acceptt
   allow_jsont
   controllert   exc_typet	   exc_valuet	   exc_tracet   output(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   dispatch_exception  s    
s   _use_sa(args)c      	   O   s  t  i d  t i   t _ z y |  | |   } Wn t i t i f j
 o< t	   o t  i d  t i
   n t  i d    n= t	   o t  i d  t i   n t  i d    n Xt	   o t  i d  t i
   n t  i d  Wd  t  i d  t i   X| S(	   Ns   Starting SA transactions0   Redirect in active transaction - will commit nows    Redirect in inactive transactions/   Error in active transaction - will rollback nows   Error in inactive transactions-   Transaction is still active - will commit nows   Transaction is already inactives   Ending SA transaction(   R   R   R,   R[   R    t   sa_transactionR   R   R   t   sa_transaction_activeRj   Rk   t   close(   R   R   R9   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   sa_rwt  s0    


c         C   s\   t  i d  t   o t  i d  t i   n t  i d  t i   t i   t _ d  S(   Ns   Restarting SA transactions/   Transaction is still active - will rollback nows   Transaction is already inactive(	   R   R   R   R,   Rk   R   R[   R    R   (   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   sa_restart_transaction  s    

c          C   s   y t  i SWno t j
 oc y t    i SWq} t t f j
 o9 y t i }  |  o |  i SWqx t j
 o t SXq} Xn Xd S(   s-   Check whether SA transaction is still active.N(   R,   t	   is_activeR\   R*   R    R   R2   (   Rh   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR     s    	c         C   s   h  } |  d j o | Sx- |  i i i   D] } t |  |  | | <q+ W|  i | d <|  i o' | i t |  i	   | i
 d  n | S(   s3   Convert SQLObject to a dictionary based on columns.t   idt	   childNameN(   Rq   t   sqlmetat   columnst   keysR{   R   t   _inheritableR   t
   so_to_dictt   _parentR   (   t   sqlobjt   dR|   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR     s     
c         C   s^   | d j o
 h  } n | i t d   |  i i i     |  i o t |  i |  n | S(   sq   Return a dict with all columns from a SQLObject.

    This includes the columns from InheritableSO's bases.

    c         S   s   |  d  d j S(   i    R   (    (   t   i(    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   <lambda>  s    N(	   Rq   R   t   filterR   R   R   R   t
   so_columnst   __base__(   t   sqlclassR   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR     s    

c         C   sL   | d j o
 g  } n | i |  i i  |  i o t |  i |  n | S(   ss   Return a list with all joins from a SQLObject.

    The list includes the columns from InheritableSO's bases.

    N(   Rq   t   extendR   t   joinsR   t   so_joinsR   (   R   R   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR     s    

t   EndTransactionsFilterc           B   s   e  Z d    Z RS(   c         C   sG   t    o2 y t i   Wq< t j
 o t i   q< Xn t   d  S(   N(   R   R,   t   expunge_allR\   t   clearR   (   R-   (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   on_end_resource  s    
(   Rn   Ro   R   (    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyR     s   R   R%   R;   R   R   R   R   R   R   R,   t   session_mapperR   R   R   R   (L   Rp   R   R=   t   loggingR   R    t   cherrypy.filters.basefilterR   R   t   sqlalchemy.ormR   t   ImportErrorRq   R]   t   sqlobject.dbconnectionR   R   R   t   sqlobject.util.threadinglocalR   Rc   t
   peak.rulesR   R   R	   t
   turbogearsR
   t   turbogears.utilR   t	   getLoggerR   R2   R    R   R   R   R%   R$   R:   R,   R)   t   save_or_updateR&   R;   R   R6   R   R   t   elixirt   bind_meta_datat   setRJ   R   R   RE   RF   R3   Rs   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   __all__(    (    (    sq   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/TurboGears-1.1rc1-py2.6.egg/turbogears/database.pyt   <module>   s   			
								v2				#					