config don’t read cli args directly

This commit is contained in:
Guilhem Fauré 2023-06-21 11:54:27 +02:00
parent d86fa7ab10
commit dea2f45155

View File

@ -23,12 +23,10 @@ from yaml import Loader, load
NAME: str = "spip2md" # Name of program, notably used in logs NAME: str = "spip2md" # Name of program, notably used in logs
# Searches for a configuration file from all CLI args and in standard locations # Searches for a configuration file from standard locations or params
# & return his path if found
def config(*start_locations: str) -> Optional[str]: def config(*start_locations: str) -> Optional[str]:
# Search for config files in CLI arguments and function params first # Search for config files in function params first
argv = __import__("sys").argv config_locations: list[str] = list(start_locations)
config_locations: list[str] = argv[1:] + list(start_locations)
if "XDG_CONFIG_HOME" in environ: if "XDG_CONFIG_HOME" in environ:
config_locations += [ config_locations += [
@ -50,6 +48,7 @@ def config(*start_locations: str) -> Optional[str]:
"/spip2md.yaml", "/spip2md.yaml",
] ]
# Return the first path that actually exists
for path in config_locations: for path in config_locations:
if isfile(path): if isfile(path):
return path return path
@ -98,6 +97,3 @@ class Configuration:
setattr(self, attr, directory) setattr(self, attr, directory)
else: else:
setattr(self, attr, config[attr]) setattr(self, attr, config[attr])
CFG = Configuration(config())