semaphore ater enter key are pushed
authorOrakaro <nhatminh_179@hotmail.com>
Sat, 19 Jul 2014 04:13:23 +0000 (13:13 +0900)
committerOrakaro <nhatminh_179@hotmail.com>
Sat, 19 Jul 2014 04:13:23 +0000 (13:13 +0900)
rainbowstream/db.py
rainbowstream/draw.py
rainbowstream/rainbow.py
rainbowstream/table_def.py
setup.py

index 5bfb040..f8a1139 100644 (file)
@@ -98,3 +98,33 @@ class RainbowDB():
         session = Session()
         res = session.query(Theme).all()
         return res
+
+    def semaphore_store(self, flag):
+        """
+        Store semaphore flag
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        th = Semaphore(flag)
+        session.add(th)
+        session.commit()
+
+    def semaphore_update(self, flag):
+        """
+        Update semaphore flag
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Semaphore).all()
+        for r in res:
+            r.flag = flag
+        session.commit()
+
+    def semaphore_query(self):
+        """
+        Query semaphore
+        """
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Semaphore).all()
+        return res[0].flag
index d3e18dc..96d2657 100644 (file)
@@ -139,7 +139,7 @@ def color_func(func_name):
     return globals()[func_name]
 
 
-def draw(t, iot=False, keyword=None, fil=[], ig=[]):
+def draw(t, iot=False, keyword=None, check_semaphore=False, fil=[], ig=[]):
     """
     Draw the rainbow
     """
@@ -240,6 +240,12 @@ def draw(t, iot=False, keyword=None, fil=[], ig=[]):
     )
     line3 = '  ' + tweet
 
+    # Check the semaphore lock
+    if check_semaphore:
+        while db.semaphore_query():
+            time.sleep(0.5)
+
+    # Output
     printNicely('')
     printNicely(line1)
     printNicely(line2)
index 42bafba..308b76e 100644 (file)
@@ -135,18 +135,23 @@ def authen():
 
 def get_decorated_name():
     """
-    Beginning of every line
+    Init function
     """
+    # Get name
     t = Twitter(auth=authen())
     name = '@' + t.account.verify_credentials()['screen_name']
     g['original_name'] = name[1:]
     g['decorated_name'] = color_func(c['DECORATED_NAME'])('[' + name + ']: ')
 
+    # Theme init
     files = os.listdir(os.path.dirname(__file__) + '/colorset')
     themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json']
     g['themes'] = themes
     db.theme_store(c['THEME'])
 
+    # Semaphore init
+    db.semaphore_store(False)
+
 
 def switch():
     """
@@ -1537,17 +1542,22 @@ def listen():
         except:
             cmd = ''
         g['cmd'] = cmd
-        # Save cmd to global variable and call process
         try:
+            # Lock the semaphore
+            db.semaphore_update(True)
+            # Save cmd to global variable and call process
             g['stuff'] = ' '.join(line.split()[1:])
+            # Process the command
             process(cmd)()
+            # Not re-display
+            if cmd in ['switch', 't', 'rt', 'rep']:
+                g['prefix'] = False
+            else:
+                g['prefix'] = True
+            # Release the semaphore lock
+            db.semaphore_update(False)
         except Exception:
             printNicely(red('OMG something is wrong with Twitter right now.'))
-        # Not redisplay prefix
-        if cmd in ['switch', 't', 'rt', 'rep']:
-            g['prefix'] = False
-        else:
-            g['prefix'] = True
 
 
 def stream(domain, args, name='Rainbow Stream'):
@@ -1607,6 +1617,7 @@ def stream(domain, args, name='Rainbow Stream'):
                     t=tweet,
                     iot=args.image_on_term,
                     keyword=args.track_keywords,
+                    check_semaphore=True,
                     fil=args.filter,
                     ig=args.ignore,
                 )
index fda3948..822fa1c 100644 (file)
@@ -38,5 +38,16 @@ class Theme(Base):
         self.theme_name = theme_name
 
 
+class Semaphore(Base):
+
+    __tablename__ = "semaphore"
+
+    semaphore_id = Column(Integer, primary_key=True)
+    flag = Column(Boolean)
+
+    def __init__(self, flag):
+        self.flag = flag
+
+
 def init_db():
     Base.metadata.create_all(engine)
index 95fa835..ab8a711 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
 import os, sys
 
 # Bumped version
-version = '0.3.10'
+version = '0.4.0'
 
 # Require
 install_requires = [