Exemplo n.º 1
0
def call(params, request):
    func = params.get('_function', '')
    module_name = params.get('_module', '')
    args = {}
    for k in params.keys():
        if not (k.startswith('_') or k.startswith('pico_')):
            params[k] = params[k]
            try:
                args[k] = json.loads(params[k])
            except Exception:
                try:
                    args[k] = json.loads(params[k].replace("'", '"'))
                except Exception:
                    args[k] = params[k]
    callback = params.get('_callback', None)
    init_args = json.loads(params.get('_init', '[]'))
    class_name = params.get('_class', None)
    usecache = json.loads(params.get('_usecache', 'true'))
    x_session_id = params.get('_x_session_id', None)
    if x_session_id:
        request['X-SESSION-ID'] = x_session_id
    response = Response()
    if usecache and os.path.exists(CACHE_PATH):
        try:
            response = serve_file(CACHE_PATH + cache_key(params))
            log("Serving from cache")
        except OSError:
            pass
    if not response.content:
        module = pico.modules.load(module_name, RELOAD)
        json_loaders = getattr(module, "json_loaders", [])
        from_json = lambda s: pico.from_json(s, json_loaders)
        for k in args:
            args[k] = from_json(args[k])
        if class_name:
            init_args = map(from_json, init_args)
            response = call_method(module, class_name, func, args, init_args)
        else:
            response = call_function(module, func, args)
        response.json_dumpers = getattr(module, "json_dumpers", {})
        log(usecache, response.cacheable)
        if usecache and response.cacheable:
            log("Saving to cache")
            try:
                os.stat(CACHE_PATH)
            except Exception:
                os.mkdir(CACHE_PATH)
            f = open(CACHE_PATH + cache_key(params) + '.json', 'w')
            out = response.output
            if hasattr(out, 'read'):
                out = out.read()
                response.output.seek(0)
            else:
                out = out[0]
            f.write(out)
            f.close()
    response.callback = callback
    return response
Exemplo n.º 2
0
def call(params, request):
    func = params.get('_function', '')
    module_name = params.get('_module', '')
    args = {}
    for k in params.keys():
        if not (k.startswith('_') or k.startswith('pico_')):
            params[k] = params[k]
            try:
                args[k] = json.loads(params[k])
            except Exception:
                try:
                    args[k] = json.loads(params[k].replace("'", '"'))
                except Exception:
                    args[k] = params[k]
    callback = params.get('_callback', None)
    init_args = json.loads(params.get('_init', '[]'))
    class_name = params.get('_class', None)
    usecache = json.loads(params.get('_usecache', 'true'))
    x_session_id = params.get('_x_session_id', None)
    if x_session_id:
        request['X-SESSION-ID'] = x_session_id
    response = Response()
    if usecache and os.path.exists(CACHE_PATH):
        try:
            response = serve_file(CACHE_PATH + cache_key(params))
            log("Serving from cache")
        except OSError:
            pass
    if not response.content:
        module = pico.modules.load(module_name, RELOAD)
        json_loaders = getattr(module, "json_loaders", [])
        from_json = lambda s: pico.from_json(s, json_loaders)
        for k in args:
            args[k] = from_json(args[k])
        if class_name:
            init_args = map(from_json, init_args)
            response = call_method(module, class_name, func, args, init_args)
        else:
            response = call_function(module, func, args)
        response.json_dumpers = getattr(module, "json_dumpers", {})
        log(usecache, response.cacheable)
        if usecache and response.cacheable:
            log("Saving to cache")
            try:
                os.stat(CACHE_PATH)
            except Exception:
                os.mkdir(CACHE_PATH)
            f = open(CACHE_PATH + cache_key(params) + '.json', 'w')
            out = response.output
            if hasattr(out, 'read'):
                out = out.read()
                response.output.seek(0)
            else:
                out = out[0]
            f.write(out)
            f.close()
    response.callback = callback
    return response