Skip to content

Commit

Permalink
Add an optional 'title' flag to the create_new_narrative function
Browse files Browse the repository at this point in the history
  • Loading branch information
briehl committed Sep 15, 2017
1 parent 0489b97 commit 5580ea2
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 69 deletions.
11 changes: 7 additions & 4 deletions NarrativeService.spec
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module NarrativeService {
mapping<string, string> meta> object_info;

/* Information about a workspace.

ws_id id - the numerical ID of the workspace.
ws_name workspace - name of the workspace.
username owner - name of the user who owns (e.g. created) this workspace.
Expand All @@ -66,7 +66,7 @@ module NarrativeService {
lock_status lockstat - the status of the workspace lock.
usermeta metadata - arbitrary user-supplied metadata about
the workspace.

*/
typedef tuple<int id, string workspace, string owner, string moddate,
int max_objid, string user_permission, string globalread,
Expand All @@ -78,7 +78,7 @@ module NarrativeService {

/*
ref - reference to any DataPalette container pointing to given object,
refs - list of references to all DataPalette containers pointing to
refs - list of references to all DataPalette containers pointing to
given object.
*/
typedef structure {
Expand Down Expand Up @@ -222,6 +222,8 @@ module NarrativeService {
copydata - packed inport data in format "import(;...)*" (alternative to importData)
importData - import data in unpacked form (alternative to copydata)
includeIntroCell - if 1, adds an introductory markdown cell at the top (optional, default 0)
title - name of the new narrative (optional, if a string besides 'Untitled', this will
mark the narrative as not temporary, so it will appear in the dashboard)
*/
typedef structure {
string app;
Expand All @@ -232,6 +234,7 @@ module NarrativeService {
string copydata;
list<string> importData;
boolean includeIntroCell;
string title;
} CreateNewNarrativeParams;

typedef structure {
Expand Down Expand Up @@ -306,7 +309,7 @@ module NarrativeService {
returns (NarratorialList) authentication optional;




typedef structure {
workspace_info ws;
Expand Down
68 changes: 47 additions & 21 deletions lib/NarrativeService/NarrativeManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def copy_narrative(self, newName, workspaceRef, workspaceId):
raise # continue raising previous exception

def create_new_narrative(self, app, method, appparam, appData, markdown,
copydata, importData, includeIntroCell):
copydata, importData, includeIntroCell, title):
if app and method:
raise ValueError("Must provide no more than one of the app or method params")

Expand All @@ -257,7 +257,11 @@ def create_new_narrative(self, app, method, appparam, appData, markdown,
cells = [{"method": method}]
elif markdown:
cells = [{"markdown": markdown}]
return self._create_temp_narrative(cells, appData, importData, includeIntroCell)
narr_info = self._create_temp_narrative(cells, appData, importData, includeIntroCell, title)
if title is not None:
# update workspace info so it's not temporary
pass
return narr_info

def _get_intro_markdown(self):
"""
Expand All @@ -268,17 +272,17 @@ def _get_intro_markdown(self):
intro_md = intro_file.read()
return intro_md

def _create_temp_narrative(self, cells, parameters, importData, includeIntroCell):
def _create_temp_narrative(self, cells, parameters, importData, includeIntroCell, title):
# Migration to python of JavaScript class from https://github.com/kbase/kbase-ui/blob/4d31151d13de0278765a69b2b09f3bcf0e832409/src/client/modules/plugins/narrativemanager/modules/narrativeManager.js#L414
narr_id = int(round(time.time() * 1000))
workspaceName = self.user_id + ':narrative_' + str(narr_id)
narrativeName = "Narrative." + str(narr_id)

ws = self.ws
ws_info = ws.create_workspace({'workspace': workspaceName, 'description': ''})
newWorkspaceInfo = ServiceUtils.workspaceInfoToObject(ws_info)
[narrativeObject, metadataExternal] = self._fetchNarrativeObjects(workspaceName, cells,
parameters, includeIntroCell)
[narrativeObject, metadataExternal] = self._fetchNarrativeObjects(
workspaceName, cells, parameters, includeIntroCell, title
)
objectInfo = ws.save_objects({'workspace': workspaceName,
'objects': [{'type': 'KBaseNarrative.Narrative',
'data': narrativeObject,
Expand All @@ -289,12 +293,22 @@ def _create_temp_narrative(self, cells, parameters, importData, includeIntroCell
'Workspace/Narrative bundle.'}],
'hidden': 0}]})[0]
objectInfo = ServiceUtils.objectInfoToObject(objectInfo)
self._completeNewNarrative(newWorkspaceInfo['id'], objectInfo['id'], importData)
return {'workspaceInfo': newWorkspaceInfo, 'narrativeInfo': objectInfo}

def _fetchNarrativeObjects(self, workspaceName, cells, parameters, includeIntroCell):
is_temporary = 'true'
if title is not None and title != 'Untitled':
is_temporary = 'false'
ws_info = self._completeNewNarrative(ws_info[0], objectInfo['id'],
importData, is_temporary, title)
return {
'workspaceInfo': ServiceUtils.workspaceInfoToObject(ws_info),
'narrativeInfo': objectInfo
}

def _fetchNarrativeObjects(self, workspaceName, cells, parameters, includeIntroCell, title):
if not cells:
cells = []
if not title:
title = 'Untitled'

# fetchSpecs
appSpecIds = []
methodSpecIds = []
Expand All @@ -316,13 +330,14 @@ def _fetchNarrativeObjects(self, workspaceName, cells, parameters, includeIntroC
spec_id = spec['info']['id']
specMapping['methods'][spec_id] = spec
# end of fetchSpecs

metadata = {'job_ids': {'methods': [],
'apps': [],
'job_usage': {'queue_time': 0, 'run_time': 0}},
'format': 'ipynb',
'creator': self.user_id,
'ws_name': workspaceName,
'name': 'Untitled',
'name': title,
'type': 'KBaseNarrative.Narrative',
'description': '',
'data_dependencies': []}
Expand Down Expand Up @@ -414,18 +429,29 @@ def _buildMethodCell(self, pos, spec, params):
cell['metadata'][self.KB_CELL] = cellInfo;
return cell

def _completeNewNarrative(self, workspaceId, objectId, importData):
def _completeNewNarrative(self, workspaceId, objectId, importData, is_temporary, title):
"""
'Completes' the new narrative by updating workspace metadata with the required fields and
copying in data from the importData list of references.
"""
new_meta = {
'narrative': str(objectId),
'is_temporary': is_temporary
}
if is_temporary == 'false' and title is not None:
new_meta['narrative_nice_name'] = title

self.ws.alter_workspace_metadata({'wsi': {'id': workspaceId},
'new': {'narrative': str(objectId),
'is_temporary': 'true'}})
'new': new_meta})
# copy_to_narrative:
if not importData:
return
objectsToCopy = [{'ref': x} for x in importData]
infoList = self.ws.get_object_info_new({'objects': objectsToCopy, 'includeMetadata': 0})
for item in infoList:
objectInfo = ServiceUtils.objectInfoToObject(item)
self.copy_object(objectInfo['ref'], workspaceId, None, None, objectInfo)
if importData:
objectsToCopy = [{'ref': x} for x in importData]
infoList = self.ws.get_object_info_new({'objects': objectsToCopy, 'includeMetadata': 0})
for item in infoList:
objectInfo = ServiceUtils.objectInfoToObject(item)
self.copy_object(objectInfo['ref'], workspaceId, None, None, objectInfo)

return self.ws.get_workspace_info({'id': workspaceId})

def _safeJSONStringify(self, obj):
return json.dumps(self._safeJSONStringifyPrepare(obj))
Expand Down
34 changes: 20 additions & 14 deletions lib/NarrativeService/NarrativeServiceClient.pm
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ CreateNewNarrativeParams is a reference to a hash where the following keys are d
copydata has a value which is a string
importData has a value which is a reference to a list where each element is a string
includeIntroCell has a value which is a NarrativeService.boolean
title has a value which is a string
AppParam is a reference to a list containing 3 items:
0: (step_pos) an int
1: (key) a string
Expand Down Expand Up @@ -431,6 +432,7 @@ CreateNewNarrativeParams is a reference to a hash where the following keys are d
copydata has a value which is a string
importData has a value which is a reference to a list where each element is a string
includeIntroCell has a value which is a NarrativeService.boolean
title has a value which is a string
AppParam is a reference to a list containing 3 items:
0: (step_pos) an int
1: (key) a string
Expand Down Expand Up @@ -1520,19 +1522,19 @@ a reference to a list containing 11 items:
Information about a workspace.
ws_id id - the numerical ID of the workspace.
ws_name workspace - name of the workspace.
username owner - name of the user who owns (e.g. created) this workspace.
timestamp moddate - date when the workspace was last modified.
int max_objid - the maximum object ID appearing in this workspace.
Since cloning a workspace preserves object IDs, this number may be
greater than the number of objects in a newly cloned workspace.
permission user_permission - permissions for the authenticated user of
this workspace.
permission globalread - whether this workspace is globally readable.
lock_status lockstat - the status of the workspace lock.
usermeta metadata - arbitrary user-supplied metadata about
the workspace.
ws_id id - the numerical ID of the workspace.
ws_name workspace - name of the workspace.
username owner - name of the user who owns (e.g. created) this workspace.
timestamp moddate - date when the workspace was last modified.
int max_objid - the maximum object ID appearing in this workspace.
Since cloning a workspace preserves object IDs, this number may be
greater than the number of objects in a newly cloned workspace.
permission user_permission - permissions for the authenticated user of
this workspace.
permission globalread - whether this workspace is globally readable.
lock_status lockstat - the status of the workspace lock.
usermeta metadata - arbitrary user-supplied metadata about
the workspace.
=item Definition
Expand Down Expand Up @@ -1614,7 +1616,7 @@ set_items_info has a value which is a reference to a list where each element is
=item Description
ref - reference to any DataPalette container pointing to given object,
refs - list of references to all DataPalette containers pointing to
refs - list of references to all DataPalette containers pointing to
given object.
Expand Down Expand Up @@ -2044,6 +2046,8 @@ markdown - markdown text for cell of 'markdown' type (optional)
copydata - packed inport data in format "import(;...)*" (alternative to importData)
importData - import data in unpacked form (alternative to copydata)
includeIntroCell - if 1, adds an introductory markdown cell at the top (optional, default 0)
title - name of the new narrative (optional, if a string besides 'Untitled', this will
mark the narrative as not temporary, so it will appear in the dashboard)
=item Definition
Expand All @@ -2060,6 +2064,7 @@ markdown has a value which is a string
copydata has a value which is a string
importData has a value which is a reference to a list where each element is a string
includeIntroCell has a value which is a NarrativeService.boolean
title has a value which is a string
</pre>
Expand All @@ -2076,6 +2081,7 @@ markdown has a value which is a string
copydata has a value which is a string
importData has a value which is a reference to a list where each element is a string
includeIntroCell has a value which is a NarrativeService.boolean
title has a value which is a string
=end text
Expand Down
19 changes: 11 additions & 8 deletions lib/NarrativeService/NarrativeServiceClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ def create_new_narrative(self, params, context=None):
(optional) copydata - packed inport data in format "import(;...)*"
(alternative to importData) importData - import data in unpacked
form (alternative to copydata) includeIntroCell - if 1, adds an
introductory markdown cell at the top (optional, default 0)) ->
structure: parameter "app" of String, parameter "method" of
String, parameter "appparam" of String, parameter "appData" of
list of type "AppParam" -> tuple of size 3: parameter "step_pos"
of Long, parameter "key" of String, parameter "value" of String,
parameter "markdown" of String, parameter "copydata" of String,
parameter "importData" of list of String, parameter
"includeIntroCell" of type "boolean" (@range [0,1])
introductory markdown cell at the top (optional, default 0) title
- name of the new narrative (optional, if a string besides
'Untitled', this will mark the narrative as not temporary, so it
will appear in the dashboard)) -> structure: parameter "app" of
String, parameter "method" of String, parameter "appparam" of
String, parameter "appData" of list of type "AppParam" -> tuple of
size 3: parameter "step_pos" of Long, parameter "key" of String,
parameter "value" of String, parameter "markdown" of String,
parameter "copydata" of String, parameter "importData" of list of
String, parameter "includeIntroCell" of type "boolean" (@range
[0,1]), parameter "title" of String
:returns: instance of type "CreateNewNarrativeOutput" -> structure:
parameter "workspaceInfo" of type "WorkspaceInfo" (Restructured
workspace info 'wsInfo' tuple: id: wsInfo[0], name: wsInfo[1],
Expand Down
32 changes: 19 additions & 13 deletions lib/NarrativeService/NarrativeServiceImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class NarrativeService:
# state. A method could easily clobber the state set by another while
# the latter method is running.
######################################### noqa
VERSION = "0.0.3"
GIT_URL = "git@github.com:kbaseapps/NarrativeService"
GIT_COMMIT_HASH = "868fb02d7d38125c905aab8a13e6267f92b73535"
VERSION = "0.0.7"
GIT_URL = "https://github.com/briehl/NarrativeService"
GIT_COMMIT_HASH = "0489b972a84475ee9c89f42263abb5ca0de4c67c"

#BEGIN_CLASS_HEADER
def _nm(self, ctx):
Expand Down Expand Up @@ -178,14 +178,17 @@ def create_new_narrative(self, ctx, params):
(optional) copydata - packed inport data in format "import(;...)*"
(alternative to importData) importData - import data in unpacked
form (alternative to copydata) includeIntroCell - if 1, adds an
introductory markdown cell at the top (optional, default 0)) ->
structure: parameter "app" of String, parameter "method" of
String, parameter "appparam" of String, parameter "appData" of
list of type "AppParam" -> tuple of size 3: parameter "step_pos"
of Long, parameter "key" of String, parameter "value" of String,
parameter "markdown" of String, parameter "copydata" of String,
parameter "importData" of list of String, parameter
"includeIntroCell" of type "boolean" (@range [0,1])
introductory markdown cell at the top (optional, default 0) title
- name of the new narrative (optional, if a string besides
'Untitled', this will mark the narrative as not temporary, so it
will appear in the dashboard)) -> structure: parameter "app" of
String, parameter "method" of String, parameter "appparam" of
String, parameter "appData" of list of type "AppParam" -> tuple of
size 3: parameter "step_pos" of Long, parameter "key" of String,
parameter "value" of String, parameter "markdown" of String,
parameter "copydata" of String, parameter "importData" of list of
String, parameter "includeIntroCell" of type "boolean" (@range
[0,1]), parameter "title" of String
:returns: instance of type "CreateNewNarrativeOutput" -> structure:
parameter "workspaceInfo" of type "WorkspaceInfo" (Restructured
workspace info 'wsInfo' tuple: id: wsInfo[0], name: wsInfo[1],
Expand Down Expand Up @@ -241,8 +244,11 @@ def create_new_narrative(self, ctx, params):
copydata = params.get('copydata')
importData = params.get('importData')
includeIntroCell = params.get('includeIntroCell', 0)
returnVal = self._nm(ctx).create_new_narrative(app, method, appparam, appData, markdown,
copydata, importData, includeIntroCell)
title = params.get('title', None)
returnVal = self._nm(ctx).create_new_narrative(
app, method, appparam, appData, markdown, copydata, importData,
includeIntroCell, title
)
#END create_new_narrative

# At some point might do deeper type checking...
Expand Down
Loading

0 comments on commit 5580ea2

Please sign in to comment.