Config.branch(): Add throw argument

In case a branch doesn't exist, branch() throws an error. Allow to
return None instead.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-07-27 18:58:28 +02:00
commit e4cee86482

View file

@ -141,13 +141,16 @@ class Config(): # export
def value(self, key: str, default = None) -> Optional[str]:
return self.get(key, default)
def branch(self, path: str): # type: ignore # Optional[Config]: FIXME: Don't know how to get hold of this type here
def branch(self, path: str, throw: bool=True): # type: ignore # Optional[Config]: FIXME: Don't know how to get hold of this type here
if self.__conf:
tree = self.__conf.get(path)
if tree is None:
msg = f'Tried to get non-existent branch "{path}" from config'
if not throw:
slog(DEBUG, msg)
return None
self.dump(ERR, msg)
throw(msg)
raise Exception(msg)
return Config(tree=tree, parent=self) # type: ignore
return None