From 1f2f615927934706083c9571cd122363a6080956 Mon Sep 17 00:00:00 2001 From: Orakaro Date: Tue, 15 Jul 2014 23:49:28 +0900 Subject: [PATCH] generate config file by default --- rainbowstream/colorset/config | 28 +++++++++++++++ rainbowstream/colorset/default.json | 27 --------------- rainbowstream/config.py | 41 ++++++++-------------- rainbowstream/draw.py | 15 +++----- rainbowstream/rainbow.py | 54 +++++++++++++---------------- setup.py | 20 ++++++++--- 6 files changed, 87 insertions(+), 98 deletions(-) create mode 100644 rainbowstream/colorset/config diff --git a/rainbowstream/colorset/config b/rainbowstream/colorset/config new file mode 100644 index 0000000..21ff30f --- /dev/null +++ b/rainbowstream/colorset/config @@ -0,0 +1,28 @@ +{ + // Themes + "THEME" : "monokai", + // 'search': max search record + "SEARCH_MAX_RECORD" : 5, + // 'home': default number of home's tweets + "HOME_TWEET_NUM" : 5, + // 'allrt': default number of retweets + "RETWEETS_SHOW_NUM" : 5, + // 'inbox','sent': default number of direct message + "MESSAGES_DISPLAY" : 5, + // 'trend': max trending topics + "TREND_MAX" : 10, + // List home timeline max + "LIST_MAX" : 5, + // 'switch': Filter and Ignore list ex: ['@fat','@mdo'] + "ONLY_LIST" : [], + "IGNORE_LIST" : [], + // Autocomplete history file name + "HISTORY_FILENAME" : "completer.hist", + // Image config + "IMAGE_SHIFT" : 10, + "IMAGE_MAX_HEIGHT" : 40, + // Stream config + "USER_DOMAIN" : "userstream.twitter.com", + "PUBLIC_DOMAIN" : "stream.twitter.com", + "SITE_DOMAIN" : "sitestream.twitter.com" +} diff --git a/rainbowstream/colorset/default.json b/rainbowstream/colorset/default.json index f2b23a4..134891d 100644 --- a/rainbowstream/colorset/default.json +++ b/rainbowstream/colorset/default.json @@ -1,29 +1,4 @@ { - // 'search': max search record - "SEARCH_MAX_RECORD" : 5, - // 'home': default number of home's tweets - "HOME_TWEET_NUM" : 5, - // 'allrt': default number of retweets - "RETWEETS_SHOW_NUM" : 5, - // 'inbox','sent': default number of direct message - "MESSAGES_DISPLAY" : 5, - // 'trend': max trending topics - "TREND_MAX" : 10, - // List home timeline max - "LIST_MAX" : 5, - // 'switch': Filter and Ignore list ex: ['@fat','@mdo'] - "ONLY_LIST" : [], - "IGNORE_LIST" : [], - // Autocomplete history file name - "HISTORY_FILENAME" : "completer.hist", - // Image config - "IMAGE_SHIFT" : 10, - "IMAGE_MAX_HEIGHT" : 40, - // Stream config - "USER_DOMAIN" : "userstream.twitter.com", - "PUBLIC_DOMAIN" : "stream.twitter.com", - "SITE_DOMAIN" : "sitestream.twitter.com", - /* Color config There are 16 basic colors supported : * default @@ -43,8 +18,6 @@ * light_cyan * white and 256 colors from term_0 to term_255 - Color code can be reference at - http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html */ "DECORATED_NAME" : "grey", diff --git a/rainbowstream/config.py b/rainbowstream/config.py index b17a0e2..de15655 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -2,6 +2,7 @@ import json import re import os import os.path +from collections import OrderedDict # Regular expression for comments comment_re = re.compile( @@ -9,7 +10,6 @@ comment_re = re.compile( re.DOTALL | re.MULTILINE ) - def load_config(filepath): """ Load config from filepath @@ -20,34 +20,12 @@ def load_config(filepath): while match: content = content[:match.start()] + content[match.end():] match = comment_re.search(content) - return json.loads(content) + return json.loads(content, object_pairs_hook=OrderedDict) # Config dictionary c = {} -# Load default -default_config = os.path.dirname(__file__) + '/colorset/default.json' -data = load_config(default_config) -for d in data: - c[d] = data[d] -c['theme'] = 'default' -# Load init if exist -try: - path = os.path.dirname(__file__) + '/colorset/init' - f = open(path) - lines = f.readlines() - if len(lines) > 1: - raise Exception('More than 1 default theme') - theme_name = lines[0].strip() - default_config = os.path.dirname( - __file__) + '/colorset/' + theme_name + '.json' - data = load_config(default_config) - for d in data: - c[d] = data[d] - c['theme'] = theme_name - f.close() -except: - pass -# Load user's colorset + +# Load user's config rainbow_config = os.environ.get( 'HOME', os.environ.get( @@ -57,6 +35,15 @@ try: data = load_config(rainbow_config) for d in data: c[d] = data[d] - c['theme'] = 'custom' +except: + pass + +# Load default theme +theme_file = os.path.dirname( + __file__) + '/colorset/' + c['THEME'] + '.json' +try: + data = load_config(theme_file) + for d in data: + c[d] = data[d] except: pass diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 7d4de2e..a24ce03 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -118,17 +118,10 @@ def check_theme(): """ exists = db.theme_query() themes = [t.theme_name for t in exists] - if c['theme'] != themes[0]: - c['theme'] = themes[0] - # Determine path - if c['theme'] == 'custom': - config = os.environ.get( - 'HOME', - os.environ.get('USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - else: - config = os.path.dirname( - __file__) + '/colorset/' + c['theme'] + '.json' + if c['THEME'] != themes[0]: + c['THEME'] = themes[0] + config = os.path.dirname( + __file__) + '/colorset/' + c['THEME'] + '.json' # Load new config data = load_config(config) if data: diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 8072114..5ce703a 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -11,6 +11,7 @@ import argparse import time import requests import webbrowser +import json from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup from twitter.api import * @@ -144,9 +145,8 @@ def get_decorated_name(): files = os.listdir(os.path.dirname(__file__) + '/colorset') themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] - themes += ['custom'] g['themes'] = themes - db.theme_store(c['theme']) + db.theme_store(c['THEME']) def switch(): @@ -1131,32 +1131,31 @@ def theme(): if not g['stuff']: # List themes for theme in g['themes']: - line = '' - # Detect custom config - if theme == 'custom': - line += light_magenta('custom') - custom_path = os.environ.get( - 'HOME', - os.environ.get('USERPROFILE', - '')) + os.sep + '.rainbow_config.json' - if not os.path.exists(custom_path): - line += light_magenta( - ' (create your own config file at ~/.rainbow_config.json)') - else: - line += light_magenta(' (loaded)') - else: - line += light_magenta(theme) - if c['theme'] == theme: + line = light_magenta(theme) + if c['THEME'] == theme: line = ' ' * 2 + light_yellow('* ') + line else: line = ' ' * 4 + line printNicely(line) elif g['stuff'] == 'current_as_default': - # Set default - path = os.path.dirname(__file__) + '/colorset/init' - f = open(path, 'w') - f.write(c['theme']) - f.close() + # 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) printNicely(light_green('Okay it will be applied from next time :)')) else: @@ -1177,17 +1176,14 @@ def theme(): c[nc] = new_config[nc] # Update db and reset colors db.theme_update(g['stuff']) - c['theme'] = g['stuff'] + c['THEME'] = g['stuff'] reset_cycle() g['decorated_name'] = color_func( c['DECORATED_NAME'])( '[@' + g['original_name'] + ']: ') printNicely(green('Theme changed.')) except: - if g['stuff'] == 'custom': - printNicely(red('~/.rainbow_config.json is not exists!')) - else: - printNicely(red('No such theme exists.')) + printNicely(red('No such theme exists.')) def help_discover(): @@ -1612,7 +1608,7 @@ def stream(domain, args, name='Rainbow Stream'): art_dict = { c['USER_DOMAIN']: name, c['PUBLIC_DOMAIN']: args.track_keywords, - c['SITE_DOMAIN']: 'Site Stream', + c['SITE_DOMAIN']: name, } if g['ascii_art']: ascii_art(art_dict[domain]) diff --git a/setup.py b/setup.py index 294fd86..0d99cd7 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,10 @@ from setuptools import setup, find_packages -import sys +import os, sys -version = '0.3.4' +# Bumped version +version = '0.3.5' +# Require install_requires = [ "python-dateutil", "requests", @@ -11,9 +13,21 @@ install_requires = [ "twitter", "Pillow", ] +# Python 3 doesn't hava pysqlite if sys.version[0] == "2": install_requires += ["pysqlite"] +# Copy default config if not exists +default = os.environ.get( + 'HOME', + os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' +if not os.path.isfile(default): + cmd = 'cp rainbowstream/colorset/config ' + default + os.system(cmd) + +# Setup setup(name='rainbowstream', version=version, description="A smart and nice Twitter client on terminal.", @@ -25,8 +39,6 @@ setup(name='rainbowstream', "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3.2", - "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries", "Topic :: Utilities", -- 2.25.1