MCPcopy Create free account
hub / github.com/datapane/datapane / JSONPlugin

Class JSONPlugin

python-client/src/datapane/_vendor/bottle.py:1974–2015  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1972
1973
1974class JSONPlugin(object):
1975 name = 'json'
1976 api = 2
1977
1978 def __init__(self, json_dumps=json_dumps):
1979 self.json_dumps = json_dumps
1980
1981 def setup(self, app):
1982 app.config._define('json.enable', default=True, validate=bool,
1983 help="Enable or disable automatic dict->json filter.")
1984 app.config._define('json.ascii', default=False, validate=bool,
1985 help="Use only 7-bit ASCII characters in output.")
1986 app.config._define('json.indent', default=True, validate=bool,
1987 help="Add whitespace to make json more readable.")
1988 app.config._define('json.dump_func', default=None,
1989 help="If defined, use this function to transform"
1990 " dict into json. The other options no longer"
1991 " apply.")
1992
1993 def apply(self, callback, route):
1994 dumps = self.json_dumps
1995 if not self.json_dumps: return callback
1996
1997 @functools.wraps(callback)
1998 def wrapper(*a, **ka):
1999 try:
2000 rv = callback(*a, **ka)
2001 except HTTPResponse as resp:
2002 rv = resp
2003
2004 if isinstance(rv, dict):
2005 #Attempt to serialize, raises exception on failure
2006 json_response = dumps(rv)
2007 #Set content type only if serialization successful
2008 response.content_type = 'application/json'
2009 return json_response
2010 elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict):
2011 rv.body = dumps(rv.body)
2012 rv.content_type = 'application/json'
2013 return rv
2014
2015 return wrapper
2016
2017
2018class TemplatePlugin(object):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected