def call(params): function = params.get('_function', '') module_name = params.get('_module', '') parameters = dict(params) for k in parameters.keys(): if k.startswith('_') or k.startswith('pico_'): del parameters[k] else: try: parameters[k] = json.loads(parameters[k]) except Exception: try: parameters[k] = json.loads(parameters[k].replace("'", '"')) except Exception: parameters[k] = parameters[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')) response = Response() if module_name == 'pico': if function == 'authenticate': response.content = authenticate(params) elif 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) authenticated_user = authenticate(params, module) for k in parameters: parameters[k] = pico.from_json(parameters[k], getattr(module, "json_loaders", [])) if class_name: init_args = map(lambda s: pico.from_json(s, getattr(module, "json_loaders", [])), init_args) response = call_method(module, class_name, function, parameters, init_args) else: response = call_function(module, function, parameters, authenticated_user) 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
def call(params): function = params.get('_function', '') module_name = params.get('_module', '') parameters = dict(params) for k in parameters.keys(): if k.startswith('_') or k.startswith('pico_'): del parameters[k] else: try: parameters[k] = json.loads(parameters[k]) except Exception: try: parameters[k] = json.loads(parameters[k].replace("'", '"')) except Exception: parameters[k] = parameters[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', 'false')) response = Response() if module_name == 'pico': if function == 'authenticate': response.content = authenticate(params) elif usecache and os.path.exists(CACHE_PATH): try: response.content = open(CACHE_PATH + cache_key(params)) response.from_cache = True log("Serving from cache") except IOError: pass elif not response.content: module = load_module(module_name) authenticated_user = authenticate(params, module) # parameters = map(lambda s: from_json(s, getattr(module, "json_loaders", [])), parameters) if class_name: init_args = map( lambda s: pico.from_json(s, getattr(module, "json_loaders", []) ), init_args) response = call_method(module, class_name, function, parameters, init_args) else: response = call_function(module, function, parameters, authenticated_user) 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), 'w') out = response.content if hasattr(out, 'read'): out = out.read() response.content.seek(0) f.write(out) f.close() response.callback = callback return response
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
def _call(self, module_name, function_name, args, class_name=None, init_args=None): module = pico.modules.load(module_name) 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 = self.call_method(module, class_name, function_name, args, init_args) else: response = self.call_function(module, function_name, args) response.json_dumpers = getattr(module, "json_dumpers", {}) return response