Skip to content

Commit

Permalink
respond to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
briehl committed Jun 13, 2024
1 parent 6cc1286 commit a272dc3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
41 changes: 22 additions & 19 deletions lib/NarrativeService/narrativemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NarrativeManager:
KB_STATE = "widget_state"

def __init__(
self,
self: "NarrativeManager",
config: dict[str, Any],
user_id: str,
workspace_client: Workspace,
Expand All @@ -38,7 +38,7 @@ def __init__(
self.search_client = search_service_client
self.intro_cell_file = config["intro-cell-file"]

def get_narrative_doc(self, narrative_upa: str) -> dict[str, Any]:
def get_narrative_doc(self: "NarrativeManager", narrative_upa: str) -> dict[str, Any]:
try:
# ensure correct upa format and get numerical ws_id
ws_id, _, _, = (int(i) for i in narrative_upa.split("/"))
Expand Down Expand Up @@ -77,7 +77,10 @@ def get_narrative_doc(self, narrative_upa: str) -> dict[str, Any]:
"version": obj_data["info"][4]
}

def _fmt_doc_permissions(self, permissions: dict[str, str]) -> tuple[list[str], bool]:
def _fmt_doc_permissions(
self: "NarrativeManager",
permissions: dict[str, str]
) -> tuple[list[str], bool]:
# get list of users and whether a narrative is public
is_public = False
shared_users = []
Expand All @@ -89,7 +92,7 @@ def _fmt_doc_permissions(self, permissions: dict[str, str]) -> tuple[list[str],
shared_users.append(k)
return shared_users, is_public

def _get_doc_cell(self, cell: dict[str, Any]) -> dict[str, Any]:
def _get_doc_cell(self: "NarrativeManager", cell: dict[str, Any]) -> dict[str, Any]:
# get the appropriate cell format for search result doc
meta = cell.get("metadata", {}).get("kbase", {})
if cell["cell_type"] == "markdown":
Expand Down Expand Up @@ -136,7 +139,7 @@ def _get_doc_cell(self, cell: dict[str, Any]) -> dict[str, Any]:
"desc": ""
}

def revert_narrative_object(self, obj: dict[str, Any]) -> list[Any]:
def revert_narrative_object(self: "NarrativeManager", obj: dict[str, Any]) -> list[Any]:
# check that there is a proper workspace id and object id
if ("wsid" not in obj or "objid" not in obj):
raise ValueError(
Expand Down Expand Up @@ -179,7 +182,7 @@ def revert_narrative_object(self, obj: dict[str, Any]) -> list[Any]:
return revert_result

def _check_new_version_indexed(
self,
self: "NarrativeManager",
obj: dict[str, int | str],
new_version: int
) -> dict[str, Any]:
Expand All @@ -205,7 +208,7 @@ def _check_new_version_indexed(
return data

def copy_narrative(
self,
self: "NarrativeManager",
new_name: str,
workspace_ref: str,
workspace_id: int
Expand Down Expand Up @@ -285,7 +288,7 @@ def copy_narrative(
raise

def create_new_narrative(
self,
self: "NarrativeManager",
app: str,
method: str,
app_param: str,
Expand Down Expand Up @@ -326,15 +329,15 @@ def create_new_narrative(
pass
return narr_info

def _get_intro_cell(self) -> dict[str, Any]:
def _get_intro_cell(self: "NarrativeManager") -> dict[str, Any]:
"""
Loads intro cell JSON from file
"""
with open(self.intro_cell_file) as intro_cell:
return json.load(intro_cell)

def _create_temp_narrative(
self,
self: "NarrativeManager",
cells: list[dict[str, Any]],
parameters: list[list[Any]],
import_data: list[str],
Expand Down Expand Up @@ -379,7 +382,7 @@ def _create_temp_narrative(
}

def _fetch_narrative_objects(
self,
self: "NarrativeManager",
ws_name: str,
cells: list[dict[str, Any]],
parameters: list[list[Any]],
Expand Down Expand Up @@ -444,7 +447,7 @@ def _fetch_narrative_objects(
return [narrative_object, metadata_external]

def _gather_cell_data(
self,
self: "NarrativeManager",
cells: list[dict[str, Any]],
spec_mapping: dict[str, Any],
parameters: list[list[Any]],
Expand Down Expand Up @@ -474,7 +477,7 @@ def _gather_cell_data(
return cell_data

def _build_app_cell(
self,
self: "NarrativeManager",
pos: int,
spec: dict[str, Any],
params: list[list[Any]]
Expand Down Expand Up @@ -508,7 +511,7 @@ def _build_app_cell(
return cell

def _build_method_cell(
self,
self: "NarrativeManager",
pos: int,
spec: dict[str, Any],
params: list[list[Any]]
Expand All @@ -535,7 +538,7 @@ def _build_method_cell(
return cell

def _complete_new_narrative(
self,
self: "NarrativeManager",
ws_id: str | int,
obj_id: str | int,
import_data: list[str],
Expand Down Expand Up @@ -572,13 +575,13 @@ def _complete_new_narrative(
return self.ws.get_workspace_info({"id": ws_id})

def _safe_json_stringify(
self,
self: "NarrativeManager",
obj: str | list[str] | dict[str, str]
) -> str | list[str] | dict[str, str]:
return json.dumps(self._safe_json_stringify_prepare(obj))

def _safe_json_stringify_prepare(
self,
self: "NarrativeManager",
obj: str | list[str] | dict[str, str]
) -> str | list[str] | dict[str, str]:
if isinstance(obj, str):
Expand All @@ -595,7 +598,7 @@ def _safe_json_stringify_prepare(
return obj

def copy_object(
self,
self: "NarrativeManager",
ref: str,
target_ws_id: str | int,
target_ws_name: str,
Expand Down Expand Up @@ -625,7 +628,7 @@ def copy_object(
return {"info": obj_info}

def rename_narrative(
self,
self: "NarrativeManager",
narrative_ref: str,
new_name: str,
service_version: str
Expand Down
7 changes: 4 additions & 3 deletions test/unit/test_narrativemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,19 @@ def test_get_narrative_doc_not_found(config, mock_workspace_client, mock_user):

def test_revert_narrative_object(config, mock_workspace_client, mock_user):
# set up narrative
# simulate reverting fake narrative to version #2
# (make_object_history=True automatically makes 5 versions)
narrative_ref = mock_workspace_client.make_fake_narrative(
"SomeNiceName",
mock_user,
make_object_history=True
)

(ws_id, obj, _) = narrative_ref.split("/")
(ws_id, obj, ver) = narrative_ref.split("/")
assert ver == "5"

nm = NarrativeManager(config, mock_user, mock_workspace_client, mock.MagicMock())

# simulate reverting fake narrative to version #2
# (make_object_history=True automatically makes 5 versions)
revert_result = nm.revert_narrative_object({
"wsid": int(ws_id),
"objid": int(obj),
Expand Down

0 comments on commit a272dc3

Please sign in to comment.