mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
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:
parent
109179b55e
commit
1c0c5ab884
2 changed files with 16 additions and 12 deletions
|
|
@ -48,7 +48,7 @@ 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 __set(self, path_, content):
|
def __set(self, path_, content, split=True):
|
||||||
logcontent = "not-yet"
|
logcontent = "not-yet"
|
||||||
if hasattr(self, "content"):
|
if hasattr(self, "content"):
|
||||||
logcontent = self.content
|
logcontent = self.content
|
||||||
|
|
@ -58,7 +58,7 @@ class StringTree: # export
|
||||||
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('.')
|
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
|
||||||
|
|
@ -96,14 +96,14 @@ class StringTree: # export
|
||||||
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
|
||||||
slog(prio, '|' + (' ' * indent) + str(self.content), caller=caller)
|
slog(prio, '|' + (' ' * indent) + str(self.content), caller=caller)
|
||||||
indent += 2
|
indent += 2
|
||||||
for name, child in self.children.iteritems():
|
for name, child in self.children.items():
|
||||||
child.__dump(prio, indent=indent, caller=caller)
|
child.__dump(prio, indent=indent, caller=caller)
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return self.children.keys()
|
return self.children.keys()
|
||||||
|
|
||||||
def iteritems(self):
|
def items(self):
|
||||||
return self.children.iteritems()
|
return self.children.items()
|
||||||
|
|
||||||
def set_content(self, content):
|
def set_content(self, content):
|
||||||
if content is None:
|
if content is None:
|
||||||
|
|
@ -113,9 +113,9 @@ class StringTree: # export
|
||||||
raise Exception("Tried to set empty content")
|
raise Exception("Tried to set empty content")
|
||||||
self.content = content
|
self.content = content
|
||||||
|
|
||||||
def add(self, path, content = None):
|
def add(self, path, content = None, split = True):
|
||||||
slog(DEBUG, "adding", content, "at", path, "to", self.content)
|
slog(DEBUG, "adding >{}< at >{}< to >{}<".format(content, path, self.content))
|
||||||
return self.__set(path, content)
|
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", path_, "in", self.content)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from StringTree import *
|
from jwutils.stree.StringTree import *
|
||||||
from jwutils.log import *
|
from jwutils.log import *
|
||||||
|
|
||||||
def _cleanup_line(line):
|
def _cleanup_line(line):
|
||||||
|
|
@ -20,7 +20,7 @@ def _cleanup_line(line):
|
||||||
return r[1:-1]
|
return r[1:-1]
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def parse(s): # export
|
def parse(s, allow_full_lines=True): # export
|
||||||
slog(DEBUG, "parsing", s)
|
slog(DEBUG, "parsing", s)
|
||||||
root = StringTree('', content='root')
|
root = StringTree('', content='root')
|
||||||
sec = ''
|
sec = ''
|
||||||
|
|
@ -57,9 +57,13 @@ def parse(s): # export
|
||||||
continue
|
continue
|
||||||
rhs += c
|
rhs += c
|
||||||
|
|
||||||
|
split = True
|
||||||
if rhs is None:
|
if rhs is None:
|
||||||
raise Exception("failed to parse assignment", line)
|
if not allow_full_lines:
|
||||||
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs))
|
raise Exception("failed to parse assignment", line)
|
||||||
|
rhs = 'empty'
|
||||||
|
split = False
|
||||||
|
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
|
||||||
return root
|
return root
|
||||||
|
|
||||||
def read(path): # export
|
def read(path): # export
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue