Add an adapter for pecan.
This commit is contained in:
parent
a5e63f0aed
commit
0f0116be78
75
wsme/pecan.py
Normal file
75
wsme/pecan.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import inspect
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
import xml.etree.ElementTree as et
|
||||||
|
|
||||||
|
import wsme
|
||||||
|
import wsme.protocols.commons
|
||||||
|
import wsme.protocols.restjson
|
||||||
|
import wsme.protocols.restxml
|
||||||
|
|
||||||
|
pecan = sys.modules['pecan']
|
||||||
|
|
||||||
|
|
||||||
|
class JSonRenderer(object):
|
||||||
|
def __init__(self, path, extra_vars):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def render(self, template_path, namespace):
|
||||||
|
data = wsme.protocols.restjson.tojson(
|
||||||
|
namespace['datatype'],
|
||||||
|
namespace['result']
|
||||||
|
)
|
||||||
|
return json.dumps(data)
|
||||||
|
|
||||||
|
|
||||||
|
class XMLRenderer(object):
|
||||||
|
def __init__(self, path, extra_vars):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def render(self, template_path, namespace):
|
||||||
|
data = wsme.protocols.restxml.toxml(
|
||||||
|
namespace['datatype'],
|
||||||
|
'result',
|
||||||
|
namespace['result']
|
||||||
|
)
|
||||||
|
return et.tostring(data)
|
||||||
|
|
||||||
|
pecan.templating._builtin_renderers['wsmejson'] = JSonRenderer
|
||||||
|
pecan.templating._builtin_renderers['wsmexml'] = XMLRenderer
|
||||||
|
|
||||||
|
|
||||||
|
def wsexpose(*args, **kwargs):
|
||||||
|
pecan_json_decorate = pecan.expose(
|
||||||
|
template='wsmejson:',
|
||||||
|
content_type='application/json',
|
||||||
|
generic=False)
|
||||||
|
pecan_xml_decorate = pecan.expose(
|
||||||
|
template='wsmexml:',
|
||||||
|
content_type='application/xml',
|
||||||
|
generic=False
|
||||||
|
)
|
||||||
|
sig = wsme.sig(*args, **kwargs)
|
||||||
|
|
||||||
|
def decorate(f):
|
||||||
|
sig(f)
|
||||||
|
funcdef = wsme.api.FunctionDefinition.get(f)
|
||||||
|
|
||||||
|
def callfunction(self, *args, **kwargs):
|
||||||
|
args, kwargs = wsme.protocols.commons.get_args(
|
||||||
|
funcdef, args, kwargs
|
||||||
|
)
|
||||||
|
result = f(self, *args, **kwargs)
|
||||||
|
return dict(
|
||||||
|
datatype=funcdef.return_type,
|
||||||
|
result=result
|
||||||
|
)
|
||||||
|
|
||||||
|
pecan_json_decorate(callfunction)
|
||||||
|
pecan_xml_decorate(callfunction)
|
||||||
|
pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f)
|
||||||
|
return callfunction
|
||||||
|
|
||||||
|
return decorate
|
Loading…
x
Reference in New Issue
Block a user