77
88"""
99
10- import pathlib
1110import typing
1211import datetime
1312import 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
0 commit comments