Ñò
Õ¥WJc           @   sÕ  d  Z  d d k Z d d k Z d d k Z d d k l Z d d k Z d d g Z e i e	 ƒ Z
 e i e i i e d ƒ yº d d k Z d d k l Z e d d	 d
 d d d g ƒ Z e d d d d g ƒ Z d „  Z d d d „ Z d „  Z d d d d „ Z e i e d e ƒZ e i d d d d d g ƒ Wn e j
 o n Xy, d d k Z d d k l  Z  l! Z! l" Z" Wn e# Z  n Xd e  f d „  ƒ  YZ$ e% ƒ  Z& d e' f d „  ƒ  YZ( d e# f d  „  ƒ  YZ) d S(!   s€  Depreacted: pylons.database will be removed from a future version of
Pylons. SQLAlchemy 0.3.x users are recommended to migrate to SAContext
(http://cheeseshop.python.org/pypi/SAContext) for similar functionality.

Provides convenient access to SQLObject-managed and/or SQLAlchemy-managed
databases.

This module enables easy use of an SQLObject database by providing an 
auto-connect hub that will utilize the db uri string given in the Paste conf
file called ``sqlobject.dburi``.

A SQLAlchemy ``SessionContext`` is also available: it provides both thread and
process safe ``Session`` objects via the ``session_context.current`` property.
iÿÿÿÿN(   t   asboolt   AutoConnectHubt
   PackageHubi   (   t   sessioncontextt   convert_unicodet   echot	   echo_poolt   threadedt   use_ansit   use_oidst   max_overflowt	   pool_sizet   pool_recyclet   pool_timeoutc          C   s_   y t  t t i i ƒ  ƒ ƒ }  Wn t j
 o d }  n Xt i d |  ƒ d |  t i	 ƒ  f S(   s(  Return the id keying the current database session's scope.

        The session is particular to the current Pylons application -- this
        returns an id generated from the current thread and the current Pylons
        application's Globals object at pylons.g (if one is registered).
        t    s-   Returning %s as the database session scope ids   %s|%i(
   t   strt   idt   pylonst   configt   _current_objt	   TypeErrort   logt   debugt   threadt	   get_ident(   t   app_scope_id(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt	   app_scope1   s    	c         K   s.  t  ƒ  } | i | ƒ d | j o* d | j o | d | d <n | d =n |  p | i d ƒ | d <t | ƒ p | i d ƒ | d <| i d ƒ }  |  p t ‚ t t | i ƒ  d d „  ƒƒ } d |  | f } t	 i
 d } | | j o | | } n t i |  |  } | | <t i d |  | ƒ | S(	   s    Return a SQLAlchemy db engine. Uses the configuration values from
        ``get_engine_conf``.

        Engines are cached in the ``get_engines`` dict.
        t   dburit   uriR   t   keyc         S   s   |  d  S(   i    (    (   t   x(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   <lambda>Z   s    s   %s|%ss   pylons.db_enginess5   Created engine using uri: %s with engine arguments %s(   t   get_engine_conft   updatet   getR    t   popt   AssertionErrorR   t   sortedt   itemsR   R   t
   sqlalchemyt   create_engineR   R   (   R   R   t   kwargst   conft   conf_keyt
   engine_keyt
   db_enginest   engine(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR(   @   s$    	 !c          C   sÍ   h  }  xÀ t  i i ƒ  D]¯ \ } } | i d ƒ p q n | d } | t j o t | ƒ |  | <q | t j oJ y t | ƒ |  | <WqÅ t j
 o# d } t | | | f ƒ ‚ qÅ Xq | |  | <q W|  S(   st   Returns a dict of SQLAlchemy engine configuration values
        from the Pylons config file values ``sqlalchemy.*``s   sqlalchemy.i   s*   config sqlalchemy.%s is not an integer: %s(	   R   R   t	   iteritemst
   startswitht   BOOL_OPTIONSR    t   INT_OPTIONSt   intt
   ValueError(   t   resultt   kt   vt   reason(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR    g   s      
c         K   sL   | d j o
 h  } n t |  d | | } t i d ƒ t i d | |  S(   sÚ  Returns a SQLAlchemy session for the specified database uri from
        the the engine cache (returned from ``get_engines``)``. Uses the
        configuration values from ``get_engine_conf`` for uri and echo when
        None are specified.

        ``session_kwargs`` are passed to SQLAlchemy's ``create_session``
        function as keyword arguments.
        
        If the uri's engine does not exist, it will be created and added to
        the engine cache.
        R   s"   Created engine for session contextt   bind_toN(   t   NoneR(   R   R   R'   t   create_session(   R   R   t   session_kwargsR)   R.   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   make_session{   s
    
t	   scopefuncR   R(   R    R=   t   session_context(   t   ConnectionHubt   Transactiont   TheURIOpenerc           B   s_   e  Z d  Z d Z h  Z d e d „ Z d „  Z d „  Z	 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   s@   | p t  i i d ƒ } n | |  _ | |  _ t i |  ƒ d  S(   Ns   sqlobject.dburi(   R   R   R"   R   t   pool_connectionsR@   t   __init__(   t   selfR   RC   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRD   ¨   s
    		c         C   s¿   y |  i  i } | SWn¤ t j
 o˜ |  i oZ t i |  i ƒ } |  i i d ƒ o h  t _ n | |  i  _ |  i	 p d  | _ n | Sy |  i SWq» t j
 o t d ƒ ‚ q» Xn Xd  S(   Nt   sqlites9   No connection has been defined for this thread or process(   t   threadingLocalt
   connectiont   AttributeErrorR   t	   sqlobjectt   connectionForURIR0   RB   t
   cachedURIsRC   R:   t   _poolt   processConnection(   RE   t   conn(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   getConnection¯   s"    

c         O   s7   t  |  i d ƒ p |  i ƒ  n t i |  | | | Ž S(   s   Run a function in a transactionRH   (   t   hasattrRG   RP   R@   t   doInTransaction(   RE   t   funct   argst   kw(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRR   Ç   s    c         C   s[   |  i  ƒ  } t | t ƒ o | i o | i ƒ  n d S| |  i _ | i ƒ  |  i _ d S(   s   Starts a transaction.N(	   RP   t
   isinstanceRA   t	   _obsoletet   beginRG   t   old_connt   transactionRH   (   RE   RO   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRX   Í   s    
c         C   s4   |  i  i } t | t ƒ o |  i  i i ƒ  n d S(   s    Commits the current transaction.N(   RG   RH   RV   RA   t   commit(   RE   RO   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR[   ×   s    c         C   s?   |  i  i } t | t ƒ o | i o |  i  i i ƒ  n d S(   s#   Rolls back the current transaction.N(   RG   RH   RV   RA   RW   t   rollback(   RE   RO   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR\   Ý   s    c         C   sk   |  i  i } t | t ƒ p d S| i p | i ƒ  n |  i  i |  i  _ |  i  ` |  i  i i i ƒ  d S(   s9   Ends the transaction, returning to a standard connection.N(	   RG   RH   RV   RA   RW   R\   RY   t   cachet   clear(   RE   RO   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   endã   s    
	N(   t   __name__t
   __module__t   __doc__R:   R   t   paramst   TrueRD   RP   RR   RX   R[   R\   R_   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR   Ÿ   s   			
		t   UnconfiguredConnectionErrorc           B   s   e  Z d  Z RS(   sK   
    Raised when no configuration is available to set up a connection.
    (   R`   Ra   Rb   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRe   ô   s   c           B   sA   e  Z d  Z d e 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 Paste ini file
    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.

    If pool_connections=False, then a new database connection
    will be opened for every request.  This will avoid
    problems with database connections that periodically die.
    c         C   s(   | |  _  d  |  _ | |  _ | |  _ d  S(   N(   t   packagenameR:   t   hubR   RC   (   RE   Rf   R   RC   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRD     s    			c         C   sY   |  i  p< y |  i ƒ  WqF t j
 o } t t | ƒ ƒ ‚ qF Xn |  i  i | | ƒ S(   N(   Rg   t   set_hubRe   RI   R   t   __get__(   RE   t   objt   typet   e(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRi     s    
c         C   s+   |  i  p |  i ƒ  n |  i  i | | ƒ S(   N(   Rg   Rh   t   __set__(   RE   Rj   Rk   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRm     s    
c         C   s(   |  i  p |  i ƒ  n t |  i  | ƒ S(   N(   Rg   Rh   t   getattr(   RE   t   name(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   __getattr__  s    
c         C   sÕ   |  i  } | pK y t i i d |  i ƒ } Wq[ t j
 o } t t | ƒ ƒ ‚ q[ Xn | p t i i d ƒ } n | p t d ƒ ‚ n t i | ƒ } | p# t	 | d |  i
 ƒ} | t | <n | |  _ d  S(   Ns   %s.dburis   sqlobject.dburis    No database configuration found!RC   (   R   R   R   R"   Rf   R   Re   R   t   _hubsR   RC   Rg   (   RE   R   Rl   Rg   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyRh      s$    	N(
   R`   Ra   Rb   R:   Rd   RD   Ri   Rm   Rp   Rh   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyR   ú   s   			(*   Rb   t   loggingR   t   warningst   paste.deploy.convertersR    R   t   __all__t	   getLoggerR`   R   t   warnt   legacyt   pylons_database_warningt   DeprecationWarningR'   t   sqlalchemy.extR   t   setR1   R2   R   R:   R(   R    R=   t   SessionContextR?   t   extendt   ImportErrorRJ   t   sqlobject.dbconnectionR@   RA   RB   t   objectR   t   dictRq   t   KeyErrorRe   R   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.6/site-packages/Pylons-0.9.7-py2.6.egg/pylons/database.pyt   <module>   sT   	'		 
R	