2015-12-02 22:07:56 +00:00
|
|
|
import os
|
2015-12-03 22:33:04 +00:00
|
|
|
from yaml import load, dump
|
2015-12-05 22:14:54 +00:00
|
|
|
from . import constants
|
2015-12-02 22:07:56 +00:00
|
|
|
|
|
|
|
try:
|
2015-12-03 22:33:04 +00:00
|
|
|
from yaml import CLoader as Loader, CDumper as Dumper
|
2015-12-02 22:07:56 +00:00
|
|
|
except ImportError:
|
2015-12-03 22:33:04 +00:00
|
|
|
from yaml import Loader, Dumper
|
2015-12-02 22:07:56 +00:00
|
|
|
|
2015-12-02 22:39:40 +00:00
|
|
|
|
2015-12-02 22:07:56 +00:00
|
|
|
def get_config_data(filename):
|
|
|
|
try:
|
2015-12-03 22:33:04 +00:00
|
|
|
with open(filename) as config_file:
|
2015-12-06 14:20:55 +00:00
|
|
|
return load(config_file, Loader=Loader) or {}
|
2015-12-02 22:07:56 +00:00
|
|
|
except FileNotFoundError:
|
2015-12-03 22:33:04 +00:00
|
|
|
return {}
|
2015-12-04 08:22:53 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Error:", e)
|
|
|
|
return {}
|
2015-12-02 22:07:56 +00:00
|
|
|
|
2015-12-03 22:43:42 +00:00
|
|
|
|
2015-12-06 14:20:55 +00:00
|
|
|
def write_user_config():
|
2015-12-03 22:33:04 +00:00
|
|
|
try:
|
2015-12-05 22:14:54 +00:00
|
|
|
with open(constants.USER_CONFIG_DIR, 'w') as config_file:
|
2015-12-03 22:43:42 +00:00
|
|
|
dump(USER_CONFIG, config_file, indent=4, default_flow_style=False, Dumper=Dumper)
|
2015-12-03 22:33:04 +00:00
|
|
|
return "SUCCESS"
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
2015-12-02 22:07:56 +00:00
|
|
|
|
2015-12-03 22:43:42 +00:00
|
|
|
|
2015-12-06 14:20:55 +00:00
|
|
|
# Create the config file if it doesnt exist already
|
|
|
|
os.makedirs(constants.DATA_DIR, mode=0o755, exist_ok=True)
|
|
|
|
open(constants.USER_CONFIG_DIR, 'a').close()
|
|
|
|
|
|
|
|
|
2015-12-05 22:14:54 +00:00
|
|
|
DEFAULT_CONFIG = get_config_data(constants.DEFAULT_CONFIG_DIR)
|
2015-12-06 14:20:55 +00:00
|
|
|
USER_CONFIG = get_config_data(constants.USER_CONFIG_DIR)
|
2015-12-02 22:07:56 +00:00
|
|
|
|
2015-12-02 22:39:40 +00:00
|
|
|
|
2015-12-03 18:07:08 +00:00
|
|
|
def get(key):
|
2015-12-06 14:20:55 +00:00
|
|
|
if key in USER_CONFIG:
|
2015-12-02 22:07:56 +00:00
|
|
|
return USER_CONFIG[key]
|
2015-12-06 14:20:55 +00:00
|
|
|
elif key in DEFAULT_CONFIG:
|
2015-12-02 22:07:56 +00:00
|
|
|
return DEFAULT_CONFIG[key]
|
|
|
|
return None
|
2015-12-03 22:33:04 +00:00
|
|
|
|
2015-12-03 22:43:42 +00:00
|
|
|
|
2015-12-06 14:20:55 +00:00
|
|
|
def set(key, value):
|
2015-12-03 22:33:04 +00:00
|
|
|
if key not in DEFAULT_CONFIG:
|
|
|
|
return "NO_KEY"
|
|
|
|
USER_CONFIG[key] = value
|
2015-12-06 14:20:55 +00:00
|
|
|
return write_user_config()
|
2015-12-04 08:48:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def has_basics():
|
2015-12-05 22:14:54 +00:00
|
|
|
for key in constants.REQUIRED_KEYS:
|
2015-12-04 08:48:36 +00:00
|
|
|
if not get(key):
|
|
|
|
return False
|
|
|
|
return True
|