Read env vars for global and local config path

- allows to change these paths for testing purposes and also
gives user option to change his config paths
This commit is contained in:
Tomas Korbar 2018-08-21 15:40:47 +02:00
parent 7814de96d2
commit 879a58b721
1 changed files with 24 additions and 10 deletions

View File

@ -2,9 +2,13 @@ import os
from cheat.utils import Utils from cheat.utils import Utils
import json import json
class Configuration: class Configuration:
def __init__(self): def __init__(self):
self._get_global_conf_file_path()
self._get_local_conf_file_path()
self._saved_configuration = self._get_configuration() self._saved_configuration = self._get_configuration()
@ -13,14 +17,14 @@ class Configuration:
merged_config = {} merged_config = {}
try: try:
merged_config.update(self._read_configuration_file('/etc/cheat')) merged_config.update(self._read_configuration_file(self.glob_config_path))
except Exception: except Exception as e:
Utils.warn('error while parsing global configuration') Utils.warn('error while parsing global configuration Reason: ' + e.message)
try: try:
merged_config.update(self._read_configuration_file(os.path.expanduser(os.path.join('~','.config','cheat','cheat')))) merged_config.update(self._read_configuration_file(self.local_config_path))
except Exception: except Exception as e:
Utils.warn('error while parsing user configuration') Utils.warn('error while parsing user configuration Reason: ' + e.message)
merged_config.update(self._read_env_vars_config()) merged_config.update(self._read_env_vars_config())
@ -60,6 +64,16 @@ class Configuration:
current_config[key] = os.environ.get(key) current_config[key] = os.environ.get(key)
def _get_global_conf_file_path(self):
self.glob_config_path = os.environ.get('CHEAT_GLOBAL_CONF_PATH') \
or '/etc/cheat'
def _get_local_conf_file_path(self):
self.local_config_path = os.environ.get('CHEAT_LOCAL_CONF_PATH') \
or os.path.expanduser('~/.config/cheat/cheat')
def get_default_cheat_dir(self): def get_default_cheat_dir(self):
return self._saved_configuration.get('DEFAULT_CHEAT_DIR') return self._saved_configuration.get('DEFAULT_CHEAT_DIR')