Exemplo n.º 1
0
 def test_normal_cases(self):
     s = "Hi!  I'm a string!!1!"
     l = [1, True, None, s, {"wx":"yz"}]
     d = {"foo":"bar","baz":None,"a":3,"false":True,"list":l}
     for x in [s,l,d]:
         self.assertEqual(snekjson.dumps(x), json.dumps(x))
         xl = json.dumps(x)
         self.assertEqual(snekjson.loads(xl), json.loads(xl))
Exemplo n.º 2
0
 def newfunc(event, *args, **kwargs):
     response = func(event, *args, **kwargs)
     if ui_stuff.is_response(response):
         return response
     return response_core.make_response(
         body=snekjson.dumps(response,
                             separators=(',', ':'),
                             ignore_type_error=True),
         headers={"Content-Type": "application/json"})
Exemplo n.º 3
0
def make_debug(event=None, context=None, headers={}, **kwargs):
    templates = env.list_templates()
    return make_response(
        body="<pre>\n{}\n\nAvailable Jinja2 templates:\n\n{}\n</pre>".format(
            snekjson.dumps(event,
                           indent=2,
                           sort_keys=True,
                           ignore_type_error=True).replace(">", "&gt").replace(
                               "<", "&lt"), "\n".join(templates)),
        headers=headers)
Exemplo n.º 4
0
def get_debug_blob(event):
    templates = env.list_templates()
    event = add_event_params(event)
    event["debug"] = {}
    event["debug"]["matchers"] = response_core.get_matchers_debug_blob()
    event["debug"]["events.base_path(event)"] = events.base_path(event)
    event["debug"]["events.page_path(event)"] = events.page_path(event)
    return "<pre>\n{}\n\nAvailable Jinja2 templates:\n\n{}\n</pre>".format(
        snekjson.dumps(event, indent=2, sort_keys=True,
                       ignore_type_error=True).replace(">", "&gt").replace(
                           "<", "&lt"), "\n".join(templates))
Exemplo n.º 5
0
def make_ddb_safe(item):
    if isinstance(item, list):
        item = [make_ddb_safe(e) for e in item]
    if isinstance(item, dict):
        item = {k: make_ddb_safe(item[k]) for k in item}
    if isinstance(item, float):
        item = Decimal(item)
    if isinstance(item, datetime):
        item = item.strftime(STRFTIME_STRING)
    if item is None:
        item = "null"
    if item == "":
        item = json.dumps("")
    return item
Exemplo n.º 6
0
 def newfunc(event, *args, **kwargs):
     global PAGE_CACHE
     keyvals = {k: kwargs.get(k) for k in kwargs if k in keys}
     keyvals["__funcname__"] = func.__name__
     key = snekjson.dumps(keyvals,
                          separators=(',', ':'),
                          ignore_type_error=True,
                          sort_keys=True)
     cached_page = PAGE_CACHE.get(key, {})
     if cached_page:
         if cached_page.get("expires", 0) > time.time():
             print("Using cached response with key '{}'".format(key))
             return cached_page["response"]
         else:
             print("Cached response expired for key '{}'".format(key))
     response = func(event, *args, **kwargs)
     PAGE_CACHE[key] = {
         "expires": time.time() + seconds,
         "response": response
     }
     return response
Exemplo n.º 7
0
def get_debug_blob(event):
    templates = env.list_templates()
    return "<pre>\n{}\n\nAvailable Jinja2 templates:\n\n{}\n</pre>".format(
        snekjson.dumps(event, indent=2, sort_keys=True,
                       ignore_type_error=True).replace(">", "&gt").replace(
                           "<", "&lt"), "\n".join(templates))
Exemplo n.º 8
0
def dumps(obj, *args, **kwargs):
    return json.dumps(make_json_safe(obj), *args, **kwargs)