From 85672d6cf37509170e96114a3c073805d5c1c16d Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 18 Apr 2020 08:38:34 +0200 Subject: [PATCH] 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 --- tools/python/jwutils/stree/StringTree.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/python/jwutils/stree/StringTree.py b/tools/python/jwutils/stree/StringTree.py index c7625fb..66321af 100644 --- a/tools/python/jwutils/stree/StringTree.py +++ b/tools/python/jwutils/stree/StringTree.py @@ -115,7 +115,7 @@ class StringTree: # export r = self.get(path) if r is None: raise KeyError(path) - return r.value() + return r.value() # type: ignore def __setitem__(self, key, value): return self.__set(key, value) @@ -170,7 +170,12 @@ class StringTree: # export relpath = '.'.join(components[1:]) 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: raise Exception('tried to get value from leave "{}"'.format(self.content)) return self.children[next(reversed(self.children))].content # type: ignore