2020-04-10 17:55:36 +02:00
|
|
|
from __future__ import annotations
|
2025-05-29 12:05:10 +02:00
|
|
|
|
2024-12-15 15:35:29 +01:00
|
|
|
from typing import Any, List, Optional, Union
|
|
|
|
|
|
2025-05-29 12:05:10 +02:00
|
|
|
import re, fnmatch
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
from enum import Enum, auto
|
|
|
|
|
|
2017-11-01 13:26:10 +01:00
|
|
|
from jwutils.log import *
|
|
|
|
|
|
|
|
|
|
def quote(s):
|
|
|
|
|
if is_quoted(s):
|
|
|
|
|
return s
|
|
|
|
|
s = s.strip()
|
|
|
|
|
if len(s) > 0:
|
|
|
|
|
if s[0] == '"':
|
|
|
|
|
return "'" + s + "'"
|
|
|
|
|
return '"' + s + '"'
|
|
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def is_quoted(s: str) -> bool:
|
2017-11-01 13:26:10 +01:00
|
|
|
if isinstance(s, StringTree):
|
|
|
|
|
return False
|
|
|
|
|
s = s.strip()
|
|
|
|
|
if len(s) < 2:
|
|
|
|
|
return False
|
|
|
|
|
d = s[0]
|
|
|
|
|
if d == s[-1] and d in [ '"', "'" ]:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def cleanup_string(s: str) -> str:
|
2017-11-01 13:26:10 +01:00
|
|
|
if isinstance(s, StringTree):
|
|
|
|
|
return s
|
|
|
|
|
s = s.strip()
|
|
|
|
|
if is_quoted(s):
|
2017-11-05 16:35:41 +01:00
|
|
|
return s[1:-1].replace('\\' + s[0], s[0])
|
2017-11-01 13:26:10 +01:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
class StringTree: # export
|
|
|
|
|
|
2025-05-20 13:09:58 +02:00
|
|
|
def __init__(self, path: str, content: str, parent: StringTree|None=None) -> None:
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, f'Constructing StringTree(path="{path}", content="{content}")')
|
2025-05-20 13:09:58 +02:00
|
|
|
self.__parent = parent
|
2020-04-10 17:55:36 +02:00
|
|
|
self.children: OrderedDict[str, StringTree] = OrderedDict()
|
2024-12-15 15:35:29 +01:00
|
|
|
self.content: Optional[str] = None
|
2017-11-01 13:26:10 +01:00
|
|
|
self.__set(path, content)
|
2025-05-20 13:09:58 +02:00
|
|
|
|
2017-11-01 13:26:10 +01:00
|
|
|
assert(hasattr(self, "content"))
|
|
|
|
|
#assert self.content is not None
|
|
|
|
|
|
|
|
|
|
# root (content = [ symbols ])
|
|
|
|
|
# symbols (content = [ regex ])
|
|
|
|
|
# regex ( content ='[ \n\t\r]+' )
|
|
|
|
|
|
|
|
|
|
# root (content = root, children = [ symbols ])
|
|
|
|
|
# symbols (content = symbols, children = [ regex ])
|
|
|
|
|
# regex ( content = regex, children = [ '[ \n\t\r]+' ] )
|
|
|
|
|
# '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] )
|
|
|
|
|
|
2025-01-28 05:08:50 +01:00
|
|
|
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():
|
2020-04-04 11:35:26 +02:00
|
|
|
if not name in self.children.keys():
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, f'At {self.content}: Adding new child {c}')
|
2020-04-04 11:35:26 +02:00
|
|
|
self.children[name] = c
|
|
|
|
|
else:
|
2025-01-28 05:08:50 +01:00
|
|
|
self.children[name].__adopt_children(c)
|
2020-04-04 11:35:26 +02:00
|
|
|
|
2019-05-19 13:15:27 +00:00
|
|
|
def __set(self, path_, content, split=True):
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, ('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "") + f'Setting "{path_}" -> "{content}"')
|
2025-05-12 18:57:16 +02:00
|
|
|
#assert self.content != str(content) # Not sure what the idea behind this was. It often goes off, and all works fine without.
|
2020-04-04 11:35:26 +02:00
|
|
|
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__))
|
2017-11-01 13:26:10 +01:00
|
|
|
if path_ is None:
|
2020-04-04 11:35:26 +02:00
|
|
|
if isinstance(content, str):
|
|
|
|
|
self.content = cleanup_string(content)
|
|
|
|
|
elif isinstance(content, StringTree):
|
2025-01-28 05:08:50 +01:00
|
|
|
self.__adopt_children(content)
|
2020-04-04 11:35:26 +02:00
|
|
|
else:
|
|
|
|
|
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
|
2017-11-05 16:35:41 +01:00
|
|
|
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
2017-11-01 13:26:10 +01:00
|
|
|
return self
|
|
|
|
|
path = cleanup_string(path_)
|
2019-05-19 13:15:27 +00:00
|
|
|
components = path.split('.') if split else [ path ]
|
2017-11-01 13:26:10 +01:00
|
|
|
l = len(components)
|
|
|
|
|
if len(path) == 0 or l == 0:
|
2020-04-04 11:35:26 +02:00
|
|
|
#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__
|
2025-01-28 05:08:50 +01:00
|
|
|
self.__adopt_children(content)
|
2020-04-04 11:35:26 +02:00
|
|
|
else:
|
2025-01-28 05:08:50 +01:00
|
|
|
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)
|
2017-11-01 13:26:10 +01:00
|
|
|
return self
|
|
|
|
|
|
2020-04-04 11:35:26 +02:00
|
|
|
#assert self.content is not None, "tried to set empty content to {}".format(path_)
|
2017-11-01 13:26:10 +01:00
|
|
|
|
|
|
|
|
nibble = components[0]
|
|
|
|
|
rest = '.'.join(components[1:])
|
|
|
|
|
if nibble not in self.children:
|
2025-05-20 13:09:58 +02:00
|
|
|
self.children[nibble] = StringTree('', content=nibble, parent=self)
|
2017-11-01 13:26:10 +01:00
|
|
|
if l > 1:
|
|
|
|
|
assert len(rest) > 0
|
|
|
|
|
return self.children[nibble].__set(rest, content=content)
|
2025-01-28 07:18:50 +01:00
|
|
|
# last component, a.k.a. leaf
|
2017-11-01 13:26:10 +01:00
|
|
|
if content is not None:
|
2025-05-20 13:09:58 +02:00
|
|
|
gc = content if isinstance(content, StringTree) else StringTree('', content=content, parent=self.children[nibble])
|
2025-01-28 07:18:50 +01:00
|
|
|
# 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:
|
|
|
|
|
del self.children[nibble].children[gc.content]
|
2020-04-04 11:35:26 +02:00
|
|
|
self.children[nibble].children[gc.content] = gc
|
2017-11-01 13:26:10 +01:00
|
|
|
return self.children[nibble]
|
|
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def __str__(self) -> str:
|
2020-04-04 11:29:14 +02:00
|
|
|
return 'st:"{}"'.format(self.content)
|
|
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def __getitem__(self, path: str) -> str:
|
2017-11-01 13:26:10 +01:00
|
|
|
r = self.get(path)
|
|
|
|
|
if r is None:
|
|
|
|
|
raise KeyError(path)
|
2020-04-18 08:38:34 +02:00
|
|
|
return r.value() # type: ignore
|
2017-11-01 13:26:10 +01:00
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
return self.__set(key, value)
|
|
|
|
|
|
2017-11-22 09:34:26 +01:00
|
|
|
def __dump(self, prio, indent=0, **kwargs):
|
|
|
|
|
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
|
|
|
|
slog(prio, '|' + (' ' * indent) + str(self.content), caller=caller)
|
2017-11-01 13:26:10 +01:00
|
|
|
indent += 2
|
2019-05-19 13:15:27 +00:00
|
|
|
for name, child in self.children.items():
|
2017-11-01 13:26:10 +01:00
|
|
|
child.__dump(prio, indent=indent, caller=caller)
|
|
|
|
|
|
2025-05-29 12:04:52 +02:00
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
if self.__parent is None:
|
|
|
|
|
return ''
|
|
|
|
|
prefix = self.__parent.path
|
|
|
|
|
if len(prefix):
|
|
|
|
|
prefix += '.'
|
|
|
|
|
return prefix + self.content
|
|
|
|
|
|
2017-11-01 13:26:10 +01:00
|
|
|
def keys(self):
|
|
|
|
|
return self.children.keys()
|
|
|
|
|
|
2019-05-19 13:15:27 +00:00
|
|
|
def items(self):
|
|
|
|
|
return self.children.items()
|
2017-11-01 13:26:10 +01:00
|
|
|
|
|
|
|
|
def set_content(self, content):
|
|
|
|
|
if content is None:
|
|
|
|
|
raise Exception("Tried to set none content")
|
|
|
|
|
content = cleanup_string(content)
|
|
|
|
|
if len(content) == 0:
|
|
|
|
|
raise Exception("Tried to set empty content")
|
|
|
|
|
self.content = content
|
2017-11-22 09:34:26 +01:00
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
|
2019-05-19 13:15:27 +00:00
|
|
|
return self.__set(path, content, split)
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def get(self, path_: str) -> Optional[StringTree]:
|
2020-04-04 11:35:26 +02:00
|
|
|
slog(DEBUG, 'looking for "{}" in "{}"'.format(path_, self.content))
|
|
|
|
|
assert not isinstance(path_, int)
|
2017-11-01 13:26:10 +01:00
|
|
|
path = cleanup_string(path_)
|
|
|
|
|
if len(path) == 0:
|
|
|
|
|
slog(DEBUG, "returning myself")
|
|
|
|
|
return self
|
|
|
|
|
if is_quoted(path_):
|
|
|
|
|
if not path in self.children.keys():
|
|
|
|
|
return None
|
|
|
|
|
return self.children[path]
|
|
|
|
|
components = path.split('.')
|
|
|
|
|
if len(components) == 0:
|
|
|
|
|
return self
|
|
|
|
|
name = cleanup_string(components[0])
|
|
|
|
|
if not hasattr(self, "children"):
|
|
|
|
|
return None
|
|
|
|
|
if not name in self.children.keys():
|
|
|
|
|
slog(DEBUG, "Name \"" + name + "\" is not in children of", self.content)
|
|
|
|
|
for child in self.children:
|
|
|
|
|
slog(DEBUG, "child = ", child)
|
|
|
|
|
return None
|
|
|
|
|
relpath = '.'.join(components[1:])
|
|
|
|
|
return self.children[name].get(relpath)
|
|
|
|
|
|
2025-01-16 10:53:09 +01:00
|
|
|
def value(self, path = None, default=None) -> Optional[str]:
|
2020-04-18 08:38:34 +02:00
|
|
|
if path:
|
|
|
|
|
child = self.get(path)
|
|
|
|
|
if child is None:
|
2025-01-16 10:53:09 +01:00
|
|
|
if default:
|
|
|
|
|
return default
|
2020-04-18 08:38:34 +02:00
|
|
|
return None
|
|
|
|
|
return child.value()
|
2017-11-01 13:26:10 +01:00
|
|
|
if len(self.children) == 0:
|
2025-05-16 06:52:22 +02:00
|
|
|
raise Exception('tried to get value from leaf "{}"'.format(self.content))
|
2025-01-28 05:08:50 +01:00
|
|
|
slog(DEBUG, f'Returning value from children {self.children}')
|
2020-04-10 17:55:36 +02:00
|
|
|
return self.children[next(reversed(self.children))].content # type: ignore
|
2017-11-01 13:26:10 +01:00
|
|
|
|
2025-05-20 13:09:58 +02:00
|
|
|
@property
|
|
|
|
|
def parent(self):
|
|
|
|
|
return self.__parent
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def root(self):
|
|
|
|
|
if self.__parent is None:
|
|
|
|
|
return self
|
|
|
|
|
return self.__parent.root
|
|
|
|
|
|
2024-12-15 15:35:29 +01:00
|
|
|
def child_list(self, depth_first: bool=True) -> List[StringTree]:
|
2020-04-04 11:31:25 +02:00
|
|
|
if depth_first == False:
|
|
|
|
|
raise Exception("tried to retrieve child list with breadth-first search, not yet implemented")
|
|
|
|
|
r = []
|
2024-10-06 17:04:25 +02:00
|
|
|
for name, c in self.children.items():
|
2020-04-04 11:31:25 +02:00
|
|
|
r.append(c)
|
2024-10-06 17:04:25 +02:00
|
|
|
r.extend(c.child_list())
|
|
|
|
|
return r
|
2020-04-04 11:31:25 +02:00
|
|
|
|
2020-04-10 17:55:36 +02:00
|
|
|
def dump(self, prio: int, *args, **kwargs) -> None:
|
2017-11-22 09:34:26 +01:00
|
|
|
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
|
|
|
|
msg = ''
|
|
|
|
|
if args is not None:
|
|
|
|
|
msg = ' ' + ' '.join(args) + ' '
|
|
|
|
|
slog(prio, ",------------" + msg + "----------- >", caller=caller)
|
2017-11-01 13:26:10 +01:00
|
|
|
self.__dump(prio, indent=0, caller=caller)
|
2017-11-22 09:34:26 +01:00
|
|
|
slog(prio, "`------------" + msg + "----------- <", caller=caller)
|
2025-05-29 12:05:10 +02:00
|
|
|
|
|
|
|
|
class Match(Enum):
|
|
|
|
|
Equal = auto()
|
|
|
|
|
RegExArg = auto()
|
|
|
|
|
RegExConf = auto()
|
|
|
|
|
GlobArg = auto()
|
|
|
|
|
GlobConf = auto()
|
|
|
|
|
|
|
|
|
|
def __find(self, key: str|None, val: str|None, match: Match, depth_first: bool):
|
|
|
|
|
|
|
|
|
|
def __children():
|
|
|
|
|
for name, child in self.children.items():
|
|
|
|
|
ret.extend(child.__find(key, val, match, depth_first))
|
|
|
|
|
|
|
|
|
|
def __self():
|
|
|
|
|
_val = self.value()
|
|
|
|
|
_content = self.content
|
|
|
|
|
try:
|
|
|
|
|
if (
|
|
|
|
|
(key == _content and matcher(val, _val))
|
|
|
|
|
or (key is None and matcher(val, _val))
|
|
|
|
|
or (key == _content and val is None)
|
|
|
|
|
):
|
|
|
|
|
ret.append(self)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
if isinstance(e, re.PatternError):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
def __debug_matcher(matcher, log_level=DEBUG):
|
|
|
|
|
def __matcher(x, y):
|
|
|
|
|
slog(log_level, f'Comparing "{x}" ~ "{y}"')
|
|
|
|
|
return matcher(x, y)
|
|
|
|
|
return __matcher
|
|
|
|
|
|
|
|
|
|
if not self.children:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
matcher = lambda x, y: x == y
|
|
|
|
|
match match:
|
|
|
|
|
case self.Match.Equal:
|
|
|
|
|
pass
|
|
|
|
|
case self.Match.RegExArg:
|
|
|
|
|
matcher = lambda x, y: re.search(x, y) is not None
|
|
|
|
|
case self.Match.RegExConf:
|
|
|
|
|
matcher = lambda x, y: re.search(y, x) is not None
|
|
|
|
|
case self.Match.GlobArg:
|
|
|
|
|
matcher = lambda x, y: fnmatch.fnmatch(y, x)
|
|
|
|
|
case self.Match.GlobConf:
|
|
|
|
|
matcher = lambda x, y: fnmatch.fnmatch(x, y)
|
|
|
|
|
case _:
|
|
|
|
|
raise NotImplementedError(f'Matcher {match} is not yet implemented')
|
|
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
|
|
|
|
|
|
if depth_first:
|
|
|
|
|
__children()
|
|
|
|
|
__self()
|
|
|
|
|
else:
|
|
|
|
|
__self()
|
|
|
|
|
__children()
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def find(self, key: str|None=None, val: str|None=None, match: Match=Match.Equal, depth_first: bool=False):
|
|
|
|
|
return [ node.parent.path for node in self.__find(key, val, match=match, depth_first=depth_first)]
|