stree/serdes.py: Add support for kwarg "root_content"

parse() and read() up to present automatically set the root content
of a StringTree object to "root". This can now be optionally defined
by the "root_content" keyword argument.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2020-04-04 11:14:33 +02:00
commit 020bb970bd

View file

@ -20,9 +20,9 @@ def _cleanup_line(line):
return r[1:-1] return r[1:-1]
return r return r
def parse(s, allow_full_lines=True): # export def parse(s, allow_full_lines=True, root_content='root'): # export
slog(DEBUG, "parsing", s) slog(DEBUG, "parsing", s)
root = StringTree('', content='root') root = StringTree('', content=root_content)
sec = '' sec = ''
for line in s.splitlines(): for line in s.splitlines():
slog(DEBUG, "line=", line) slog(DEBUG, "line=", line)
@ -66,7 +66,7 @@ def parse(s, allow_full_lines=True): # export
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split) root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
return root return root
def read(path): # export def read(path, root_content='root'): # export
with open(path, 'r') as infile: with open(path, 'r') as infile:
s = infile.read() s = infile.read()
return parse(s) return parse(s, root_content=root_content)