From 4cf86720f0c6834b4af48c577685237f59447937 Mon Sep 17 00:00:00 2001 From: Vu Nhat Minh Date: Thu, 3 Jul 2014 13:31:36 +0900 Subject: [PATCH] theme on DB --- rainbowstream/config.py | 8 +++++++- rainbowstream/db.py | 35 +++++++++++++++++++++++++++++++++-- rainbowstream/draw.py | 24 ++++++++++++++++++++++++ rainbowstream/rainbow.py | 20 +++++++++++++++----- rainbowstream/table_def.py | 13 +++++++++++++ 5 files changed, 92 insertions(+), 8 deletions(-) diff --git a/rainbowstream/config.py b/rainbowstream/config.py index 418afbb..4b4aef6 100644 --- a/rainbowstream/config.py +++ b/rainbowstream/config.py @@ -3,6 +3,10 @@ import re import os import os.path +from .db import * + +db = RainbowDB() + # Regular expression for comments comment_re = re.compile( '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', @@ -21,8 +25,10 @@ def load_config(filepath): while match: content = content[:match.start()] + content[match.end():] match = comment_re.search(content) + db.theme_store('user') return json.loads(content) - except: + except IOError: + db.theme_store('default') pass # Load default colorset diff --git a/rainbowstream/db.py b/rainbowstream/db.py index fabc3a4..775c856 100644 --- a/rainbowstream/db.py +++ b/rainbowstream/db.py @@ -1,5 +1,6 @@ import os from sqlalchemy import create_engine +from sqlalchemy import update from sqlalchemy.orm import sessionmaker from .table_def import * @@ -19,8 +20,8 @@ class RainbowDB(): """ Session = sessionmaker(bind=self.engine) session = Session() - m = Tweet(tweet_id) - session.add(m) + t = Tweet(tweet_id) + session.add(t) session.commit() def rainbow_to_tweet_query(self, rid): @@ -68,3 +69,33 @@ class RainbowDB(): session = Session() res = session.query(Message).filter_by(message_id=mid).all() return res + + def theme_store(self, theme_name): + """ + Store theme + """ + Session = sessionmaker(bind=self.engine) + session = Session() + th = Theme(theme_name) + session.add(th) + session.commit() + + def theme_update(self, theme_name): + """ + Update theme + """ + Session = sessionmaker(bind=self.engine) + session = Session() + res = session.query(Theme).all() + for r in res: + r.theme_name = theme_name + session.commit() + + def theme_query(self): + """ + Query theme + """ + Session = sessionmaker(bind=self.engine) + session = Session() + res = session.query(Theme).all() + return res diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 9a8fbdd..5063df4 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -14,6 +14,29 @@ from .config import * from .db import * db = RainbowDB() +cur_theme = None + +def check_theme(): + """ + Check current theme and update if necessary + """ + exists = db.theme_query() + themes = [t.theme_name for t in exists] + if cur_theme != themes[0]: + cur_theme = themes[0] + # Determine path + if cur_theme == 'user': + config = os.environ.get( + 'HOME', + os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' + else: + config = 'rainbowstream/colorset/'+cur_theme+'.json' + # Load new config + data = load_config(config) + for d in data: + c[d] = data[d] def color_func(func_name): @@ -31,6 +54,7 @@ def draw(t, iot=False, keyword=None, fil=[], ig=[]): Draw the rainbow """ + check_theme() # Retrieve tweet tid = t['id'] text = t['text'] diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index ce5d998..00e01eb 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -139,6 +139,7 @@ def get_decorated_name(): files = os.listdir('rainbowstream/colorset') themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] + themes += ['user'] g['themes'] = themes @@ -768,8 +769,19 @@ def theme(): """ if not g['stuff']: # List themes + line = ' ' * 2 + light_yellow('* ') for theme in g['themes']: - line = ' ' * 2 + light_yellow('* ' + theme) + # Detect custom config + if theme == 'user': + line += light_magenta('custom') + exists = db.theme_query() + themes = [t.theme_name for t in exists] + if themes[0] == 'user': + line += light_magenta(' (applied)') + else: + line += light_magenta(' (not specified)') + else: + line += light_magenta(theme) printNicely(line) else: # Change theme @@ -779,10 +791,8 @@ def theme(): new_config = load_config(new_config) for nc in new_config: c[nc] = new_config[nc] - # Reset stream - g['stuff'] = 'mine' - g['ascii_art'] = False - switch() + # Update db + theme_update(g['stuff']) g['decorated_name'] = color_func( c['DECORATED_NAME'])( '[@' + g['original_name'] + ']: ') diff --git a/rainbowstream/table_def.py b/rainbowstream/table_def.py index f2435ab..9948ad7 100644 --- a/rainbowstream/table_def.py +++ b/rainbowstream/table_def.py @@ -27,5 +27,18 @@ class Message(Base): self.message_id = message_id +class Theme(Base): + + __tablename__ = "theme" + + theme_id = Column(Integer, primary_key=True) + theme_name = Column(String) + changed = Column(Boolean, default=False) + + def __init__(self, theme_name): + self.theme_name = theme_name + self.Boolean = False + + def init_db(): Base.metadata.create_all(engine) -- 2.25.1