def get_workspace_info(self, params): """ Some magic workspace ids. 12345 - the standard one. 678 - doesn't have useful narrative info in its metadata 789 - raises a permissions error 890 - raises a deleted workspace error otherwise, returns workspace info with narrative = 1, and narrative name = 'Fake' """ wsid = params.get('id', 12345) name = params.get('workspace', 'some_workspace') if wsid == 678: return [ wsid, name, 'owner', 'moddate', 'largestid', 'a', 'n', 'unlocked', {} ] elif wsid == 789: raise ServerError("JSONRPCError", -32500, "User you may not read workspace 789") elif wsid == 890: raise ServerError("JSONRPCError", -32500, "Workspace 890 is deleted") elif name != 'invalid_workspace': return [ wsid, name, 'owner', 'moddate', 'largestid', 'a', 'n', 'unlocked', { 'is_temporary': 'false', 'narrative': '1', 'narrative_nice_name': 'Fake' } ] else: raise Exception('not found')
def get_workspace_info(self, params): """ Some magic workspace ids. 12345 (WSID_STANDARD) - the standard one. 678 - doesn't have useful narrative info in its metadata 789 - raises a permissions error 890 - raises a deleted workspace error otherwise, returns workspace info with narrative = 1, and narrative name = 'Fake' """ wsid = params.get("id", WSID_STANDARD) name = params.get("workspace", "some_workspace") if wsid == 678: return [ wsid, name, "owner", "moddate", "largestid", "a", "n", "unlocked", {}, ] elif wsid == 789: raise ServerError( "JSONRPCError", -32500, "User you may not read workspace 789" ) elif wsid == 890: raise ServerError("JSONRPCError", -32500, "Workspace 890 is deleted") elif name != "invalid_workspace": return [ wsid, name, "owner", "moddate", "largestid", "a", "n", "unlocked", { "is_temporary": "false", "narrative": "1", "narrative_nice_name": "Fake", }, ] else: raise Exception("not found")
def alter_workspace_metadata(self, params): ws_id = params['wsi']['id'] self._check_ws_id(ws_id) new_meta = params['new'] if self.fake_ws_db[str(ws_id)]['perms'].get(FAKE_ADMIN_ID, 'n') == 'n': raise ServerError( 'JSONRPCError', -32500, 'User {} may not alter metadata for workspace {}'.format( FAKE_ADMIN_ID, ws_id)) self.fake_ws_db[str(ws_id)]['ws_info'][8].update(new_meta)
def get_job_logs(self, params): """ params: job_id, skip_lines skip_lines = number of lines to skip, get all the rest single line: { is_error 0,1 line: string } """ job_id = params["job_id"] def log_gen(log_params, total_lines=MAX_LOG_LINES): skip = log_params.get("skip_lines", 0) lines = list() if skip < total_lines: for i in range(total_lines - skip): lines.append( {"is_error": 0, "line": "This is line {}".format(i + skip)} ) return {"last_line_number": max(total_lines, skip), "lines": lines} if job_id == JOB_COMPLETED: return log_gen(params, total_lines=MAX_LOG_LINES) job = self.job_state_data.get( job_id, {"job_id": job_id, "status": "does_not_exist"} ) if job["status"] == "does_not_exist": raise ServerError( "JSONRPCError", 99, "Job ID is not registered: " + job["job_id"] ) if job["status"] != COMPLETED_STATUS: raise ServerError( "JSONRPCError", 2, "Cannot find job log with id: " + job["job_id"] ) # otherwise, return five lines of logs return log_gen(params, total_lines=5)
def alter_workspace_metadata(self, params): ws_id = params["wsi"]["id"] self._check_ws_id(ws_id) new_meta = params["new"] if self.fake_ws_db[str(ws_id)]["perms"].get(FAKE_ADMIN_ID, "n") == "n": raise ServerError( "JSONRPCError", -32500, "User {} may not alter metadata for workspace {}".format( FAKE_ADMIN_ID, ws_id), ) self.fake_ws_db[str(ws_id)]["ws_info"][8].update(new_meta)
def mock_read_narrative(style): """ Mocks the NarrativeIO.read_narrative() function. Style should be one of "good", "bad", or "private". A "good" narrative will just return the valid read_narrative() results by loading and returning the given file. (will raise a ValueError if file is None). A "bad" narrative will raise a ValueError, and a "private" style will raise a NarrativeIO.PermissionsError. """ if style == test_narrative_ref: return config.load_json_file(config.get('narrative_refs', 'narr_file')) elif style == bad_narrative_ref: raise ValueError('Bad Narrative!') elif style == private_narrative_ref: raise WorkspaceError(ServerError("Error", -32500, "Private workspace!"), private_narrative_ref)
def _check_ws_id(self, ws_id): if str(ws_id) not in self.fake_ws_db: raise ServerError('JSONRPCError', -32500, 'No workspace with id {} exists'.format(ws_id))
def __init__(self, name=None, code=None, message=None, **kw): ServerError.__init__(self, name, code, message, **kw)
def get_job_logs(self, params): raise ServerError("JSONRPCError", 2, "Can't get job logs")
def check_job_canceled(self, params): raise ServerError("JSONRPCError", 1, "Can't cancel job")
def cancel_job(self, params): raise ServerError("JSONRPCError", -32000, "Can't cancel job")
def check_workspace_jobs(self, params): raise ServerError("JSONRPCError", -32000, "Job lookup failed.")