From ceec85939c25fe0a6b9d586b3066be4878f2379c Mon Sep 17 00:00:00 2001 From: vunhat_minh Date: Fri, 25 Jul 2014 18:16:56 +0900 Subject: [PATCH] configurable prefix + change THEME in config --- rainbowstream/colorset/config | 2 ++ rainbowstream/config.py | 41 +++++++++++++++----------- rainbowstream/draw.py | 47 ++++++++++++++++++------------ rainbowstream/rainbow.py | 54 +++++++++++++++++------------------ setup.py | 2 +- 5 files changed, 82 insertions(+), 64 deletions(-) diff --git a/rainbowstream/colorset/config b/rainbowstream/colorset/config index 164411a..168dad9 100644 --- a/rainbowstream/colorset/config +++ b/rainbowstream/colorset/config @@ -5,6 +5,8 @@ "THEME" : "monokai", // Ascii Art "ASCII_ART" : true, + // Prefix + "PREFIX" : "", // 'search': max search record "SEARCH_MAX_RECORD" : 5, // 'home': default number of home's tweets diff --git a/rainbowstream/config.py b/rainbowstream/config.py index dcef9f7..1245ca4 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -63,9 +63,10 @@ def get_default_config(key): try: data = load_config(path) except: - raise Exception('No such config key.') + raise Exception('No such config key.') return data[key] + def get_config(key): """ Get current value of a config key @@ -91,11 +92,13 @@ def set_config(key, value): 'USERPROFILE', '')) + os.sep + '.rainbow_config.json' data = load_config(path) + # Update global config + c[key] = value + # Update config file if key in data: fixup(data, key, value) else: data[key] = value - c[key] = value # Save with open(path, 'w') as out: json.dump(data, out, indent=4) @@ -103,25 +106,29 @@ def set_config(key, value): def delete_config(key): - """ - Delete a config key - """ - path = os.environ.get( + """ + Delete a config key + """ + path = os.environ.get( 'HOME', os.environ.get( 'USERPROFILE', '')) + os.sep + '.rainbow_config.json' - data = load_config(path) - # Drop key - if key in data and key in c: - data.pop(key) - c.pop(key) - else: - raise Exception('No such config key.') - # Save - with open(path, 'w') as out: - json.dump(data, out, indent=4) - os.system('chmod 777 ' + path) + data = load_config(path) + # Drop key + if key in data and key in c: + data.pop(key) + c.pop(key) + try: + data[key] = c[key] = get_default_config(key) + except: + pass + else: + raise Exception('No such config key.') + # Save + with open(path, 'w') as out: + json.dump(data, out, indent=4) + os.system('chmod 777 ' + path) def reload_config(): diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 29240de..ae60a72 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -112,36 +112,47 @@ def show_calendar(month, date, rel): def check_config(): - """ - Check if config is changed - """ - changed = False - data = get_all_config() - for key in c: - if key in data: - if data[key] != c[key]: - changed = True - if changed: - reload_config() + """ + Check if config is changed + """ + changed = False + data = get_all_config() + for key in c: + if key in data: + if data[key] != c[key]: + changed = True + if changed: + reload_config() + + +def validate_theme(theme): + """ + Validate a theme exists or not + """ + # Theme changed check + files = os.listdir(os.path.dirname(__file__) + '/colorset') + themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] + return theme in themes -def check_theme(): +def reload_theme(current_config): """ Check current theme and update if necessary """ exists = db.theme_query() themes = [t.theme_name for t in exists] - if c['THEME'] != themes[0]: - c['THEME'] = themes[0] + if current_config != themes[0]: config = os.path.dirname( - __file__) + '/colorset/' + c['THEME'] + '.json' + __file__) + '/colorset/' + current_config + '.json' # Load new config data = load_config(config) if data: for d in data: c[d] = data[d] - # Re-init color cycle - g['cyc'] = init_cycle() + # Restart color cycle and update db/config + start_cycle() + db.theme_update(current_config) + set_config('THEME', current_config) def color_func(func_name): @@ -158,8 +169,8 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]): Draw the rainbow """ - check_theme() check_config() + reload_theme(c['THEME']) # Retrieve tweet tid = t['id'] text = t['text'] diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 08fee9e..08f240d 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -144,8 +144,12 @@ def init(args): # Get name t = Twitter(auth=authen()) name = '@' + t.account.verify_credentials()['screen_name'] + if not get_config('PREFIX'): + set_config('PREFIX', name) g['original_name'] = name[1:] - g['decorated_name'] = color_func(c['DECORATED_NAME'])('[' + name + ']: ') + g['decorated_name'] = lambda x: color_func( + c['DECORATED_NAME'])( + '[' + x + ']: ') # Theme init files = os.listdir(os.path.dirname(__file__) + '/colorset') themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] @@ -156,6 +160,7 @@ def init(args): # Image on term c['IMAGE_ON_TERM'] = args.image_on_term + def switch(): """ Switch stream @@ -1113,7 +1118,8 @@ def config(): line = ' ' * 2 + light_green(key) + ': ' + light_magenta(value) printNicely(line) except: - printNicely(light_magenta('This config key does not exist in default.')) + printNicely( + light_magenta('This config key does not exist in default.')) # Delete specific config key in config file elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'drop': key = g['stuff'].split()[0] @@ -1126,8 +1132,17 @@ def config(): elif len(g['stuff'].split()) == 3 and g['stuff'].split()[1] == '=': key = g['stuff'].split()[0] value = g['stuff'].split()[-1] + if key == 'THEME' and not validate_theme(value): + printNicely(red('Invalid theme\'s value.')) + return try: set_config(key, value) + # Apply theme immediately + if key == 'THEME': + reload_theme(value) + g['decorated_name'] = lambda x: color_func( + c['DECORATED_NAME'])( + '[' + x + ']: ') printNicely(light_green('Updated successfully.')) except: printNicely(light_magenta('Not valid value.')) @@ -1150,33 +1165,15 @@ def theme(): else: line = ' ' * 4 + line printNicely(line) - elif g['stuff'] == 'current_as_default': - # Set as default - set_config('THEME', c['THEME']) - printNicely(light_green('Okay it will be applied from next time :)')) else: # Change theme try: - # Load new config - if g['stuff'] != 'custom': - new_config = os.path.dirname( - __file__) + '/colorset/' + g['stuff'] + '.json' - else: - new_config = os.environ.get( - 'HOME', os.environ.get( - 'USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - new_config = load_config(new_config) - if new_config: - for nc in new_config: - c[nc] = new_config[nc] - # Update db and reset colors - db.theme_update(g['stuff']) - c['THEME'] = g['stuff'] - start_cycle() - g['decorated_name'] = color_func( + # Load new theme + reload_theme(g['stuff']) + # Redefine decorated_name + g['decorated_name'] = lambda x: color_func( c['DECORATED_NAME'])( - '[@' + g['original_name'] + ']: ') + '[' + x + ']: ') printNicely(green('Theme changed.')) except: printNicely(red('No such theme exists.')) @@ -1570,7 +1567,7 @@ def listen(): ], # list [], # cal [key for key in dict(get_all_config())], # config - g['themes'] + ['current_as_default'], # theme + g['themes'], # theme [ 'discover', 'tweets', @@ -1588,7 +1585,7 @@ def listen(): reset() while True: if g['prefix']: - line = raw_input(g['decorated_name']) + line = raw_input(g['decorated_name'](c['PREFIX'])) else: line = raw_input() try: @@ -1610,7 +1607,8 @@ def listen(): g['prefix'] = True # Release the semaphore lock db.semaphore_update(False) - except Exception: + except Exception as e: + print e printNicely(red('OMG something is wrong with Twitter right now.')) diff --git a/setup.py b/setup.py index 593dc29..aa4ead6 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages import os, sys # Bumped version -version = '0.4.5' +version = '0.4.6' # Require install_requires = [ -- 2.25.1