theme on DB
authorVu Nhat Minh <vunhat_minh@dwango.co.jp>
Thu, 3 Jul 2014 04:31:36 +0000 (13:31 +0900)
committerVu Nhat Minh <vunhat_minh@dwango.co.jp>
Thu, 3 Jul 2014 04:31:36 +0000 (13:31 +0900)
rainbowstream/config.py
rainbowstream/db.py
rainbowstream/draw.py
rainbowstream/rainbow.py
rainbowstream/table_def.py

index 418afbb0a03411a1a563cdb5d479c4c5c43c4215..4b4aef6fbc44bd15dc587067d9c584361b13c71e 100644 (file)
@@ -3,6 +3,10 @@ import re
 import os
 import os.path
 
 import os
 import os.path
 
+from .db import *
+
+db = RainbowDB()
+
 # Regular expression for comments
 comment_re = re.compile(
     '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
 # 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)
             while match:
                 content = content[:match.start()] + content[match.end():]
                 match = comment_re.search(content)
+        db.theme_store('user')
         return json.loads(content)
         return json.loads(content)
-    except:
+    except IOError:
+        db.theme_store('default')
         pass
 
 # Load default colorset
         pass
 
 # Load default colorset
index fabc3a4c26bcee11f65cbc4fcd4211b7079fccaf..775c8563d51dd6d81c79d9ae42b2d1417a22008b 100644 (file)
@@ -1,5 +1,6 @@
 import os
 from sqlalchemy import create_engine
 import os
 from sqlalchemy import create_engine
+from sqlalchemy import update
 from sqlalchemy.orm import sessionmaker
 from .table_def import *
 
 from sqlalchemy.orm import sessionmaker
 from .table_def import *
 
@@ -19,8 +20,8 @@ class RainbowDB():
         """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         """
         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):
         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
         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
index 9a8fbdd041c77110f21d64e097170e23f28ba35b..5063df4ed73b6422705a39609568014410f6d865 100644 (file)
@@ -14,6 +14,29 @@ from .config import *
 from .db import *
 
 db = RainbowDB()
 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):
 
 
 def color_func(func_name):
@@ -31,6 +54,7 @@ def draw(t, iot=False, keyword=None, fil=[], ig=[]):
     Draw the rainbow
     """
 
     Draw the rainbow
     """
 
+    check_theme()
     # Retrieve tweet
     tid = t['id']
     text = t['text']
     # Retrieve tweet
     tid = t['id']
     text = t['text']
index ce5d99898171f1230bbc691782efb0782b3bd91f..00e01eb9e6079175e5ba9c1ba7b315b3e2ee3109 100644 (file)
@@ -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']
 
     files = os.listdir('rainbowstream/colorset')
     themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
+    themes += ['user']
     g['themes'] = themes
 
 
     g['themes'] = themes
 
 
@@ -768,8 +769,19 @@ def theme():
     """
     if not g['stuff']:
         # List themes
     """
     if not g['stuff']:
         # List themes
+        line = ' ' * 2 + light_yellow('* ')
         for theme in g['themes']:
         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
             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]
             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'] + ']: ')
             g['decorated_name'] = color_func(
                 c['DECORATED_NAME'])(
                 '[@' + g['original_name'] + ']: ')
index f2435ab30cbe3d3d555dd6bd347959d79137b277..9948ad70139fe13d0e9b6c2a5df9ac637370f142 100644 (file)
@@ -27,5 +27,18 @@ class Message(Base):
         self.message_id = message_id
 
 
         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)
 def init_db():
     Base.metadata.create_all(engine)