def test_missing_env_path(self): backup_dir = os.environ["NARRATIVE_DIR"] del os.environ["NARRATIVE_DIR"] test_wm = WidgetManager() self.assertIsInstance(test_wm.widget_param_map, dict) self.assertFalse(test_wm.widget_param_map) os.environ["NARRATIVE_DIR"] = backup_dir
def _run_widget_app_internal(self, app_id, tag, version, cell_id, run_id): self._send_comm_message('run_status', { 'event': 'validating_app', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) # Intro tests: self.spec_manager.check_app(app_id, tag, raise_exception=True) if version is not None and tag != "release": raise ValueError("App versions only apply to released app modules!") # Get the spec & params spec = self.spec_manager.get_spec(app_id, tag) if 'behavior' not in spec: raise ValueError("This app appears invalid - it has no defined behavior") behavior = spec['behavior'] if 'kb_service_input_mapping' in behavior: # it's a service! Should run this with run_app! raise ValueError('This app appears to be a long-running job! Please start it using the run_app function instead.') if 'script_module' in behavior or 'script_name' in behavior: # It's an old NJS script. These don't work anymore. raise ValueError('This app relies on a service that is now obsolete. Please contact the administrator.') # Here, we just deal with two behaviors: # 1. None of the above - it's a viewer. # 2. ***TODO*** python_class / python_function. Import and exec the python code. # for now, just map the inputs to outputs. # First, validate. # Preflight check the params - all required ones are present, all values are the right type, all numerical values are in given ranges #spec_params = self.spec_manager.app_params(spec) #(params, ws_refs) = self._validate_parameters(app_id, tag, spec_params, kwargs) log_info = { 'app_id': app_id, 'tag': tag, 'username': system_variable('user_id'), 'ws': system_variable('workspace') } kblogging.log_event(self._log, "run_widget_app", log_info) self._send_comm_message('run_status', { 'event': 'success', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) # now just map onto outputs. custom_widget = spec.get('widgets', {}).get('input', None) return WidgetManager().show_custom_widget(custom_widget, app_id, version, tag, spec, cell_id)
def show_output_widget(self, state=None): """ For a complete job, returns the job results. An incomplete job throws an exception """ from biokbase.narrative.widgetmanager import WidgetManager if state is None: state = self.state() if state['job_state'] == 'completed' and 'result' in state: (output_widget, widget_params) = self._get_output_info(state) return WidgetManager().show_output_widget(output_widget, widget_params, tag=self.tag) else: return "Job is incomplete! It has status '{}'".format(state['job_state'])
def setUpClass(self): config = TestConfig() os.environ[ "KB_WORKSPACE_ID"] = "12345" # That's the same workspace as my luggage! app_specs_list = config.load_json_file( config.get("specs", "app_specs_file")) app_specs_dict = dict() for s in app_specs_list: app_specs_dict[s["info"]["id"]] = s self.wm = WidgetManager() self.good_widget = "kbaseTabTable" self.bad_widget = "notAWidget" self.good_tag = "release" self.bad_tag = "notATag" self.widget_with_consts = "kbaseContigSetView"
def setUpClass(self): config = TestConfig() os.environ[ 'KB_WORKSPACE_ID'] = '12345' # That's the same workspace as my luggage! app_specs_list = config.load_json_file( config.get('specs', 'app_specs_file')) app_specs_dict = dict() for s in app_specs_list: app_specs_dict[s['info']['id']] = s self.wm = WidgetManager() self.good_widget = "kbaseTabTable" self.bad_widget = "notAWidget" self.good_tag = "release" self.bad_tag = "notATag" self.widget_with_consts = "kbaseContigSetView"
def setUpClass(self, mock_sm): os.environ[ 'KB_WORKSPACE_ID'] = '12345' # That's the same workspace as my luggage! specs_list = read_json_file('data/specs.json') specs_dict = dict() for s in specs_list: specs_dict[s['info']['id']] = s mock_sm.return_value.app_specs = { 'release': specs_dict, 'beta': specs_dict, 'dev': specs_dict } # mock_njs.return_value.list_methods_spec.return_value = read_narrative_file('data/specs.json') self.wm = WidgetManager() self.good_widget = "kbaseTabTable" self.bad_widget = "notAWidget" self.good_tag = "release" self.bad_tag = "notATag"
def setUpClass(self, mock_sm): config = TestConfig() os.environ[ 'KB_WORKSPACE_ID'] = '12345' # That's the same workspace as my luggage! specs_list = config.load_json_file( config.get('specs', 'app_specs_file')) specs_dict = dict() for s in specs_list: specs_dict[s['info']['id']] = s mock_sm.return_value.app_specs = { 'release': specs_dict, 'beta': specs_dict, 'dev': specs_dict } self.wm = WidgetManager() self.good_widget = "kbaseTabTable" self.bad_widget = "notAWidget" self.good_tag = "release" self.bad_tag = "notATag"
def show_output_widget(self, state=None): """ For a complete job, returns the job results. An incomplete job throws an exception """ from biokbase.narrative.widgetmanager import WidgetManager if not state: state = self.state() else: self._update_state(state) state = self._internal_state() if state["status"] == COMPLETED_STATUS and "job_output" in state: (output_widget, widget_params) = self._get_output_info(state) return WidgetManager().show_output_widget(output_widget, widget_params, tag=self.tag) else: return f"Job is incomplete! It has status '{state['status']}'"
def _run_local_app_internal(self, app_id, params, widget_state, tag, version, cell_id, run_id): self._send_comm_message( 'run_status', { 'event': 'validating_app', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) spec = self._get_validated_app_spec(app_id, tag, False, version=version) # Here, we just deal with two behaviors: # 1. None of the above - it's a viewer. # 2. ***TODO*** python_class / python_function. # Import and exec the python code. # for now, just map the inputs to outputs. # First, validate. # Preflight check the params - all required ones are present, all # values are the right type, all numerical values are in given ranges spec_params = self.spec_manager.app_params(spec) (params, ws_refs) = validate_parameters(app_id, tag, spec_params, params) # Log that we're trying to run a job... log_info = { 'app_id': app_id, 'tag': tag, 'username': system_variable('user_id'), 'ws': system_variable('workspace') } kblogging.log_event(self._log, "run_local_app", log_info) self._send_comm_message( 'run_status', { 'event': 'success', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) (output_widget, widget_params) = map_outputs_from_state([], params, spec) # All a local app does is route the inputs to outputs through the # spec's mapping, and then feed that into the specified output widget. wm = WidgetManager() if widget_state is not None: return wm.show_advanced_viewer_widget(output_widget, widget_params, widget_state, cell_id=cell_id, tag=tag) else: return wm.show_output_widget(output_widget, widget_params, cell_id=cell_id, tag=tag)
def run_local_app( self, app_id, params, tag="release", version=None, cell_id=None, run_id=None, widget_state=None, ): """ Attempts to run a local app. These do not return a Job object, but just the result of the app. In most cases, this will be a Javascript display of the result, but could be anything. If the app_spec looks like it makes a service call, then this raises a ValueError. Otherwise, it validates each parameter in **kwargs against the app spec, executes it, and returns the result. Parameters: ----------- app_id - should be from the app spec, e.g. 'view_expression_profile' params - the dictionary of parameters for the app. Should be key-value pairs where they keys are strings. If any non-optional parameters are missing, an informative string will be printed. tag - optional, one of [release|beta|dev] (default=release) version - optional, a semantic version string. Only released modules have versions, so if the tag is not 'release', and a version is given, a ValueError will be raised. Example: run_local_app('NarrativeViewers/view_expression_profile', { "input_expression_matrix": "MyMatrix", "input_gene_ids": "1234" }, version='0.0.1', input_expression_matrix="MyMatrix") """ spec = self._get_validated_app_spec(app_id, tag, False, version=version) # Here, we just deal with two behaviors: # 1. None of the above - it's a viewer. # 2. ***TODO*** python_class / python_function. # Import and exec the python code. # for now, just map the inputs to outputs. # First, validate. # Preflight check the params - all required ones are present, all # values are the right type, all numerical values are in given ranges spec_params = self.spec_manager.app_params(spec) (params, ws_refs) = validate_parameters(app_id, tag, spec_params, params) # Log that we're trying to run a job... log_info = { "app_id": app_id, "tag": tag, "username": system_variable("user_id"), "ws": system_variable("workspace"), } kblogging.log_event(self._log, "run_local_app", log_info) self._send_comm_message( MESSAGE_TYPE["RUN_STATUS"], { "event": "success", "event_at": timestamp(), "cell_id": cell_id, "run_id": run_id, }, ) (output_widget, widget_params) = map_outputs_from_state([], params, spec) # All a local app does is route the inputs to outputs through the # spec's mapping, and then feed that into the specified output widget. wm = WidgetManager() if widget_state is not None: return wm.show_advanced_viewer_widget( output_widget, widget_params, widget_state, cell_id=cell_id, tag=tag ) else: return wm.show_output_widget( output_widget, widget_params, cell_id=cell_id, tag=tag )
def _run_local_app_advanced_internal(self, app_id, params, widget_state, tag, version, cell_id, run_id, **kwargs): self._send_comm_message( 'run_status', { 'event': 'validating_app', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) # Intro tests: self.spec_manager.check_app(app_id, tag, raise_exception=True) if version is not None and tag != "release": raise ValueError("App versions only apply to released modules!") # Get the spec & params spec = self.spec_manager.get_spec(app_id, tag) if 'behavior' not in spec: raise ValueError("This app appears invalid - " + "it has no defined behavior") behavior = spec['behavior'] if 'script_module' in behavior or 'script_name' in behavior: # It's an old NJS script. These don't work anymore. raise ValueError('This app relies on a service that is now ' + 'obsolete. Please contact the administrator.') # Here, we just deal with two behaviors: # 1. None of the above - it's a viewer. # 2. ***TODO*** python_class / python_function. # Import and exec the python code. # for now, just map the inputs to outputs. # First, validate. # Preflight check the params - all required ones are present, all # values are the right type, all numerical values are in given ranges spec_params = self.spec_manager.app_params(spec) (params, ws_refs) = self._validate_parameters(app_id, tag, spec_params, params) # Log that we're trying to run a job... log_info = { 'app_id': app_id, 'tag': tag, 'username': system_variable('user_id'), 'ws': system_variable('workspace') } kblogging.log_event(self._log, "run_local_app", log_info) self._send_comm_message( 'run_status', { 'event': 'success', 'event_at': datetime.datetime.utcnow().isoformat() + 'Z', 'cell_id': cell_id, 'run_id': run_id }) (output_widget, widget_params) = map_outputs_from_state([], params, spec) # All a local app does is route the inputs to outputs through the # spec's mapping, and then feed that into the specified output widget. return WidgetManager().show_advanced_viewer_widget(output_widget, widget_params, widget_state, cell_id=cell_id, tag=tag)