check semaphore fist on tweet and message
authorOrakaro <nhatminh_179@hotmail.com>
Sun, 27 Jul 2014 12:58:26 +0000 (21:58 +0900)
committerOrakaro <nhatminh_179@hotmail.com>
Sun, 27 Jul 2014 12:58:26 +0000 (21:58 +0900)
docs/conf.py
rainbowstream/db.py
rainbowstream/draw.py
rainbowstream/rainbow.py
rainbowstream/table_def.py
setup.py

index a5e6386..1d3359c 100644 (file)
@@ -51,9 +51,9 @@ copyright = u'2014, Vu Nhat Minh'
 # built documents.
 #
 # The short X.Y version.
-version = '0.5.3'
+version = '0.5.4'
 # The full version, including alpha/beta/rc tags.
-release = '0.5.3'
+release = '0.5.4'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
index 58cc4b5..01b91fb 100644 (file)
@@ -113,26 +113,26 @@ class RainbowDB():
         return res
 
 
-    def semaphore_store(self, flag, pause):
+    def semaphore_store(self, lock, pause):
         """
-        Store semaphore flag
+        Store semaphore lock
         """
         Session = sessionmaker(bind=self.engine)
         session = Session()
-        th = Semaphore(flag, pause)
+        th = Semaphore(lock, pause)
         session.add(th)
         session.commit()
 
 
-    def semaphore_update_flag(self, flag):
+    def semaphore_update_lock(self, lock):
         """
-        Update semaphore flag
+        Update semaphore lock
         """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         res = session.query(Semaphore).all()
         for r in res:
-            r.flag = flag
+            r.lock = lock
         session.commit()
 
 
@@ -148,14 +148,14 @@ class RainbowDB():
         session.commit()
 
 
-    def semaphore_query_flag(self):
+    def semaphore_query_lock(self):
         """
-        Query semaphore flag
+        Query semaphore lock
         """
         Session = sessionmaker(bind=self.engine)
         session = Session()
         res = session.query(Semaphore).all()
-        return res[0].flag
+        return res[0].lock
 
 
     def semaphore_query_pause(self):
index 0be6ece..218aa42 100644 (file)
@@ -169,8 +169,17 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
     Draw the rainbow
     """
 
+    # Check the semaphore lock (stream process only)
+    if check_semaphore:
+        if db.semaphore_query_pause():
+            return
+        while db.semaphore_query_lock():
+            time.sleep(0.5)
+
+    # Check config and theme
     check_config()
     reload_theme(c['THEME'])
+
     # Retrieve tweet
     tid = t['id']
     text = t['text']
@@ -294,13 +303,6 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
         printNicely(red('Wrong format in config.'))
         return
 
-    # Check the semaphore lock (stream process only)
-    if check_semaphore:
-        if db.semaphore_query_pause():
-            return
-        while db.semaphore_query_flag():
-            time.sleep(0.5)
-
     # Draw
     printNicely(formater)
 
@@ -314,10 +316,19 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
                 printNicely(red('Sorry, image link is broken'))
 
 
-def print_message(m):
+def print_message(m, check_semaphore=False):
     """
     Print direct message
     """
+
+    # Check the semaphore lock (stream process only)
+    if check_semaphore:
+        if db.semaphore_query_pause():
+            return
+        while db.semaphore_query_lock():
+            time.sleep(0.5)
+
+    # Retrieve message
     sender_screen_name = '@' + m['sender_screen_name']
     sender_name = m['sender']['name']
     text = unescape(m['text'])
@@ -456,7 +467,7 @@ def show_profile(u):
     if c['IMAGE_ON_TERM']:
         try:
             response = requests.get(profile_image_url)
-            image_to_display(BytesIO(response.content), 2, 20)
+            image_to_display(BytesIO(response.content))
         except:
             pass
     else:
index 4a1e3d7..fbac32c 100644 (file)
@@ -1619,7 +1619,7 @@ def listen():
         g['cmd'] = cmd
         try:
             # Lock the semaphore
-            db.semaphore_update_flag(True)
+            db.semaphore_update_lock(True)
             # Save cmd to global variable and call process
             g['stuff'] = ' '.join(line.split()[1:])
             # Process the command
@@ -1630,7 +1630,7 @@ def listen():
             else:
                 g['prefix'] = True
             # Release the semaphore lock
-            db.semaphore_update_flag(False)
+            db.semaphore_update_lock(False)
         except Exception:
             printNicely(red('OMG something is wrong with Twitter right now.'))
 
@@ -1689,7 +1689,7 @@ def stream(domain, args, name='Rainbow Stream'):
                     ig=args.ignore,
                 )
             elif tweet.get('direct_message'):
-                print_message(tweet['direct_message'])
+                print_message(tweet['direct_message'],check_semaphore=True)
     except TwitterHTTPError:
         printNicely('')
         printNicely(
index b16f175..03ceb5a 100644 (file)
@@ -43,11 +43,11 @@ class Semaphore(Base):
     __tablename__ = "semaphore"
 
     semaphore_id = Column(Integer, primary_key=True)
-    flag = Column(Boolean)
+    lock = Column(Boolean)
     pause = Column(Boolean)
 
-    def __init__(self, flag, pause):
-        self.flag = flag
+    def __init__(self, lock, pause):
+        self.lock = lock
         self.pause = pause
 
 
index 51306bc..ae00494 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
 import os, os.path, sys
 
 # Bumped version
-version = '0.5.3'
+version = '0.5.4'
 
 # Require
 install_requires = [