mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +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
|
||||
|
||||
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.content: Optional[str] = None
|
||||
self.__set(path, content)
|
||||
|
|
@ -51,21 +51,20 @@ class StringTree: # export
|
|||
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
||||
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
||||
|
||||
def __add_children(self, rhs):
|
||||
assert isinstance(rhs, StringTree)
|
||||
slog(DEBUG, "{}: adding children of {}: ".format(str(self), str(rhs)), str(rhs.children))
|
||||
#rhs.dump(INFO, "These children are added")
|
||||
self.content = rhs.content
|
||||
for name, c in rhs.children.items():
|
||||
def __adopt_children(self, parent):
|
||||
assert isinstance(parent, StringTree)
|
||||
slog(DEBUG, f'At {self.content}: Adopting children of {parent}')
|
||||
#parent.dump(INFO, "These children are added")
|
||||
self.content = parent.content
|
||||
for name, c in parent.children.items():
|
||||
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
|
||||
else:
|
||||
self.children[name].__add_children(c)
|
||||
self.children[name].__adopt_children(c)
|
||||
|
||||
def __set(self, path_, content, split=True):
|
||||
slog(DEBUG, "+ setting >" + str(content) + "< at path \"" + str(path_) + "\"" +
|
||||
(', containing "{}"'.format(str(self.content))) if hasattr(self, "content") else "")
|
||||
slog(DEBUG, ('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "") + f'Setting "{path_}" -> "{content}"')
|
||||
assert self.content != str(content)
|
||||
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__))
|
||||
|
|
@ -73,7 +72,7 @@ class StringTree: # export
|
|||
if isinstance(content, str):
|
||||
self.content = cleanup_string(content)
|
||||
elif isinstance(content, StringTree):
|
||||
self.__add_children(content)
|
||||
self.__adopt_children(content)
|
||||
else:
|
||||
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
||||
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)
|
||||
if isinstance(content, StringTree):
|
||||
#assert isinstance(content, StringTree), "Type: " + type(content).__name__
|
||||
self.__add_children(content)
|
||||
self.__adopt_children(content)
|
||||
else:
|
||||
#self.content = cleanup_string(content)
|
||||
self.content = content
|
||||
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
||||
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
||||
assert(content != '"[a-zA-Z0-9+_*/-]"')
|
||||
#self.children[content] = StringTree(None, content)
|
||||
if self.content != content:
|
||||
#self.content = cleanup_string(content)
|
||||
slog(DEBUG, f'Changing content: "{self.content}" ->"{content}"')
|
||||
assert(content != '"[a-zA-Z0-9+_*/-]"')
|
||||
self.content = content
|
||||
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
||||
#self.children[content] = StringTree(None, content)
|
||||
return self
|
||||
|
||||
#assert self.content is not None, "tried to set empty content to {}".format(path_)
|
||||
|
|
@ -143,7 +143,7 @@ class StringTree: # export
|
|||
self.content = content
|
||||
|
||||
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)
|
||||
|
||||
def get(self, path_: str) -> Optional[StringTree]:
|
||||
|
|
@ -181,6 +181,7 @@ class StringTree: # export
|
|||
return child.value()
|
||||
if len(self.children) == 0:
|
||||
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
|
||||
|
||||
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)
|
||||
sec = ''
|
||||
for line in s.splitlines():
|
||||
slog(DEBUG, "line=", line)
|
||||
slog(DEBUG, f'Parsing: "{line}"')
|
||||
line = _cleanup_line(line)
|
||||
#slog(DEBUG, "cleaned line=", line)
|
||||
if not len(line):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue