Skip to content

Commit c2401b7

Browse files
committed
✨ Added 'tree' to Folder class
1 parent 3daa867 commit c2401b7

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

simvue/api/objects/folder.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
"""
99

10-
import pathlib
1110
import typing
1211
import datetime
1312
import json
@@ -72,6 +71,7 @@ def __init__(self, identifier: str | None = None, **kwargs) -> None:
7271
any additional arguments to be passed to the object initialiser
7372
"""
7473
super().__init__(identifier, **kwargs)
74+
self._properties.remove("tree")
7575

7676
@classmethod
7777
@pydantic.validate_call
@@ -101,6 +101,32 @@ def get(
101101

102102
return super().get(count=count, offset=offset, **_params)
103103

104+
@property
105+
def tree(self) -> dict[str, object]:
106+
"""Return hierarchy for this folder.
107+
108+
Returns
109+
-------
110+
dict
111+
a nested dictionary describing the hierarchy
112+
"""
113+
_level: int = len(self.path.split("/"))
114+
_folders = self.__class__.get(
115+
filters=json.dumps([f"path contains {self.path}"])
116+
)
117+
_paths = [folder.path.split("/") for _, folder in _folders]
118+
_paths = sorted(_paths, key=len)
119+
_out_dict: dict[str, object] = {}
120+
_modifier = None
121+
for path in _paths:
122+
if len(path) <= _level:
123+
continue
124+
_modifier = _out_dict
125+
for element in path[1:]:
126+
_modifier.setdefault(element, {})
127+
_modifier = _modifier[element]
128+
return _out_dict
129+
104130
@property
105131
@staging_check
106132
def tags(self) -> list[str]:
@@ -115,7 +141,7 @@ def tags(self, tags: list[str]) -> None:
115141
self._staging["tags"] = tags
116142

117143
@property
118-
def path(self) -> pathlib.Path:
144+
def path(self) -> str:
119145
"""Return the path of this folder"""
120146
return self._get_attribute("path")
121147

tests/unit/test_folder.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,19 @@ def test_folder_get_properties() -> None:
147147

148148
if _failed:
149149
raise AssertionError("\n" + "\n\t- ".join(": ".join(i) for i in _failed))
150+
151+
152+
@pytest.mark.api
153+
@pytest.mark.online
154+
def test_folder_tree() -> None:
155+
N_FOLDERS: int = 10
156+
_uuid: str = f"{uuid.uuid4()}".split("-")[0]
157+
_root_folder_path: str = f"/simvue_unit_testing/objects/folder/{_uuid}"
158+
for i in range(N_FOLDERS):
159+
_path = f"{_root_folder_path}/test_folder_tree_{i}"
160+
_folder = Folder.new(path=_path)
161+
_folder.commit()
162+
_, _root_folder = next(Folder.get(filters=json.dumps([f"path == {_root_folder_path}"])))
163+
assert len(_root_folder.tree["simvue_unit_testing"]["objects"]["folder"][_uuid]) == N_FOLDERS
164+
_root_folder.delete(recursive=True)
165+

0 commit comments

Comments
 (0)