stree.serdes: Support glob patterns for includes

Allow -include /some/dir/app.d/*.conf style includes.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-01 20:42:40 +02:00
commit 1347a34481

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os, glob
from jwutils.stree.StringTree import * from jwutils.stree.StringTree import *
from jwutils.log import * from jwutils.log import *
@ -70,7 +70,7 @@ 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) root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
return root return root
def _read_lines(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None): def _read_lines_from_one_path(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
try: try:
with open(path, 'r') as infile: with open(path, 'r') as infile:
slog(log_prio, 'Reading {}"{}"'.format(' ' * level * 2, path)) slog(log_prio, 'Reading {}"{}"'.format(' ' * level * 2, path))
@ -102,7 +102,17 @@ def _read_lines(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
slog(DEBUG, msg) slog(DEBUG, msg)
return None return None
def _read_lines(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
paths = glob.glob(path)
ret = []
for p in paths:
rr = _read_lines_from_one_path(p, throw=throw, level=level, log_prio=log_prio, paths_buf=paths_buf)
if rr is None:
return None
ret.extend(rr)
return ret
def read(path: str, root_content: str='root', log_prio=INFO, paths_buf=None) -> StringTree: # export 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) lines = _read_lines_from_one_path(path, log_prio=log_prio, paths_buf=paths_buf)
s = ''.join(lines) s = ''.join(lines)
return parse(s, root_content=root_content) return parse(s, root_content=root_content)