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
'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)
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():
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):
Draw the rainbow
"""
- check_theme()
check_config()
+ reload_theme(c['THEME'])
# Retrieve tweet
tid = t['id']
text = t['text']
# 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']
# Image on term
c['IMAGE_ON_TERM'] = args.image_on_term
+
def switch():
"""
Switch stream
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]
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.'))
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.'))
], # list
[], # cal
[key for key in dict(get_all_config())], # config
- g['themes'] + ['current_as_default'], # theme
+ g['themes'], # theme
[
'discover',
'tweets',
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:
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.'))