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.datetimeobject in UTC with timezone info.
- ll.nightshade.httpdate(dt)[source]
Return a string suitable for a “Last-Modified” and “Expires” header.
dtis adatetime.datetimeobject. Ifdt.tzinfoisNonedtis 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:
objectConnectobjects 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
Connectdecorator. Eitherconnectstringorpool(a database pool object) must be specified.retryspecifies how often to retry calling the wrapped function after a database exception.kwargswill be passed on to theconnect()call.
- class ll.nightshade.Call[source]
Bases:
objectWrap an Oracle procedure or function in a CherryPy handler.
A
Callobject wraps a procedure or function object fromll.orasqland makes it callable just like a CherryPy handler.- __init__(callable, connection)[source]
Create a
Callobject wrapping the function or procedurecallable.
- __call__(*args, **kwargs)[source]
Call the procedure/function with the arguments
argsandkwargsmapping Python function arguments to Oracle procedure/function arguments. On return from the procedure thec_outparameter 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_etagis 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.