def get_props(model_id, indra_dir): try: model = get_model(model_id, indra_dir=indra_dir) with open(indra_dir + "/" + model["props"]) as file: return json.loads(file.read()) except (IndexError, KeyError, ValueError): return err_return("Invalid model id " + str(model_id)) except FileNotFoundError: return err_return("Models or props file not found")
def get_props_for_current_execution(model_id, indra_dir): try: execution_key = execution_registry.create_new_execution_registry() model = get_model(model_id, indra_dir=indra_dir) with open(indra_dir + "/" + model["props"]) as file: props = json.loads(file.read()) execution_registry.set_propargs(execution_key, props) props[EXEC_KEY] = \ execution_registry.get_execution_key_as_prop(execution_key) return props except (IndexError, KeyError, ValueError): return err_return("Invalid model id " + str(model_id)) except FileNotFoundError: # noqa: F821 return err_return("Models or props file not found")
def get_props(model_id, indra_dir): try: model = get_model_by_id(model_id, indra_dir=indra_dir) if model is None: return err_return(f"Model id {model_id} not found.") prop_file = get_prop_path(model["module"], model["package"]) print("prop_file = ", prop_file) with open(prop_file) as file: props = json.loads(file.read()) return props except (IndexError, KeyError, ValueError): return err_return("Invalid model id " + str(model_id)) except FileNotFoundError: # noqa: F821 return err_return("Models or props file not found")
def delete(self, exec_key): print("Clearing registry for key - {}".format(exec_key)) try: registry.del_exec_env(exec_key) except KeyError: return err_return( "Key - {} does not exist in registry".format(exec_key)) return {'success': True}
def create_model(model_id, props, indra_dir): """ We get some props and create a model in response. """ model_rec = get_model_by_id(model_id, indra_dir=indra_dir) if model_rec is not None: return module_from_model(model_rec).create_model(props=props) else: return err_return("Model not found: " + str(model_id))
def get(self, execution_key): print("Clearing registry for key - {}".format(execution_key)) try: execution_registry \ .clear_data_for_execution_key(execution_key) except KeyError: return err_return( "Key - {} does not exist in registry".format(execution_key)) return {'success': True}
def get(self): """ Get agent by name from the registry. """ name = request.args.get('name') exec_key = request.args.get('exec_key') if name is None: return err_return("You must pass an agent name.") agent = get_agent(name, exec_key) if agent is None: raise (wz.NotFound(f"Agent {name} not found.")) # trying out raising an exception so comment dis out: # return err_return(f"Agent {name} not found.") return agent.to_json()
def put(self, run_time): """ Put a model env to the server and run it `run_time` periods. Catch all possible exceptions to keep the server responsive. """ try: exec_key = api.payload['exec_key'] print(f'Executing for key {exec_key}') model = run_model(api.payload, run_time, indra_dir) if model is None: return err_return(f"Model not found: {api.payload['module']}") registry.save_reg(exec_key) return json_converter(model) except Exception as err: raise wz.InternalServerError(f"Server error: {str(err)}")
def test_err_return(self): """ Testing whether we are able to get the right error message """ rv = err_return("error message") self.assertEqual(rv, {"Error:": "error message"})