StringTree.value(): Add optional path argument

As opposed to the C++ variant, StringTree.value() doesn't take a path
argument, but only returns the value of the node it's called on.
Change this. Returns None if the path is not found.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2020-04-18 08:38:34 +02:00
commit 85672d6cf3

View file

@ -115,7 +115,7 @@ class StringTree: # export
r = self.get(path) r = self.get(path)
if r is None: if r is None:
raise KeyError(path) raise KeyError(path)
return r.value() return r.value() # type: ignore
def __setitem__(self, key, value): def __setitem__(self, key, value):
return self.__set(key, value) return self.__set(key, value)
@ -170,7 +170,12 @@ class StringTree: # export
relpath = '.'.join(components[1:]) relpath = '.'.join(components[1:])
return self.children[name].get(relpath) return self.children[name].get(relpath)
def value(self) -> str: def value(self, path = None) -> Optional[str]:
if path:
child = self.get(path)
if child is None:
return None
return child.value()
if len(self.children) == 0: if len(self.children) == 0:
raise Exception('tried to get value from leave "{}"'.format(self.content)) raise Exception('tried to get value from leave "{}"'.format(self.content))
return self.children[next(reversed(self.children))].content # type: ignore return self.children[next(reversed(self.children))].content # type: ignore