From 29fd0be6b9d79af8d3d87c1001a220e0441833ed Mon Sep 17 00:00:00 2001 From: Orakaro Date: Sat, 19 Jul 2014 14:23:07 +0900 Subject: [PATCH] documenting config --- docs/index.rst | 11 ++++++ rainbowstream/config.py | 83 ++++++++++++++++++++++++++++++++++++++++ rainbowstream/rainbow.py | 80 ++++++++++++++++++++++++++++---------- 3 files changed, 153 insertions(+), 21 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 109799f..b36f201 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -257,6 +257,17 @@ Theme’s screenshot: For detaile information, see `theme usage and customization`_. +Config explanation +^^^^^^^^^^^^^^^^^^ + +- ``THEME`` theme. + +- ``ASCII_ART`` Boolen value, decide to diplay your twitter name by ascii art at stream begin or not. + +- ``SEARCH_MAX_RECORD`` : max tweets can display on a 'search' command. + +- ``HOME_TWEET_NUM`` : default tweets to display on a 'home' command. + Development ----------- diff --git a/rainbowstream/config.py b/rainbowstream/config.py index ff2d17d..d481dcd 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -10,6 +10,17 @@ comment_re = re.compile( re.DOTALL | re.MULTILINE ) +def fixup(adict, k, v): + """ + Fix up a key in json format + """ + for key in adict.keys(): + if key == k: + adict[key] = v + elif type(adict[key]) is dict: + fixup(adict[key], k, v) + + def load_config(filepath): """ Load config from filepath @@ -22,6 +33,78 @@ def load_config(filepath): match = comment_re.search(content) return json.loads(content, object_pairs_hook=OrderedDict) + +def get_all_config(): + """ + Get all config + """ + path = os.environ.get( + 'HOME', + os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' + return load_config(path) + + +def get_default_config(key): + """ + Get default value of a config key + """ + path = os.path.dirname( + __file__) + '/colorset/config' + data = load_config(path) + return data[key] + + +def get_config(key): + """ + Get current value of a config key + """ + return c[key] + + +def set_config(key,value): + """ + Set a config key with specific value + """ + # Modify value + if value.isdigit(): + value = int(value) + if value.lower() == 'True': + value = True + elif value.lower() == 'False': + value = False + # Fix up + path = os.environ.get( + 'HOME', + os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' + data = load_config(path) + fixup(data, key, value) + # Save + with open(path, 'w') as out: + json.dump(data, out, indent = 4) + os.system('chmod 777 ' + path) + + +def reload_config(): + """ + Reload config + """ + rainbow_config = os.environ.get( + 'HOME', + os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' + try: + data = load_config(rainbow_config) + for d in data: + c[d] = data[d] + except: + print('It seems that ~/.rainbow_config.json has wrong format :(') + + # Config dictionary c = {} diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 308b76e..d4c1254 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -64,6 +64,7 @@ cmdset = [ 'report', 'list', 'cal', + 'config', 'theme', 'h', 'c', @@ -1079,6 +1080,44 @@ def cal(): show_calendar(month, date, rel) +def config(): + """ + Browse and change config + """ + all_config = get_all_config() + g['stuff'] = g['stuff'].strip() + # List all config + if not g['stuff']: + for k in all_config: + line = ' '*2 + light_green(k) + ': ' + light_yellow(str(all_config[k])) + printNicely(line) + guide = 'Detailed explanation can be found at ' + \ + color_func(c['TWEET']['link'])('http://rainbowstream.readthedocs.org/en/latest/') + printNicely(guide) + # Print specific config + elif len(g['stuff'].split()) == 1: + if g['stuff'] in all_config: + k = g['stuff'] + line = ' '*2 + light_green(k) + ': ' + light_yellow(str(all_config[k])) + printNicely(line) + else: + printNicely(red('No config key like this.')) + # Print specific config's default value + elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'default': + key = g['stuff'].split()[0] + value = get_default_config(key) + line = ' '*2 + light_green(key) + ': ' + light_magenta(value) + printNicely(line) + # Set specific config + elif len(g['stuff'].split()) == 3 and g['stuff'].split()[1] == '=' : + key = g['stuff'].split()[0] + value = g['stuff'].split()[-1] + set_config(key,value) + reload_config() + else: + printNicely(light_magenta('Sorry I can\'s understand.')) + + def theme(): """ List and change theme @@ -1094,24 +1133,7 @@ def theme(): printNicely(line) elif g['stuff'] == 'current_as_default': # Set as default - def fixup(adict, k, v): - for key in adict.keys(): - if key == k: - adict[key] = v - elif type(adict[key]) is dict: - fixup(adict[key], k, v) - # Modify - path = os.environ.get( - 'HOME', - os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - data = load_config(rainbow_config) - fixup(data, 'THEME', c['THEME']) - # Save - with open(path, 'w') as out: - json.dump(data, out, indent = 4) - os.system('chmod 777 ' + path) + set_config('THEME',c['THEME']) printNicely(light_green('Okay it will be applied from next time :)')) else: # Change theme @@ -1358,12 +1380,26 @@ def help(): usage += s * 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \ ' for current month.\n' - # Screening + # Config usage += '\n' - usage += s + grey(u'\u266A' + ' Screening \n') - usage += s * 2 + light_green('theme') + ' will list available theme.' + \ + usage += s + grey(u'\u266A' + ' Config \n') + usage += s * 2 + light_green('theme') + ' will list available theme. ' + \ light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \ ' theme immediately.\n' + usage += s * 2 + light_green('config') + ' will list all config.\n' + usage += s * 3 + \ + light_green('config ASCII_ART') + ' will output current value of ' +\ + light_yellow('ASCII_ART') +' config key.\n' + usage += s * 3 + \ + light_green('config ASCII_ART default') + ' will output default value of ' + \ + light_yellow('ASCII_ART') +' config key.\n' + usage += s * 3 + \ + light_green('config ASCII_ART = False') + ' will set value of ' + \ + light_yellow('ASCII_ART') +' config key to ' + light_yellow('False') + '.\n' + + # Screening + usage += '\n' + usage += s + grey(u'\u266A' + ' Screening \n') usage += s * 2 + light_green('h') + ' will show this help again.\n' usage += s * 2 + light_green('c') + ' will clear the screen.\n' usage += s * 2 + light_green('q') + ' will quit.\n' @@ -1457,6 +1493,7 @@ def process(cmd): report, list, cal, + config, theme, help, clear, @@ -1516,6 +1553,7 @@ def listen(): 'del' ], # list [], # cal + [key for key in dict(get_all_config())], # config g['themes'] + ['current_as_default'], # theme [ 'discover', -- 2.25.1