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())
class ll.nightshade.UTC[source]

Bases: tzinfo

Timezone object for UTC

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 a datetime.datetime object. If dt.tzinfo is None dt is assumed to be in the local timezone (using the current UTC offset which might be different from the one used by dt).

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. Either connectstring or pool (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 the connect() 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 from ll.orasql and makes it callable just like a CherryPy handler.

__init__(callable, connection)[source]

Create a Call object wrapping the function or procedure callable.

__call__(*args, **kwargs)[source]

Call the procedure/function with the arguments args and kwargs mapping Python function arguments to Oracle procedure/function arguments. On return from the procedure the c_out parameter is mapped to the CherryPy response body, and the parameters p_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) and p_cachecontrol (a string) are mapped to the appropriate CherryPy response headers. If p_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.