mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
StringTree: Beautify debug logging
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
1ff24b870a
commit
126ecbb411
2 changed files with 22 additions and 21 deletions
|
|
@ -35,7 +35,7 @@ def cleanup_string(s: str) -> str:
|
||||||
class StringTree: # export
|
class StringTree: # export
|
||||||
|
|
||||||
def __init__(self, path: str, content: str) -> None:
|
def __init__(self, path: str, content: str) -> None:
|
||||||
slog(DEBUG, "ctor, path =", path, "content =", content)
|
slog(DEBUG, f'Constructing StringTree(path="{path}", content="{content}")')
|
||||||
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
||||||
self.content: Optional[str] = None
|
self.content: Optional[str] = None
|
||||||
self.__set(path, content)
|
self.__set(path, content)
|
||||||
|
|
@ -51,21 +51,20 @@ class StringTree: # export
|
||||||
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
||||||
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
||||||
|
|
||||||
def __add_children(self, rhs):
|
def __adopt_children(self, parent):
|
||||||
assert isinstance(rhs, StringTree)
|
assert isinstance(parent, StringTree)
|
||||||
slog(DEBUG, "{}: adding children of {}: ".format(str(self), str(rhs)), str(rhs.children))
|
slog(DEBUG, f'At {self.content}: Adopting children of {parent}')
|
||||||
#rhs.dump(INFO, "These children are added")
|
#parent.dump(INFO, "These children are added")
|
||||||
self.content = rhs.content
|
self.content = parent.content
|
||||||
for name, c in rhs.children.items():
|
for name, c in parent.children.items():
|
||||||
if not name in self.children.keys():
|
if not name in self.children.keys():
|
||||||
slog(DEBUG, "{}: adding new child: {}".format(str(self), str(c)))
|
slog(DEBUG, f'At {self.content}: Adding new child {c}')
|
||||||
self.children[name] = c
|
self.children[name] = c
|
||||||
else:
|
else:
|
||||||
self.children[name].__add_children(c)
|
self.children[name].__adopt_children(c)
|
||||||
|
|
||||||
def __set(self, path_, content, split=True):
|
def __set(self, path_, content, split=True):
|
||||||
slog(DEBUG, "+ setting >" + str(content) + "< at path \"" + str(path_) + "\"" +
|
slog(DEBUG, ('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "") + f'Setting "{path_}" -> "{content}"')
|
||||||
(', containing "{}"'.format(str(self.content))) if hasattr(self, "content") else "")
|
|
||||||
assert self.content != str(content)
|
assert self.content != str(content)
|
||||||
if content is not None and not type(content) in [str, StringTree]:
|
if content is not None and not type(content) in [str, StringTree]:
|
||||||
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
||||||
|
|
@ -73,7 +72,7 @@ class StringTree: # export
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
self.content = cleanup_string(content)
|
self.content = cleanup_string(content)
|
||||||
elif isinstance(content, StringTree):
|
elif isinstance(content, StringTree):
|
||||||
self.__add_children(content)
|
self.__adopt_children(content)
|
||||||
else:
|
else:
|
||||||
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
||||||
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
||||||
|
|
@ -85,14 +84,15 @@ class StringTree: # export
|
||||||
#assert self.content is None or (isinstance(content, StringTree) and content.content == self.content)
|
#assert self.content is None or (isinstance(content, StringTree) and content.content == self.content)
|
||||||
if isinstance(content, StringTree):
|
if isinstance(content, StringTree):
|
||||||
#assert isinstance(content, StringTree), "Type: " + type(content).__name__
|
#assert isinstance(content, StringTree), "Type: " + type(content).__name__
|
||||||
self.__add_children(content)
|
self.__adopt_children(content)
|
||||||
else:
|
else:
|
||||||
#self.content = cleanup_string(content)
|
if self.content != content:
|
||||||
self.content = content
|
#self.content = cleanup_string(content)
|
||||||
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
slog(DEBUG, f'Changing content: "{self.content}" ->"{content}"')
|
||||||
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
assert(content != '"[a-zA-Z0-9+_*/-]"')
|
||||||
assert(content != '"[a-zA-Z0-9+_*/-]"')
|
self.content = content
|
||||||
#self.children[content] = StringTree(None, content)
|
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
||||||
|
#self.children[content] = StringTree(None, content)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
#assert self.content is not None, "tried to set empty content to {}".format(path_)
|
#assert self.content is not None, "tried to set empty content to {}".format(path_)
|
||||||
|
|
@ -143,7 +143,7 @@ class StringTree: # export
|
||||||
self.content = content
|
self.content = content
|
||||||
|
|
||||||
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
|
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
|
||||||
slog(DEBUG, "adding >{}< at >{}< to >{}<".format(content, path, self.content))
|
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
|
||||||
return self.__set(path, content, split)
|
return self.__set(path, content, split)
|
||||||
|
|
||||||
def get(self, path_: str) -> Optional[StringTree]:
|
def get(self, path_: str) -> Optional[StringTree]:
|
||||||
|
|
@ -181,6 +181,7 @@ class StringTree: # export
|
||||||
return child.value()
|
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))
|
||||||
|
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 # type: ignore
|
||||||
|
|
||||||
def child_list(self, depth_first: bool=True) -> List[StringTree]:
|
def child_list(self, depth_first: bool=True) -> List[StringTree]:
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> Stri
|
||||||
root = StringTree('', content=root_content)
|
root = StringTree('', content=root_content)
|
||||||
sec = ''
|
sec = ''
|
||||||
for line in s.splitlines():
|
for line in s.splitlines():
|
||||||
slog(DEBUG, "line=", line)
|
slog(DEBUG, f'Parsing: "{line}"')
|
||||||
line = _cleanup_line(line)
|
line = _cleanup_line(line)
|
||||||
#slog(DEBUG, "cleaned line=", line)
|
#slog(DEBUG, "cleaned line=", line)
|
||||||
if not len(line):
|
if not len(line):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue