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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2019-05-19 13:15:27 +00:00
commit 1c0c5ab884
2 changed files with 16 additions and 12 deletions

View file

@ -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)

View file

@ -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:
if not allow_full_lines:
raise Exception("failed to parse assignment", line)
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs))
rhs = 'empty'
split = False
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
return root
def read(path): # export