X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;ds=sidebyside;f=rainbowstream%2Fconfig.py;h=f97b2099740da2a5dc22ee26e2cf50254509dbed;hb=844de15119c7cfd8e051dd6cadfd4522669f25f6;hp=41312952fe8419b489df5f3be84cfef133e8650e;hpb=63388de74a32694faa66df05b93fcbcc72833c48;p=rainbowstream.git diff --git a/rainbowstream/config.py b/rainbowstream/config.py index 4131295..f97b209 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -6,13 +6,21 @@ from collections import OrderedDict # Regular expression for comments in config file comment_re = re.compile( - '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', + '(^)[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', re.DOTALL | re.MULTILINE ) # Config dictionary c = {} +def user_filepath(): + """ + Determine the user's config file location + """ + if 'RAINBOW_CONFIG' in os.environ: + return os.environ['RAINBOW_CONFIG'] + + return os.path.expanduser("~") + os.sep + '.rainbow_config.json' def fixup(adict, k, v): """ @@ -42,29 +50,30 @@ def get_all_config(): """ Get all config """ - path = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - data = load_config(path) - # Hard to set from prompt - data.pop('ONLY_LIST', None) - data.pop('IGNORE_LIST', None) - return data + try: + path = user_filepath() + data = load_config(path) + # Hard to set from prompt + data.pop('ONLY_LIST', None) + data.pop('IGNORE_LIST', None) + data.pop('FORMAT', None) + data.pop('QUOTE_FORMAT', None) + data.pop('NOTIFY_FORMAT', None) + return data + except: + return [] def get_default_config(key): """ Get default value of a config key """ - path = os.path.dirname( - __file__) + '/colorset/config' try: + path = os.path.dirname(__file__) + '/colorset/config' data = load_config(path) + return data[key] except: - raise Exception('No such config key.') - return data[key] + raise Exception('This config key does not exist in default.') def get_config(key): @@ -85,19 +94,15 @@ def set_config(key, value): value = True elif value.lower() == 'false': value = False - # Fix up - path = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' + # Update global config + c[key] = value + # Load current user config + path = user_filepath() data = {} try: data = load_config(path) except: - pass - # Update global config - c[key] = value + return # Update config file if key in data: fixup(data, key, value) @@ -113,12 +118,11 @@ def delete_config(key): """ Delete a config key """ - path = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - data = load_config(path) + path = user_filepath() + try: + data = load_config(path) + except: + raise Exception('Config file is messed up.') # Drop key if key in data and key in c: data.pop(key) @@ -139,17 +143,13 @@ def reload_config(): """ Reload config """ - rainbow_config = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' try: + rainbow_config = user_filepath() data = load_config(rainbow_config) for d in data: c[d] = data[d] except: - print('It seems that ~/.rainbow_config.json has wrong format :(') + raise Exception('Can not reload config file with wrong format.') def init_config(): @@ -157,8 +157,8 @@ def init_config(): Init configuration """ # Load the initial config - config = os.path.dirname( - __file__) + '/colorset/config' + config = os.path.dirname(__file__) + \ + '/colorset/config' try: data = load_config(config) for d in data: @@ -166,20 +166,16 @@ def init_config(): except: pass # Load user's config - rainbow_config = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' + rainbow_config = user_filepath() try: data = load_config(rainbow_config) for d in data: c[d] = data[d] - except: - pass + except (IOError, ValueError) as e: + c['USER_JSON_ERROR'] = str(e) # Load default theme - theme_file = os.path.dirname( - __file__) + '/colorset/' + c['THEME'] + '.json' + theme_file = os.path.dirname(__file__) + \ + '/colorset/' + c['THEME'] + '.json' try: data = load_config(theme_file) for d in data: