diff --git a/tools/python/jwutils/stree/serdes.py b/tools/python/jwutils/stree/serdes.py index 4525c12..f2ad994 100644 --- a/tools/python/jwutils/stree/serdes.py +++ b/tools/python/jwutils/stree/serdes.py @@ -1,3 +1,5 @@ +import os + from jwutils.stree.StringTree import * from jwutils.log import * @@ -66,7 +68,36 @@ 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): + try: + with open(path, 'r') as infile: + ret = [] + for line in infile: # lines are all trailed by \n + m = re.search(r'^\s*(-)*include\s+(\S+)', line) + if m: + optional = m.group(1) == '-' + include_path = m.group(2) + if include_path[0] != '/': + 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)) + if include_lines is None: + msg = f'{path}: Failed to process "{line}"' + slog(DEBUG, line) + continue + ret.extend(include_lines) + continue + ret.append(line) + return ret + except Exception as e: + msg = f'Failed to read file "{path}": {e}' + if throw: + raise Exception(msg) + slog(DEBUG, msg) + return None + def read(path: str, root_content: str='root') -> StringTree: # export - with open(path, 'r') as infile: - s = infile.read() + lines = _read_lines(path) + s = ''.join(lines) return parse(s, root_content=root_content)