diff --git a/src/python/jw/util/stree/StringTree.py b/src/python/jw/util/stree/StringTree.py index 23a2cce..ff14c9c 100644 --- a/src/python/jw/util/stree/StringTree.py +++ b/src/python/jw/util/stree/StringTree.py @@ -43,6 +43,9 @@ def cleanup_string(s: str) -> str: class StringTree(Mapping): # export + class Error(Exception): + pass + def __init__( self, path: str, content: str, parent: StringTree | None = None ) -> None: @@ -90,7 +93,7 @@ class StringTree(Mapping): # export #assert self.content != str(content) if content is not None and type(content) not in [str, StringTree]: - raise Exception( + raise self.Error( "Tried to add content of unsupported type {}".format( type(content).__name__ ) @@ -101,7 +104,7 @@ class StringTree(Mapping): # export elif isinstance(content, StringTree): self.__adopt_children(content) else: - raise Exception( + raise self.Error( "Tried to add content of unsupported type {}".format( type(content).__name__ ) @@ -143,7 +146,8 @@ class StringTree(Mapping): # export if nibble not in self.children: self.children[nibble] = StringTree('', content = nibble, parent = self) if length > 1: - assert len(rest) > 0 + if len(rest) <= 0: + raise self.Error(f'{nibble} has missing child name') return self.children[nibble].__set(rest, content = content) # last component, a.k.a. leaf if content is not None: @@ -151,7 +155,7 @@ class StringTree(Mapping): # export '', content = content, parent = self.children[nibble] ) if gc.content is None: - raise Exception(f'Grandchild {nibble} has not content') + raise self.Error(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: @@ -168,7 +172,7 @@ class StringTree(Mapping): # export def __getitem__(self, path: str) -> StringTree: ret = self.get(path) if ret is None: - raise KeyError(path) + raise self.Error(path) return ret def __setitem__(self, key: str, value: str) -> StringTree: @@ -204,10 +208,10 @@ class StringTree(Mapping): # export def set_content(self, content): if content is None: - raise Exception("Tried to set none content") + raise self.Error("Tried to set none content") content = cleanup_string(content) if len(content) == 0: - raise Exception("Tried to set empty content") + raise self.Error("Tried to set empty content") self.content = content def add( @@ -253,7 +257,7 @@ class StringTree(Mapping): # export return None return child.value() if len(self.children) == 0: - raise Exception('tried to get value from leaf "{}"'.format(self.content)) + raise self.Error('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 @@ -314,7 +318,7 @@ class StringTree(Mapping): # export if isinstance(e, re.PatternError): pass else: - raise + raise self.Error(str(e)).with_traceback(e.__traceback__) from e def __select_matcher(m: StringTree.Match) -> Any: match m: