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]
def get_config():
def get_config(custom_config=None):
"""Return the path of the found configuration file and its content
:returns: (path, config)
:rtype: (str, dict)
"""
if custom_config:
global CONFIG_FILES
CONFIG_FILES = [custom_config]
for f in CONFIG_FILES:
try:
with open(f, 'r') as c:
@ -30,6 +34,11 @@ def get_config():
# accessible or if the file is not present at all
# in any of CONFIG_PATHS.
pass
else:
if custom_config:
raise FileNotFoundError('Config file {} not found.'
.format(custom_config))
else:
raise FileNotFoundError("Could not find {} in any of {}."
.format(CONFIG_FILE, ', '.join(CONFIG_PATHS)))
.format(CONFIG_FILE,
', '.join(CONFIG_PATHS)))

View File

@ -1,5 +1,7 @@
#!/usr/bin/env python3
from pprint import pformat as pp
import sys
import argparse
import phi.ldap.client
from phi.config import get_config
@ -9,13 +11,17 @@ log = get_logger(__name__)
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', {}))
# log.info("Found configuration at '{}':\n{}"
# .format(config_file, pp(config)))
log.info("Found configuration at '{}':\n".format(config_file))
log.info("Using configuration at '{}':\n".format(config_file))
log.info('Opening LDAP client')
client = phi.ldap.client.Client(**config['ldap'])