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 5bfb04081c715ad1b8f2690b1527a42987e9222a..f8a11397933484e6f3ed2368ffee35000a22f135 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 d3e18dc97860a11129ebf1d5aa6da4ca39d3daab..96d26577dd063df7ddd1f94b2f4acd23f6434dc2 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 42bafba16671e8e7398b71bcabdd136bd4f5d167..308b76e00625e8af5afbd21109af710ccaf72f70 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 fda3948dfbe0ad06d8bf77e08c1d296fbca93862..822fa1c7ff53d4d9da09eca6cee1ea0f56dfd820 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 95fa835eb8d2b5f9bc158a0d86f05787d83d9d06..ab8a7111c3f60d4efd54ed7d887d0a5340f793da 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 = [