Config: Mutually exclude loading from tree or files

Initializing Config from a StringTree object doesn't stop it from
looking for config files to read. Stop that.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-01-28 08:05:16 +01:00
commit 8c0b975e1b

View file

@ -13,9 +13,7 @@ from .log import *
class Config(): # export
def __init__(self, search_dirs: Optional[list[str]]=None, glob_paths: Optional[list[str]]=None,
defaults: Dict[str, str]=None, tree: Optional[StringTree]=None, parent=None,
root_section=None) -> None:
def __load(self, search_dirs, glob_paths):
def __is_abs(path):
if path is None:
@ -26,14 +24,13 @@ class Config(): # export
return False
return True
self.__parent = parent
ret = StringTree("", "")
exe = Path(os.path.basename(sys.argv[0])).stem
if glob_paths is None:
glob_paths = [f'.{exe}', f'{exe}.conf']
if search_dirs is None:
env_key = re.sub('[-.]', '_', exe)
search_dirs = os.getenv(env_key)
self.__conf = tree if tree else StringTree("", "")
for path in glob_paths:
dirs = search_dirs
if dirs is None:
@ -60,7 +57,22 @@ class Config(): # export
slog(ERR, f' {((os.stat(p).st_mode) & 0o7777):o} {pp}')
raise Exception(msg)
tree.dump(DEBUG, f)
self.__conf.add("", tree)
ret.add("", tree)
return ret
def __init__(self, search_dirs: Optional[list[str]]=None, glob_paths: Optional[list[str]]=None,
defaults: Dict[str, str]=None, tree: Optional[StringTree]=None, parent=None,
root_section=None) -> None:
self.__parent = parent
if tree is not None:
assert(search_dirs is None)
assert(glob_paths is None)
self.__conf = tree
else:
assert(tree is None)
self.__conf = self.__load(search_dirs=search_dirs, glob_paths=glob_paths)
if root_section is not None:
self.__conf = self.__conf.get(root_section)