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 a5e6386adfa2a224b2a0d3b5d2e8549a7ec5b9fa..1d3359c06ac2e9ef5f755c699da26fcd76b4fb95 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 58cc4b5555bee624b7d20b83ed7ceeb957c31382..01b91fb163fbd7e5933da975d46e878610ae87d1 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 0be6ecebc389ae8fb440a4d6a6d67d18a9fe2588..218aa42b509b714ee591060d74b7a6a5c6bf94a6 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 4a1e3d78c02c6624126aecec5a66724fd48a1dba..fbac32cb818bebd18cde231d440d74becb029f7c 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 b16f175191765c1649f716bc4d4cf0d8fee09d35..03ceb5a4e6350a2b6a7c99c2cc82562fbc36dc41 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 51306bc15dcbcc15718d83542c2ced834478bc86..ae0049449fed54f1bd1c24f07b9d740f566ca7c6 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 = [