def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False): """ Uploads a Narrative from a downloaded object file. This file needs to be in JSON format, and it expects all data and info that is usually returned by the Workspace.get_objects method. Returns a dict of three elements: ws: the id of the workspace that was created obj: the id of the narrative object ref: the above two joined together into an object ref (for convenience) """ # read the file f = open(nar_file, "r") nar = json.loads(f.read()) f.close() # do some setup. current_nar_metadata = ws_metadata current_nar_metadata["narrative_nice_name"] = nar["data"]["metadata"]["name"] ws_client = Workspace(url=url, token=auth_token) # create the new workspace for the narrative ws_info = ws_client.create_workspace( { "workspace": "{}:{}".format(user_id, str(time.time()).replace(".", "")), "meta": current_nar_metadata, "globalread": "r" if set_public else "n", } ) ws_id = ws_info[0] # setup and save the narrative object nar["info"][10] ws_save_obj = { "type": "KBaseNarrative.Narrative", "data": nar["data"], "name": nar["info"][1], "meta": nar["info"][10], "provenance": [ { "script": "upload_narrative_test.py", "description": "Temporary Narrative uploaded for automated testing", } ], } obj_info = ws_client.save_objects({"id": ws_id, "objects": [ws_save_obj]}) # tweak the workspace's metadata to properly present its narrative ws_client.alter_workspace_metadata( {"wsi": {"id": ws_id}, "new": {"narrative": obj_info[0][0]}} ) return { "ws": ws_info[0], "obj": obj_info[0][0], "refstr": "{}/{}".format(ws_info[0], obj_info[0][0]), "ref": NarrativeRef({"wsid": ws_info[0], "objid": obj_info[0][0]}), }
def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False): """ Uploads a Narrative from a downloaded object file. This file needs to be in JSON format, and it expects all data and info that is usually returned by the Workspace.get_objects method. Returns a dict of three elements: ws: the id of the workspace that was created obj: the id of the narrative object ref: the above two joined together into an object ref (for convenience) """ # read the file f = open(nar_file, 'r') nar = json.loads(f.read()) f.close() # do some setup. current_nar_metadata = ws_metadata current_nar_metadata['narrative_nice_name'] = nar['data']['metadata']['name'] ws_client = Workspace(url=url, token=auth_token) # create the new workspace for the narrative ws_info = ws_client.create_workspace({ 'workspace': '{}:{}'.format(user_id, str(time.time()).replace('.', '')), 'meta': current_nar_metadata, 'globalread': 'r' if set_public else 'n' }) ws_id = ws_info[0] # setup and save the narrative object metadata = nar['info'][10] ws_save_obj = { 'type': 'KBaseNarrative.Narrative', 'data': nar['data'], 'name': nar['info'][1], 'meta': nar['info'][10], 'provenance': [{ 'script': 'upload_narrative_test.py', 'description': 'Temporary Narrative uploaded for automated testing' }] } obj_info = ws_client.save_objects({'id': ws_id, 'objects': [ws_save_obj]}) # tweak the workspace's metadata to properly present its narrative ws_client.alter_workspace_metadata({'wsi': {'id': ws_id}, 'new': {'narrative': obj_info[0][0]}}) return { 'ws': ws_info[0], 'obj': obj_info[0][0], 'refstr': '{}/{}'.format(ws_info[0], obj_info[0][0]), 'ref': NarrativeRef({'wsid': ws_info[0], 'objid': obj_info[0][0]}) }