Skip to content

Commit

Permalink
chore(models-listnode): delete dead code (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Feb 1, 2024
1 parent b5963de commit fea23fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 139 deletions.
128 changes: 11 additions & 117 deletions src/dsp_tools/commands/project/models/listnode.py
Expand Up @@ -9,26 +9,16 @@
* Instantiate a new object with ``iri`` (IRI of listnode)
* Call the ``read`` method on the instance
* Access information about the instance
UPDATE:
* Only partially implemented. Only "label" and "comment" attributes may be changed.
* You need an instance of an existing ListNode by reading an instance
* Change the attributes by assigning the new values
* Call the ``update`` method on the instance
DELETE
* NOT YET IMPLEMENTED!
"""
from __future__ import annotations

from pprint import pprint
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.langstring import LangString, Languages
from dsp_tools.models.langstring import LangString
from dsp_tools.utils.connection import Connection


Expand All @@ -50,12 +40,12 @@ class ListNode(Model):
label : LangString
A LangString instance with language dependent labels. Setting this attribute overwrites all entries
with the new ones. In order to add/remove a specific entry, use "addLabel" or "rmLabel".
with the new ones.
At least one label is required [read/write].
comments : LangString
A LangString instance with language dependent comments. Setting this attributes overwrites all entries
with the new ones. In order to add/remove a specific entry, use "addComment" or "rmComment".
with the new ones.
name : str
A unique name for the ListNode (unique inside this list) [read/write].
Expand Down Expand Up @@ -85,12 +75,6 @@ class ListNode(Model):
read : ListNode information object
Returns information about a single list node
update : ListNode information object
Updates the changed attributes and returns the updated information from the ListNode
delete :
NOT YET IMPLEMENTED
getAllNodes : ListNode
Get all nodes of a list. The IRI of the root node has to be supplied.
Expand All @@ -112,7 +96,7 @@ class ListNode(Model):
_name: Optional[str]
_parent: Optional[str]
_isRootNode: bool
_children: Optional[list["ListNode"]]
_children: Optional[list[ListNode]]
_rootNodeIri: Optional[str]

def __init__(
Expand All @@ -123,9 +107,9 @@ def __init__(
label: Optional[LangString] = None,
comments: Optional[LangString] = None,
name: Optional[str] = None,
parent: Optional[Union["ListNode", str]] = None,
parent: Optional[Union[ListNode, str]] = None,
isRootNode: bool = False,
children: Optional[list["ListNode"]] = None,
children: Optional[list[ListNode]] = None,
rootNodeIri: Optional[str] = None,
) -> None:
"""
Expand All @@ -135,13 +119,6 @@ def __init__(
* The "con" and at least one "label" are required
READ:
* The "con" and "iri" attributes are required
UPDATE:
* Only "label", "comments" and "name" may be changed
DELETE:
* Not yet implemented
Setting a label overwites all entries. To add/remove a specific entry, use "addLabel" or "rmLabel".
Setting a comment overwites all entries. To add/remove a specific entry, use "addComment" or "rmComment".
Args:
con: A valid Connection instance with a user logged in that has the appropriate permissions
Expand Down Expand Up @@ -203,28 +180,6 @@ def label(self, value: Optional[Union[LangString, str]]) -> None:
self._label = LangString(value)
self._changed.add("label")

def addLabel(self, lang: Union[Languages, str], value: str) -> None:
"""
Add/replace a node label with the given language (executed at next update)
:param lang: The language the label, either a string "EN", "DE", "FR", "IT" or a Language instance
:param value: The text of the label
:return: None
"""

self._label[lang] = value
self._changed.add("label")

def rmLabel(self, lang: Union[Languages, str]) -> None:
"""
Remove a label from a list node (executed at next update)
Args:
lang: language of the label (string "EN", "DE", "FR", "IT", "RM", or a Language instance)
"""
del self._label[lang]
self._changed.add("label")

@property
def comments(self) -> LangString:
return self._comments or LangString({})
Expand All @@ -234,28 +189,6 @@ def comments(self, value: Optional[Union[LangString, str]]) -> None:
self._comments = LangString(value)
self._changed.add("comments")

def addComment(self, lang: Union[Languages, str], value: str) -> None:
"""
Add/replace a node comments with the given language (executed at next update)
:param lang: The language the comments, either a string "EN", "DE", "FR", "IT" or a Language instance
:param value: The text of the comments
:return: None
"""

self._comments[lang] = value
self._changed.add("comments")

def rmComment(self, lang: Union[Languages, str]) -> None:
"""
Remove a comments from a list node (executed at next update)
:param lang: language of the comment (string "EN", "DE", "FR", "IT", "RM", or a Language instance)
:return: None
"""
del self._comments[lang]
self._changed.add("comments")

@property
def name(self) -> Optional[str]:
return self._name
Expand All @@ -270,7 +203,7 @@ def parent(self) -> Optional[str]:
return self._parent if self._parent else None

@parent.setter
def parent(self, value: Union[str, "ListNode"]) -> None:
def parent(self, value: Union[str, ListNode]) -> None:
if isinstance(value, ListNode):
self._parent = value.iri
else:
Expand All @@ -281,11 +214,11 @@ def isRootNode(self) -> Optional[bool]:
return self._isRootNode

@property
def children(self) -> list["ListNode"]:
def children(self) -> list[ListNode]:
return self._children or []

@children.setter
def children(self, value: list["ListNode"]) -> None:
def children(self, value: list[ListNode]) -> None:
self._children = value

@staticmethod
Expand All @@ -294,7 +227,7 @@ def __getChildren(
parent_iri: str,
project_iri: str,
children: list[Any],
) -> Optional[list["ListNode"]]:
) -> Optional[list[ListNode]]:
"""
Internal method! Should not be used directly!
Expand Down Expand Up @@ -410,46 +343,7 @@ def read(self) -> Any:
else:
return None

def update(self) -> Union[Any, None]:
"""
Update the ListNode info in DSP with the modified data in this ListNode instance
:return: JSON-object from DSP-API reflecting the update
"""

jsonobj = self._toJsonObj_update()
if jsonobj:
result = self._con.put(ListNode.ROUTE_SLASH + quote_plus(self.iri), jsonobj)
pprint(result)
return ListNode.fromJsonObj(self._con, result["listinfo"])
else:
return None

def _toJsonObj_update(self) -> dict[str, Any]:
tmp = {}
if self.iri is None:
raise BaseError("There must be a node iri given!")
tmp["listIri"] = self.iri
if self._project is None:
raise BaseError("There must be a project id given!")
tmp["projectIri"] = self._project
if not self._label.isEmpty() and "label" in self._changed:
tmp["labels"] = self._label.toJsonObj()
if not self._comments.isEmpty() and "comments" in self._changed:
tmp["comments"] = self._comments.toJsonObj()
if self._name and "name" in self._changed:
tmp["name"] = self._name
return tmp

def delete(self) -> None:
"""
Delete the given ListNode
:return: DSP-API response
"""
raise BaseError("NOT YET IMPLEMENTED")

def getAllNodes(self) -> "ListNode":
def getAllNodes(self) -> ListNode:
"""
Get all nodes of the list. Must be called from a ListNode instance that has at least set the
list iri!
Expand Down
22 changes: 0 additions & 22 deletions test/e2e/commands/project/test_listnode.py
Expand Up @@ -85,28 +85,6 @@ def test_ListNode_hierarchy(self) -> None:
self.assertTrue(node.isRootNode)
self.assertFalse(subnode.isRootNode)

def test_ListNode_update(self) -> None:
"""
Update the data of a node
:return: None
"""
node = ListNode(
con=self.con,
project=self.project,
label=LangString({Languages.EN: "root node 3"}),
comments=LangString({Languages.EN: "Third root node"}),
name="test_node",
).create()
node.addLabel("de", "Neues Label")
node.rmLabel("en")
node.addComment("fr", "un commentaire en français")
node.rmComment("en")
node.name = "test_node_update"
node.update()
self.assertEqual(node.label["de"], "Neues Label")
self.assertEqual(node.comments["fr"], "un commentaire en français")
self.assertEqual(node.name, "test_node_update")

def test_ListNode_getAllLists(self) -> None:
"""
Get all lists
Expand Down

0 comments on commit fea23fd

Please sign in to comment.