1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
dotfile-automator/project/utils/config.py

33 lines
723 B
Python
Raw Normal View History

2015-12-02 22:07:56 +00:00
import os
2015-12-02 22:08:30 +00:00
from yaml import load
2015-12-02 22:07:56 +00:00
try:
2015-12-02 22:08:30 +00:00
from yaml import CLoader as Loader
2015-12-02 22:07:56 +00:00
except ImportError:
2015-12-02 22:08:30 +00:00
from yaml import Loader
2015-12-02 22:07:56 +00:00
2015-12-02 22:39:40 +00:00
2015-12-02 22:07:56 +00:00
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
USER_CONFIG_DIR = "~/.dfa.yml"
2015-12-02 22:39:40 +00:00
2015-12-02 22:07:56 +00:00
def get_config_data(filename):
try:
config_file = open(filename)
return load(config_file, Loader=Loader)
except FileNotFoundError:
2015-12-02 22:37:36 +00:00
return []
2015-12-02 22:07:56 +00:00
2015-12-03 18:07:08 +00:00
DEFAULT_CONFIG = get_config_data(os.path.join(BASE_DIR, 'defaults.yml'))
2015-12-02 22:07:56 +00:00
USER_CONFIG = get_config_data(os.path.expanduser(USER_CONFIG_DIR))
2015-12-02 22:39:40 +00:00
2015-12-03 18:07:08 +00:00
def get(key):
2015-12-02 22:37:36 +00:00
if USER_CONFIG and key in USER_CONFIG:
2015-12-02 22:07:56 +00:00
return USER_CONFIG[key]
2015-12-02 22:37:36 +00:00
elif DEFAULT_CONFIG and key in DEFAULT_CONFIG:
2015-12-02 22:07:56 +00:00
return DEFAULT_CONFIG[key]
return None