From c1edf06dee8963a155a153af93a5da4b9a474eb8 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 2 Dec 2015 22:07:56 +0000 Subject: [PATCH] Added load of config files --- project/config/load.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 project/config/load.py diff --git a/project/config/load.py b/project/config/load.py new file mode 100644 index 0000000..f772d9c --- /dev/null +++ b/project/config/load.py @@ -0,0 +1,29 @@ +import os +from yaml import load, dump + +try: + from yaml import CLoader as Loader, CDumper as Dumper +except ImportError: + from yaml import Loader, Dumper + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +USER_CONFIG_DIR = "~/.dfa.yml" + +def get_config_data(filename): + try: + config_file = open(filename) + return load(config_file, Loader=Loader) + except FileNotFoundError: + return None + + +DEFAULT_CONFIG = get_config_data(os.path.join(BASE_DIR, 'defaults.yml')) + +USER_CONFIG = get_config_data(os.path.expanduser(USER_CONFIG_DIR)) + +def get_config(key): + if key in USER_CONFIG: + return USER_CONFIG[key] + elif key in DEFAULT_CONFIG: + return DEFAULT_CONFIG[key] + return None