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

Display more helpful permission errors

parent 2cc34496
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import os, getpass
import toml
from pathlib import Path
......@@ -42,12 +42,12 @@ def get_dirs(application_name):
dirs["config"] = this_or_else(os.environ.get("XDG_CONFIG_HOME"), default_config_home)
# Override this directory from Environment variable
key = "{}_DATA_DIR".format(application_name.upper())
key = "{}_DATA_DIR".format(application_name.upper()).replace("_", "-")
if os.getenv(key) is not None:
dirs["data"] = os.getenv(key)
# Override this directory from Environment variable
key = "{}_CONFIG_DIR".format(application_name.upper())
key = "{}_CONFIG_DIR".format(application_name.upper()).replace("_", "-")
if os.getenv(key) is not None:
dirs["config"] = os.getenv(key)
......@@ -75,14 +75,29 @@ 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))
Path(config_dir).mkdir(parents=True, exist_ok=True)
app.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)
exit(1)
except Error as e:
app.logger.error(e)
# Create a default config if it does exist
if not Path.is_file(Path(config_path)):
write_config(config_path, default_config)
try:
app.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))
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()))
exit(1)
else:
config = read_config(config_path)
if config_has_missing_keys(config, default_config, default_strconfig, app):
......@@ -124,9 +139,16 @@ def config_has_missing_keys(config, default_config, default_strconfig, app) -> b
def write_config(config_path: str, config: dict):
"""
Write a dict as toml to a given path
If config is of type dict use toml.dump
If config is of type str use f.write
"""
if type(config) == dict:
with open(config_path, "w", encoding="utf-8") as f:
toml.dump(config, f)
elif type(config) == str:
with open(config_path, "w", encoding="utf-8") as f:
f.write(config)
def read_config(config_path: str) -> dict:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment