Add class Config

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-01-20 11:39:56 +01:00
commit 84a4053157
2 changed files with 111 additions and 4 deletions

View file

@ -68,9 +68,12 @@ def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> Stri
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
return root
def _read_lines(path: str, throw=True):
def _read_lines(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
try:
with open(path, 'r') as infile:
slog(log_prio, 'Reading {}"{}"'.format(' ' * level * 2, path))
if paths_buf is not None:
paths_buf.append(path)
ret = []
for line in infile: # lines are all trailed by \n
m = re.search(r'^\s*(-)*include\s+(\S+)', line)
@ -81,7 +84,7 @@ def _read_lines(path: str, throw=True):
dir_name = os.path.dirname(path)
if len(dir_name):
include_path = dir_name + '/' + include_path
include_lines = _read_lines(include_path, throw=(not optional))
include_lines = _read_lines(include_path, throw=(not optional), level=level+1, paths_buf=paths_buf)
if include_lines is None:
msg = f'{path}: Failed to process "{line}"'
slog(DEBUG, line)
@ -97,7 +100,7 @@ def _read_lines(path: str, throw=True):
slog(DEBUG, msg)
return None
def read(path: str, root_content: str='root') -> StringTree: # export
lines = _read_lines(path)
def read(path: str, root_content: str='root', log_prio=INFO, paths_buf=None) -> StringTree: # export
lines = _read_lines(path, log_prio=log_prio, paths_buf=paths_buf)
s = ''.join(lines)
return parse(s, root_content=root_content)