mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
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:
parent
64ce94b142
commit
8c0b975e1b
1 changed files with 18 additions and 6 deletions
|
|
@ -13,9 +13,7 @@ from .log import *
|
||||||
|
|
||||||
class Config(): # export
|
class Config(): # export
|
||||||
|
|
||||||
def __init__(self, search_dirs: Optional[list[str]]=None, glob_paths: Optional[list[str]]=None,
|
def __load(self, search_dirs, glob_paths):
|
||||||
defaults: Dict[str, str]=None, tree: Optional[StringTree]=None, parent=None,
|
|
||||||
root_section=None) -> None:
|
|
||||||
|
|
||||||
def __is_abs(path):
|
def __is_abs(path):
|
||||||
if path is None:
|
if path is None:
|
||||||
|
|
@ -26,14 +24,13 @@ class Config(): # export
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.__parent = parent
|
ret = StringTree("", "")
|
||||||
exe = Path(os.path.basename(sys.argv[0])).stem
|
exe = Path(os.path.basename(sys.argv[0])).stem
|
||||||
if glob_paths is None:
|
if glob_paths is None:
|
||||||
glob_paths = [f'.{exe}', f'{exe}.conf']
|
glob_paths = [f'.{exe}', f'{exe}.conf']
|
||||||
if search_dirs is None:
|
if search_dirs is None:
|
||||||
env_key = re.sub('[-.]', '_', exe)
|
env_key = re.sub('[-.]', '_', exe)
|
||||||
search_dirs = os.getenv(env_key)
|
search_dirs = os.getenv(env_key)
|
||||||
self.__conf = tree if tree else StringTree("", "")
|
|
||||||
for path in glob_paths:
|
for path in glob_paths:
|
||||||
dirs = search_dirs
|
dirs = search_dirs
|
||||||
if dirs is None:
|
if dirs is None:
|
||||||
|
|
@ -60,7 +57,22 @@ class Config(): # export
|
||||||
slog(ERR, f' {((os.stat(p).st_mode) & 0o7777):o} {pp}')
|
slog(ERR, f' {((os.stat(p).st_mode) & 0o7777):o} {pp}')
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
tree.dump(DEBUG, f)
|
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:
|
if root_section is not None:
|
||||||
self.__conf = self.__conf.get(root_section)
|
self.__conf = self.__conf.get(root_section)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue