stree.StringTree: Add type hinting
Add some type hinting to the StringTree class in the hope to make some call semanticas clearer. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
b4041018d2
commit
aec0b0a3f5
1 changed files with 19 additions and 15 deletions
|
|
@ -8,11 +8,11 @@ from enum import Enum, auto
|
|||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import List, Optional, Union, Any
|
||||
from typing import Any, Iterator
|
||||
|
||||
from ..log import DEBUG, get_caller_pos, slog
|
||||
from ..log import DEBUG, INFO, get_caller_pos, slog
|
||||
|
||||
def quote(s):
|
||||
def quote(s: str) -> str:
|
||||
if is_quoted(s):
|
||||
return s
|
||||
s = s.strip()
|
||||
|
|
@ -48,7 +48,7 @@ class StringTree: # export
|
|||
slog(DEBUG, f'Constructing StringTree(path="{path}", content="{content}")')
|
||||
self.__parent = parent
|
||||
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
||||
self.content: Optional[str] = None
|
||||
self.content: str | None = None
|
||||
self.__set(path, content)
|
||||
|
||||
assert (hasattr(self, "content"))
|
||||
|
|
@ -63,7 +63,7 @@ class StringTree: # export
|
|||
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
||||
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
||||
|
||||
def __adopt_children(self, parent):
|
||||
def __adopt_children(self, parent: StringTree) -> None:
|
||||
assert isinstance(parent, StringTree)
|
||||
slog(DEBUG, f'At {self.content}: Adopting children of {parent}')
|
||||
#parent.dump(INFO, "These children are added")
|
||||
|
|
@ -75,7 +75,9 @@ class StringTree: # export
|
|||
else:
|
||||
self.children[name].__adopt_children(c)
|
||||
|
||||
def __set(self, path: str, content: str, split = True):
|
||||
def __set(
|
||||
self, path: str, content: str | StringTree | None, split = True
|
||||
) -> StringTree:
|
||||
slog(
|
||||
DEBUG,
|
||||
('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "")
|
||||
|
|
@ -147,11 +149,12 @@ class StringTree: # export
|
|||
gc = content if isinstance(content, StringTree) else StringTree(
|
||||
'', content = content, parent = self.children[nibble]
|
||||
)
|
||||
if gc.content is None:
|
||||
raise Exception(f'Grandchild {nibble} has not content')
|
||||
# Make sure no existing grand child is updated. It would reside too
|
||||
# far up in the grand child OrderedDict, we need it last
|
||||
if gc.content in self.children[nibble].children:
|
||||
del self.children[nibble].children[gc.content]
|
||||
assert gc.content is not None, 'Grand-child content is None'
|
||||
self.children[nibble].children[gc.content] = gc
|
||||
return self.children[nibble]
|
||||
|
||||
|
|
@ -164,7 +167,7 @@ class StringTree: # export
|
|||
raise KeyError(path)
|
||||
return r.value() # type: ignore
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
def __setitem__(self, key: str, value: str) -> StringTree:
|
||||
return self.__set(key, value)
|
||||
|
||||
def __dump(self, prio, indent = 0, **kwargs):
|
||||
|
|
@ -200,7 +203,7 @@ class StringTree: # export
|
|||
def add(
|
||||
self,
|
||||
path: str,
|
||||
content: Optional[Union[str, StringTree]] = None,
|
||||
content: str | StringTree | None = None,
|
||||
split: bool = True
|
||||
) -> StringTree:
|
||||
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
|
||||
|
|
@ -231,7 +234,7 @@ class StringTree: # export
|
|||
rel_path = '.'.join(components[1:])
|
||||
return self.children[name].get(rel_path)
|
||||
|
||||
def value(self, path = None, default = None) -> Optional[str]:
|
||||
def value(self, path: str | None = None, default: str | None = None) -> str | None:
|
||||
if path:
|
||||
child = self.get(path)
|
||||
if child is None:
|
||||
|
|
@ -242,19 +245,19 @@ class StringTree: # export
|
|||
if len(self.children) == 0:
|
||||
raise Exception('tried to get value from leaf "{}"'.format(self.content))
|
||||
slog(DEBUG, f'Returning value from children {self.children}')
|
||||
return self.children[next(reversed(self.children))].content # type: ignore
|
||||
return self.children[next(reversed(self.children))].content
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
def parent(self) -> StringTree | None:
|
||||
return self.__parent
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
def root(self) -> StringTree:
|
||||
if self.__parent is None:
|
||||
return self
|
||||
return self.__parent.root
|
||||
|
||||
def child_list(self, depth_first: bool = True) -> List[StringTree]:
|
||||
def child_list(self, depth_first: bool = True) -> list[StringTree]:
|
||||
if not depth_first:
|
||||
raise Exception(
|
||||
'Tried to retrieve child list with breadth-first '
|
||||
|
|
@ -282,7 +285,8 @@ class StringTree: # export
|
|||
GlobArg = auto()
|
||||
GlobConf = auto()
|
||||
|
||||
def __find(self, key: str | None, val: str | None, m: Match, depth_first: bool):
|
||||
def __find(self, key: str | None, val: str | None, m: Match,
|
||||
depth_first: bool) -> list[StringTree]:
|
||||
|
||||
def __children():
|
||||
for name, child in self.children.items():
|
||||
|
|
|
|||
Loading…
Reference in a new issue