ll.nightshade
– Using Oracle with CherryPy
This module provides a class Call
that allows you to use Oracle PL/SQL
procedures/functions as CherryPy response handlers. A Call
objects
wraps a ll.orasql.Procedure
or ll.orasql.Function
object from
the ll.orasql
module.
For example, you might have the following PL/SQL function:
create or replace function helloworld
(
who varchar2
)
return varchar2
as
begin
return '<html><head><h>Hello ' || who || '</h></head><body><h1>Hello, ' || who || '!</h1></body></html>';
end;
Using this function as a CherryPy response handler can be done like this:
import cherrypy
from ll import orasql, nightshade
proc = nightshade.Call(orasql.Function("helloworld"), connectstring="user/pwd")
class HelloWorld:
@cherrypy.expose
def default(self, who="World"):
cherrypy.response.headers["Content-Type"] = "text/html"
return proc(who=who)
cherrypy.quickstart(HelloWorld())
- ll.nightshade.getnow()[source]
Get the current date and time as a
datetime.datetime
object in UTC with timezone info.
- ll.nightshade.httpdate(dt)[source]
Return a string suitable for a “Last-Modified” and “Expires” header.
dt
is adatetime.datetime
object. Ifdt.tzinfo
isNone
dt
is assumed to be in the local timezone (using the current UTC offset which might be different from the one used bydt
).
- class ll.nightshade.Connect[source]
Bases:
object
Connect
objects can be used as decorators that wraps a function that needs a database connection.If calling the wrapped function results in a database exception that has been caused by a lost connection to the database or similar problems, the function is retried with a new database connection.
- __init__(connectstring=None, pool=None, retry=3, **kwargs)[source]
Create a new parameterized
Connect
decorator. Eitherconnectstring
orpool
(a database pool object) must be specified.retry
specifies how often to retry calling the wrapped function after a database exception.kwargs
will be passed on to theconnect()
call.
- class ll.nightshade.Call[source]
Bases:
object
Wrap an Oracle procedure or function in a CherryPy handler.
A
Call
object wraps a procedure or function object fromll.orasql
and makes it callable just like a CherryPy handler.- __init__(callable, connection)[source]
Create a
Call
object wrapping the function or procedurecallable
.
- __call__(*args, **kwargs)[source]
Call the procedure/function with the arguments
args
andkwargs
mapping Python function arguments to Oracle procedure/function arguments. On return from the procedure thec_out
parameter is mapped to the CherryPy response body, and the parametersp_expires
(the number of days from now),p_lastmodified
(a date in UTC),p_mimetype
(a string),p_encoding
(a string),p_etag
(a string) andp_cachecontrol
(a string) are mapped to the appropriate CherryPy response headers. Ifp_etag
is not specified a value is calculated.If the procedure/function raised a PL/SQL exception with a code between 20200 and 20599, 20000 will be subtracted from this value and the resulting value will be used as the HTTP response code, i.e. 20404 will give a “Not Found” response.