Skip to content
Snippets Groups Projects
Commit 5ea244ff authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Pass logger instead of app to be more generic

parent 162aa093
No related branches found
No related tags found
No related merge requests found
......@@ -54,7 +54,7 @@ def get_dirs(application_name):
return dirs
def initialize_config(application_name: str, default_config: str, app) -> dict:
def initialize_config(application_name: str, default_config: str, logger) -> dict:
"""
Initialize a configuration. If none exists, create a default one
"""
......@@ -76,41 +76,41 @@ def initialize_config(application_name: str, default_config: str, app) -> dict:
# Create the config_dir if it doesn't exist
if not Path.is_dir(Path(config_dir)):
try:
app.logger.warning(os.environ.get(config_path_environment_variable))
logger.warning(os.environ.get(config_path_environment_variable))
Path(config_dir).mkdir(parents=True, exist_ok=True)
app.logger.info("Config directory didn't exist, created directory: {}".format(config_dir))
logger.info("Config directory didn't exist, created directory: {}".format(config_dir))
except PermissionError as e:
app.logger.error("Error: Not sufficient permissions to create config directory at {} (Running {} as user {})".format(config_dir, application_name, getpass.getuser()))
app.logger.error("Consider creating {} manually with write permissions for {}. After initial write of default config.toml on the first run {} won't write that file ever again".format(config_dir, getpass.getuser(), application_name), file=sys.stderr)
logger.error("Error: Not sufficient permissions to create config directory at {} (Running {} as user {})".format(config_dir, application_name, getpass.getuser()))
logger.error("Consider creating {} manually with write permissions for {}. After initial write of default config.toml on the first run {} won't write that file ever again".format(config_dir, getpass.getuser(), application_name), file=sys.stderr)
exit(1)
except FileExistsError as e:
app.logger.debug("Config dir at {} already existed".format(config_dir))
logger.debug("Config dir at {} already existed".format(config_dir))
# Create a default config if it does exist
if not Path.is_file(Path(config_path)):
try:
app.logger.debug("Attempting to write default config to {}".format(config_path))
logger.debug("Attempting to write default config to {}".format(config_path))
write_config(config_path, default_strconfig)
app.logger.info("Created new default config.toml at:\n{}".format(config_path))
logger.info("Created new default config.toml at:\n{}".format(config_path))
config = default_config
except PermissionError as e:
logging.error("Error: Not sufficient permissions to write default config.toml into directory {} (as user {})".format(config_dir, getpass.getuser()))
logging.error("Consider changing permissions on {} manually so writing is allowed for user {}. After initial write of default config.toml on the first run {} won't write that file ever again. so you can make it read only for {} after that".format(config_dir, getpass.getuser(), application_name, getpass.getuser()))
logger.error("Error: Not sufficient permissions to write default config.toml into directory {} (as user {})".format(config_dir, getpass.getuser()))
logger.error("Consider changing permissions on {} manually so writing is allowed for user {}. After initial write of default config.toml on the first run {} won't write that file ever again. so you can make it read only for {} after that".format(config_dir, getpass.getuser(), application_name, getpass.getuser()))
exit(1)
else:
config = read_config(config_path)
if config_has_missing_keys(config, default_config, default_strconfig, app):
app.logger.error("Error: there are keys missing in the config. Delete to let {} create a new one, or add the missing keys manually".format(application_name))
if config_has_missing_keys(config, default_config, default_strconfig, logger):
logger.error("Error: there are keys missing in the config. Delete to let {} create a new one, or add the missing keys manually".format(application_name))
exit(1)
app.logger.info("Read config from: {}".format(config_path))
logger.info("Read config from: {}".format(config_path))
# Log info about environment variable if not set
if os.environ.get(config_path_environment_variable) is None:
app.logger.info("Override config path by setting the Environment variable {}=".format(config_path_environment_variable))
logger.info("Override config path by setting the Environment variable {}=".format(config_path_environment_variable))
return config
def config_has_missing_keys(config, default_config, default_strconfig, app) -> bool:
def config_has_missing_keys(config, default_config, default_strconfig, logger) -> bool:
"""
Returns True if any of the keys from the default config are missing
Prints those missing keys and the default config, if this is the case
......@@ -118,17 +118,17 @@ def config_has_missing_keys(config, default_config, default_strconfig, app) -> b
missing = []
for default_key in default_config.keys():
if not default_key in config.keys():
app.logger.error("Your config.toml is missing the [{}] section!".format(default_key))
logger.error("Your config.toml is missing the [{}] section!".format(default_key))
missing.append(True)
else:
for sub_key in default_config[default_key].keys():
if not sub_key in config[default_key].keys():
app.logger.error("Your config.toml is missing the key \"{}\" in the [{}] section!".format(sub_key, default_key))
logger.error("Your config.toml is missing the key \"{}\" in the [{}] section!".format(sub_key, default_key))
missing.append(True)
if any(missing):
app.logger.error("The default config looks like this:")
app.logger.error(default_strconfig)
logger.error("The default config looks like this:")
logger.error(default_strconfig)
return True
else:
return False
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment