"""Yet another WSGI micro-framework...""" |
"""Yet another WSGI micro-framework...""" |
|
|
import cgi, string, new, sys |
import cgi, string, new, sys |
from wsgiref.util import shift_path_info, application_uri |
from wsgiref.util import shift_path_info, application_uri |
|
from peak import context |
|
|
__all__ = [ |
__all__ = [ |
"Page", "form_handler", "HTML", "Text", "Template", "HTTP", "expose", |
"Page", "form_handler", "HTML", "Text", "Template", "HTTP", "expose", |
"test", "Redirector", "EvalTemplate", "EvalMap", "Method", |
"test", "Redirector", "EvalTemplate", "EvalMap", "Method", "DB" |
] |
] |
|
|
class Method(object): |
class Method(object): |
|
|
|
|
|
|
|
class DB(context.Service): |
db = None # DBAPI database connection object |
db = None # DBAPI database connection object |
|
|
def db_connect(self): |
def db_connect(self): |
"""Override this in a subclass to return a DBAPI connection object""" |
"""Override this in a subclass to return a DBAPI connection object""" |
raise NotImplementedError |
raise NotImplementedError |
|
|
|
def get_db(self): |
|
if self.db is None: |
|
self.db = self.db_connect() |
|
return self.db |
|
|
def cursor(self, *args, **kw): |
def cursor(self, *args, **kw): |
"""Create and return a cursor (after optionally running a query on it) |
"""Create and return a cursor (after optionally running a query on it) |
|
|
used to set cursor attributes prior to the ``execute()`` (if |
used to set cursor attributes prior to the ``execute()`` (if |
applicable). |
applicable). |
""" |
""" |
db = self.db |
cursor = self.get_db().cursor() |
if db is None: |
|
db = self.db = self.db_connect() |
|
|
|
cursor = db.cursor() |
|
for k, v in kw.items(): |
for k, v in kw.items(): |
setattr(cursor, k, v) |
setattr(cursor, k, v) |
|
|
self.__dict__ = dict(zip([d[0]for d in cursor.description], row)) |
self.__dict__ = dict(zip([d[0]for d in cursor.description], row)) |
|
|
|
|
|
|
|
|
def test(app, environ={}, form={}, **kw): |
def test(app, environ={}, form={}, **kw): |
"""Print the output of a WSGI app |
"""Print the output of a WSGI app |
|
|