You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2017. It is now read-only.
[http://www.aminus.net/dejavu/ 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:
{{{
#!python
import types
import cherrypy
from cherrypy._cperror import format_exc
def flush_sandbox():
if hasattr(cherrypy.request, "sandbox"):
try:
try:
cherrypy.request.sandbox.flush_all()
except:
cherrypy.log(format_exc(), "DEJAVU")
finally:
del cherrypy.request.sandbox
def try_flush_sandbox():
if cherrypy.response.stream:
# If the body is being streamed, we have to save the data
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:
{{{
#!python
import dejavu
from dejavu import Unit, UnitProperty
# global dejavu arena which will handle
# all the dejavu work
arena = dejavu.Arena()
# Creates a specific unit that will handle our messages
# This will have only one single field
# that will be translated into a column
# in the database we'll be using
class Message(Unit):
# Note two things:
# 1. We do not explicitly specify an ID field
# as dejavu does it already
# 2. The property is per-class not per-instance
content = UnitProperty(unicode)
class Root(object):
def __init__(self):
# Purely for this example purpose we enable dejavu's log
# to see what SQL queries dejavu generates
arena.logflags += dejavu.logflags.SQL
arena.logflags += dejavu.logflags.FORGET
# Add a store to the dejavu arena
# The storage we choose is a SQLite in-memory database