mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
StringTree: Add support for adding StringTree children
Add support for adding children of type StringTree. If a StringTree argument is passed to .add(), this should do the right thing. It makes use of the newly added .__add_children() method and involves quite a bit of hairy case distinctions. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
dead9d9561
commit
dc532704d0
1 changed files with 39 additions and 15 deletions
|
|
@ -48,29 +48,51 @@ 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):
|
||||||
|
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():
|
||||||
|
if not name in self.children.keys():
|
||||||
|
slog(DEBUG, "{}: adding new child: ".format(str(self), str(c)))
|
||||||
|
self.children[name] = c
|
||||||
|
else:
|
||||||
|
self.children[name].__add_children(c)
|
||||||
|
|
||||||
def __set(self, path_, content, split=True):
|
def __set(self, path_, content, split=True):
|
||||||
logcontent = "not-yet"
|
slog(DEBUG, "+ setting >" + str(content) + "< at path \"" + str(path_) + "\"" +
|
||||||
if hasattr(self, "content"):
|
(', containing "{}"'.format(str(self.content))) if hasattr(self, "content") else "")
|
||||||
logcontent = self.content
|
assert self.content != str(content)
|
||||||
slog(DEBUG, "+ setting >" + str(content) + "< at path \"" + str(path_) + "\" to", logcontent)
|
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__))
|
||||||
if path_ is None:
|
if path_ is None:
|
||||||
self.content = cleanup_string(content)
|
if isinstance(content, str):
|
||||||
|
self.content = cleanup_string(content)
|
||||||
|
elif isinstance(content, StringTree):
|
||||||
|
self.__add_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) + "<")
|
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
||||||
return self
|
return self
|
||||||
path = cleanup_string(path_)
|
path = cleanup_string(path_)
|
||||||
components = path.split('.') if split else [ path ]
|
components = path.split('.') if split else [ path ]
|
||||||
l = len(components)
|
l = len(components)
|
||||||
if len(path) == 0 or l == 0:
|
if len(path) == 0 or l == 0:
|
||||||
assert self.content is None
|
#assert self.content is None or (isinstance(content, StringTree) and content.content == self.content)
|
||||||
#self.content = cleanup_string(content)
|
if isinstance(content, StringTree):
|
||||||
self.content = content
|
#assert isinstance(content, StringTree), "Type: " + type(content).__name__
|
||||||
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
|
self.__add_children(content)
|
||||||
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
|
else:
|
||||||
assert(content != '"[a-zA-Z0-9+_*/-]"')
|
#self.content = cleanup_string(content)
|
||||||
#self.children[content] = StringTree(None, 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)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
assert self.content is not None
|
#assert self.content is not None, "tried to set empty content to {}".format(path_)
|
||||||
|
|
||||||
nibble = components[0]
|
nibble = components[0]
|
||||||
rest = '.'.join(components[1:])
|
rest = '.'.join(components[1:])
|
||||||
|
|
@ -80,7 +102,8 @@ class StringTree: # export
|
||||||
assert len(rest) > 0
|
assert len(rest) > 0
|
||||||
return self.children[nibble].__set(rest, content=content)
|
return self.children[nibble].__set(rest, content=content)
|
||||||
if content is not None:
|
if content is not None:
|
||||||
self.children[nibble].children[content] = StringTree('', content=content)
|
gc = content if isinstance(content, StringTree) else StringTree('', content=content)
|
||||||
|
self.children[nibble].children[gc.content] = gc
|
||||||
return self.children[nibble]
|
return self.children[nibble]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -121,7 +144,8 @@ class StringTree: # export
|
||||||
return self.__set(path, content, split)
|
return self.__set(path, content, split)
|
||||||
|
|
||||||
def get(self, path_):
|
def get(self, path_):
|
||||||
slog(DEBUG, "looking for", path_, "in", self.content)
|
slog(DEBUG, 'looking for "{}" in "{}"'.format(path_, self.content))
|
||||||
|
assert not isinstance(path_, int)
|
||||||
path = cleanup_string(path_)
|
path = cleanup_string(path_)
|
||||||
if len(path) == 0:
|
if len(path) == 0:
|
||||||
slog(DEBUG, "returning myself")
|
slog(DEBUG, "returning myself")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue