Changeset 10:eff227edc39f in livinglogic.python.nightshade

Show
Ignore:
Timestamp:
05/11/06 15:26:45 (14 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add connections as an alternate way to get a cursor in Proc.

Change docstrings to have a usable first line.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • nightshade.py

    r9 r10  
    1313def httpdate(dt): 
    1414    """ 
    15     Return a string suitable for a <z>Last-Modified</z> and <z>Expires</z> header. 
    16     <arg>dt</arg> is a <class>datetime.datetime</class> object. 
     15    <par>Return a string suitable for a <z>Last-Modified</z> and <z>Expires</z> header.</par> 
     16     
     17    <par><arg>dt</arg> is a <class>datetime.datetime</class> object.</par> 
    1718    """ 
    1819    return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (weekdayname[dt.weekday()], dt.day, monthname[dt.month], dt.year, dt.hour, dt.minute, dt.second) 
     
    2122class cache(object): 
    2223    """ 
    23     <function>cache</function> can be used as a decorator with arguments for 
    24     CherryPy functions. The returned wrapper function will cache return values 
    25     of the decorated function for a certain amount of time. You can pass 
     24    <par>Decorator that adds caching to a CherryPy handler.</par> 
     25     
     26    <par>Calling a <class>cache</class> object is callable will cache return 
     27    values of the decorated function for a certain amount of time. You can pass 
    2628    the timespan either via <arg>timedelta</arg> (which must be a 
    2729    <class>datetime.timedelta</class> object), or via <arg>timedeltaargs</arg> 
     
    7274def conditional(func): 
    7375    """ 
    74     <function>conditional</function> can be used as a decorator for CherryPy 
    75     functions. It adds proper handling of conditional <lit>GET</lit>s to the 
    76     decorated function (i.e. it correctly handles the <lit>If-Modified-Since</lit> 
     76    <par>Decorator that adds handling of  conditional <lit>GET</lit>s to a CherryPy handler.</par> 
     77     
     78    <par>The decorated function will correctly handle the <lit>If-Modified-Since</lit> 
    7779    and <lit>If-None-Match</lit> request headers. For this to work properly 
    7880    the decorated function should set the <lit>Last-Modified</lit> and/or 
     
    107109class Proc(object): 
    108110    """ 
    109     <class>Proc</class> wraps a procedure object from 
     111    <par>Wrap an Oracle procedure in a CherryPy handler.</par> 
     112 
     113    <par><class>Proc</class> object wraps a procedure object from 
    110114    <pyref module="ll.orasql"><module>ll.orasql</module></pyref> and make it 
    111     usable as a CherryPy handler. 
     115    callable just like a CherryPy handler. 
    112116    """ 
    113     def __init__(self, pool, proc): 
    114         self.pool = pool 
     117    def __init__(self, proc, pool=None, connection=None): 
     118        """ 
     119        Create a  
     120        """ 
     121        if (pool is not None) == (connection is not None): 
     122            raise TypeError("either pool or connection must be specified") 
    115123        self.proc = proc 
    116124        # Calculate parameter mapping now, so we don't get concurrency problems later 
    117125        proc._calcrealargs(pool.acquire().cursor()) 
     126        self.pool = pool 
     127        self.connection = connection 
    118128 
    119129    def __call__(self, *args, **kwargs): 
     
    130140         
    131141        now = datetime.datetime.utcnow() 
    132         while 1: 
    133             connection = self.pool.acquire() 
     142        if self.pool is not None: 
     143            while 1: 
     144                connection = self.pool.acquire() 
     145                cursor = connection.cursor() 
     146                try: 
     147                    result = self.proc(cursor, *args, **kwargs) 
     148                except cx_Oracle.DatabaseError, exc: 
     149                    if "ORA-03114" in str(exc): 
     150                        # Drop dead connection and retry 
     151                        self.pool.drop(connection) 
     152                    else: 
     153                        raise 
     154                else: 
     155                    break 
     156        else: 
    134157            cursor = connection.cursor() 
    135             try: 
    136                 result = self.proc(cursor, *args, **kwargs) 
    137             except cx_Oracle.DatabaseError, exc: 
    138                 if "ORA-03114" in str(exc): 
    139                     # Drop dead connection and retry 
    140                     self.pool.drop(connection) 
    141                 else: 
    142                     raise 
    143             else: 
    144                 break 
     158            result = self.proc(cursor, *args, **kwargs) 
    145159 
    146160        # Set HTTP headers from parameters