From 1c0c5ab884f7d7f74edc485e1972961ba0b0aa3d Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 19 May 2019 13:15:27 +0000 Subject: [PATCH] Attempt to support parsing full lines into StringTree This commit tries to add support for the [section] first line second line syntax, i.e. non-assignments, without equal sign. Half-baked, but still better than before. Also support some more Python 3, i.e. dict.items() instead of iteritems() Signed-off-by: Jan Lindemann --- tools/python/jwutils/stree/StringTree.py | 16 ++++++++-------- tools/python/jwutils/stree/serdes.py | 12 ++++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tools/python/jwutils/stree/StringTree.py b/tools/python/jwutils/stree/StringTree.py index d67f332..fe9f015 100644 --- a/tools/python/jwutils/stree/StringTree.py +++ b/tools/python/jwutils/stree/StringTree.py @@ -48,7 +48,7 @@ class StringTree: # export # regex ( content = regex, children = [ '[ \n\t\r]+' ] ) # '[ \n\t\r]+)' ( content = '\n\t\r]+)', children = [] ) - def __set(self, path_, content): + def __set(self, path_, content, split=True): logcontent = "not-yet" if hasattr(self, "content"): logcontent = self.content @@ -58,7 +58,7 @@ class StringTree: # export slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<") return self path = cleanup_string(path_) - components = path.split('.') + components = path.split('.') if split else [ path ] l = len(components) if len(path) == 0 or l == 0: assert self.content is None @@ -96,14 +96,14 @@ class StringTree: # export caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1) slog(prio, '|' + (' ' * indent) + str(self.content), caller=caller) indent += 2 - for name, child in self.children.iteritems(): + for name, child in self.children.items(): child.__dump(prio, indent=indent, caller=caller) def keys(self): return self.children.keys() - def iteritems(self): - return self.children.iteritems() + def items(self): + return self.children.items() def set_content(self, content): if content is None: @@ -113,9 +113,9 @@ class StringTree: # export raise Exception("Tried to set empty content") self.content = content - def add(self, path, content = None): - slog(DEBUG, "adding", content, "at", path, "to", self.content) - return self.__set(path, content) + def add(self, path, content = None, split = True): + slog(DEBUG, "adding >{}< at >{}< to >{}<".format(content, path, self.content)) + return self.__set(path, content, split) def get(self, path_): slog(DEBUG, "looking for", path_, "in", self.content) diff --git a/tools/python/jwutils/stree/serdes.py b/tools/python/jwutils/stree/serdes.py index c712438..508b671 100644 --- a/tools/python/jwutils/stree/serdes.py +++ b/tools/python/jwutils/stree/serdes.py @@ -1,4 +1,4 @@ -from StringTree import * +from jwutils.stree.StringTree import * from jwutils.log import * def _cleanup_line(line): @@ -20,7 +20,7 @@ def _cleanup_line(line): return r[1:-1] return r -def parse(s): # export +def parse(s, allow_full_lines=True): # export slog(DEBUG, "parsing", s) root = StringTree('', content='root') sec = '' @@ -57,9 +57,13 @@ def parse(s): # export continue rhs += c + split = True if rhs is None: - raise Exception("failed to parse assignment", line) - root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs)) + if not allow_full_lines: + raise Exception("failed to parse assignment", line) + rhs = 'empty' + split = False + root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split) return root def read(path): # export