"""Function regarding the general configuration of MeaVis."""
import logging
import logging.config
import os
import shutil
import sys
import yaml
[docs]def get_app_path():
"""Get the application configuration folder regarding operating system."""
if sys.platform.startswith("win32"):
app_path = os.path.expanduser(
os.path.join("~", "AppData", "Roaming", "meavis")
)
elif sys.platform.startswith("darwin"):
app_path = os.path.expanduser(
os.path.join("~", "Library", "Preferences", "meavis")
)
else:
app_path = os.path.expanduser(os.path.join("~", ".config", "meavis"))
if not os.path.exists(app_path):
os.makedirs(app_path)
return app_path
[docs]def deploy():
"""Deploy missing configuration files."""
for data_file in os.listdir(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
):
data_path = os.path.join(get_app_path(), data_file)
if os.path.exists(data_path):
continue
shutil.copy2(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "data", data_file
),
data_path,
)
[docs]def is_deploy():
"""Check is all configuration files are deployed."""
return set(
os.listdir(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
)
) <= set(os.listdir(get_app_path()))
[docs]def load_logger():
"""Read, patch and load log configuration."""
with open(os.path.join(get_app_path(), "logging.conf")) as file:
log_conf = yaml.safe_load(file)
log_conf["handlers"]["file_short"]["filename"] = os.path.join(
get_app_path(), log_conf["handlers"]["file_short"]["filename"]
)
log_conf["handlers"]["file_long"]["filename"] = os.path.join(
get_app_path(), log_conf["handlers"]["file_long"]["filename"]
)
logging.config.dictConfig(log_conf)
[docs]def log_and_raise(cls, message):
"""Log a error and then raise it as a python exception."""
logging.getLogger("meavis").error("{}: {}".format(cls.__name__, message))
raise cls(message)
if not is_deploy():
deploy()
load_logger()
default_map = yaml.safe_load(
open(os.path.expanduser(os.path.join(get_app_path(), "default.yaml")))
)
logging.getLogger("meavis").info("Load MeaVis default attributes map.")