Wiki Navigation

Dejavu is an Object-Relational Mapper library, supporting MySQL, PostgreSQL, SQLite, Microsoft Access, Microsoft SQL Server, shelve, and custom stores. To use it with CherryPy, we create a new sandbox for each request, using a tool which then cleans it up for us automatically:

import types
import cherrypy
from cherrypy._cperror import format_exc
defflush_sandbox():
if hasattr(cherrypy.request, "sandbox"):
try:
try:
cherrypy.request.sandbox.flush_all()
except:
cherrypy.log(format_exc(), "DEJAVU")
finally:
del cherrypy.request.sandbox
deftry_flush_sandbox():
if cherrypy.response.stream:
# If the body is being streamed, we have to save the data
# *after* the response has been written out
cherrypy.request.hooks.attach('on_end_request', flush_sandbox)
else:
# If the body is not being streamed, we save the data now
if isinstance(cherrypy.response.body, types.GeneratorType):
cherrypy.response.collapse_body()
flush_sandbox()
class SandboxTool(cherrypy.Tool):
def_setup(self):
cherrypy.request.sandbox = arena.new_sandbox()
cherrypy.Tool._setup(self)
cherrypy.tools.sandbox = SandboxTool('on_end_resource', try_flush_sandbox)

Turn it on and then reference cherrypy.request.sandbox in your page handlers. In your startup script (the one that calls cherrypy.server.start), write:

myapp.arena.load(r"C:\myapp\myappsm.conf")

The filename should be that of a Dejavu configuration file. How's *that* for simple integration? :)

Her's a simple example demonstrating how to use the sandbox in your page handler: