Load custom configs

This commit is contained in:
uid 2020-10-10 21:14:21 +02:00
parent 75e2ee1b04
commit d6f48e4861
2 changed files with 23 additions and 8 deletions

View File

@ -13,12 +13,16 @@ CONFIG_FILES = [os.path.join(p, CONFIG_FILE)
for p in CONFIG_PATHS] for p in CONFIG_PATHS]
def get_config(): def get_config(custom_config=None):
"""Return the path of the found configuration file and its content """Return the path of the found configuration file and its content
:returns: (path, config) :returns: (path, config)
:rtype: (str, dict) :rtype: (str, dict)
""" """
if custom_config:
global CONFIG_FILES
CONFIG_FILES = [custom_config]
for f in CONFIG_FILES: for f in CONFIG_FILES:
try: try:
with open(f, 'r') as c: with open(f, 'r') as c:
@ -31,5 +35,10 @@ def get_config():
# in any of CONFIG_PATHS. # in any of CONFIG_PATHS.
pass pass
else: else:
raise FileNotFoundError("Could not find {} in any of {}." if custom_config:
.format(CONFIG_FILE, ', '.join(CONFIG_PATHS))) raise FileNotFoundError('Config file {} not found.'
.format(custom_config))
else:
raise FileNotFoundError("Could not find {} in any of {}."
.format(CONFIG_FILE,
', '.join(CONFIG_PATHS)))

View File

@ -1,5 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from pprint import pformat as pp from pprint import pformat as pp
import sys
import argparse
import phi.ldap.client import phi.ldap.client
from phi.config import get_config from phi.config import get_config
@ -9,13 +11,17 @@ log = get_logger(__name__)
if __name__ == '__main__': if __name__ == '__main__':
config_file, config = get_config() parser = argparse.ArgumentParser()
parser.add_argument('--config', metavar='config.yml',
type=str, help='custom configuration file')
ns = parser.parse_args(sys.argv[1:])
config_file = ns.config
config_file, config = get_config(config_file)
setup_logging(config.get('logging', {})) setup_logging(config.get('logging', {}))
log.info("Using configuration at '{}':\n".format(config_file))
# log.info("Found configuration at '{}':\n{}"
# .format(config_file, pp(config)))
log.info("Found configuration at '{}':\n".format(config_file))
log.info('Opening LDAP client') log.info('Opening LDAP client')
client = phi.ldap.client.Client(**config['ldap']) client = phi.ldap.client.Client(**config['ldap'])