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

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