refactoring some code
[rainbowstream.git] / rainbowstream / draw.py
index 6352d3a0f7209bd60debb5fca91e1063c8d59772..78c3a26ae10d1a6101e6c7243687b6f56bf46983 100644 (file)
@@ -93,24 +93,6 @@ def ascii_art(text):
     ))
 
 
-def show_calendar(month, date, rel):
-    """
-    Show the calendar in rainbow mode
-    """
-    month = random_rainbow(month)
-    date = ' '.join([cycle_color(i) for i in date.split(' ')])
-    today = str(int(os.popen('date +\'%d\'').read().strip()))
-    # Display
-    printNicely(month)
-    printNicely(date)
-    for line in rel:
-        ary = line.split(' ')
-        ary = lmap(lambda x: color_func(c['CAL']['today'])(x)
-                   if x == today
-                   else color_func(c['CAL']['days'])(x), ary)
-        printNicely(' '.join(ary))
-
-
 def check_config():
     """
     Check if config is changed
@@ -169,8 +151,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']
@@ -234,9 +225,9 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
 
     # Format info
     name = cycle_color(name)
-    nick = color_func(c['TWEET']['nick'])(' ' + screen_name + ' ')
-    clock = color_func(c['TWEET']['clock'])('[' + clock + ']')
-    id = color_func(c['TWEET']['id'])('[id=' + str(rid) + ']')
+    nick = color_func(c['TWEET']['nick'])(screen_name)
+    clock = clock
+    id = str(rid)
     fav = ''
     if favorited:
         fav = color_func(c['TWEET']['favorited'])(u'\u2605')
@@ -246,22 +237,30 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
     if expanded_url:
         for index in range(len(expanded_url)):
             tweet = lmap(
-                lambda x: expanded_url[index] if x == url[index] else x,
+                lambda x: expanded_url[index]
+                if x == url[index]
+                else x,
                 tweet)
     # Highlight RT
     tweet = lmap(
-        lambda x: color_func(
-            c['TWEET']['rt'])(x) if x == 'RT' else x,
+        lambda x: color_func(c['TWEET']['rt'])(x)
+        if x == 'RT'
+        else x,
         tweet)
     # Highlight screen_name
     tweet = lmap(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
     # Highlight link
     tweet = lmap(
-        lambda x: color_func(
-            c['TWEET']['link'])(x) if x[
-            0:4] == 'http' else x,
+        lambda x: color_func(c['TWEET']['link'])(x)
+        if x[0:4] == 'http'
+        else x,
+        tweet)
+    # Highlight hashtag
+    tweet = lmap(
+        lambda x: color_func(c['TWEET']['hashtag'])(x)
+        if x.startswith('#')
+        else x,
         tweet)
-
     # Highlight keyword
     tweet = ' '.join(tweet)
     if keyword:
@@ -275,21 +274,23 @@ def draw(t, keyword=None, check_semaphore=False, fil=[], ig=[]):
     # Load config formater
     try:
         formater = c['FORMAT']['TWEET']['DISPLAY']
-        formater =  name.join(formater.split("#name"))
-        formater =  nick.join(formater.split("#nick"))
-        formater =  clock.join(formater.split("#clock"))
-        formater =  id.join(formater.split("#id"))
-        formater =  fav.join(formater.split("#fav"))
-        formater =  tweet.join(formater.split("#tweet"))
+        formater = name.join(formater.split("#name"))
+        formater = nick.join(formater.split("#nick"))
+        formater = fav.join(formater.split("#fav"))
+        formater = tweet.join(formater.split("#tweet"))
+        # Change clock word
+        word = [w for w in formater.split() if '#clock' in w][0]
+        delimiter = color_func(c['TWEET']['clock'])(
+            clock.join(word.split('#clock')))
+        formater = delimiter.join(formater.split(word))
+        # Change id word
+        word = [w for w in formater.split() if '#id' in w][0]
+        delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id')))
+        formater = delimiter.join(formater.split(word))
     except:
         printNicely(red('Wrong format in config.'))
         return
 
-    # Check the semaphore lock
-    if check_semaphore:
-        while db.semaphore_query():
-            time.sleep(0.5)
-
     # Draw
     printNicely(formater)
 
@@ -303,10 +304,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'])
@@ -333,24 +343,32 @@ def print_message(m):
     sender_name = cycle_color(sender_name)
     sender_nick = color_func(c['MESSAGE']['sender'])(sender_screen_name)
     recipient_name = cycle_color(recipient_name)
-    recipient_nick = color_func(c['MESSAGE']['recipient'])(recipient_screen_name)
+    recipient_nick = color_func(
+        c['MESSAGE']['recipient'])(recipient_screen_name)
     to = color_func(c['MESSAGE']['to'])('>>>')
-    clock = color_func(c['MESSAGE']['clock'])('[' + clock + ']')
-    id = color_func(c['MESSAGE']['id'])('[message_id=' + str(rid) + ']')
+    clock = clock
+    id = str(rid)
 
     text = ''.join(lmap(lambda x: x + '  ' if x == '\n' else x, text))
 
     # Load config formater
     try:
         formater = c['FORMAT']['MESSAGE']['DISPLAY']
-        formater =  sender_name.join(formater.split("#sender_name"))
-        formater =  sender_nick.join(formater.split("#sender_nick"))
-        formater =  to.join(formater.split("#to"))
-        formater =  recipient_name.join(formater.split("#recipient_name"))
-        formater =  recipient_nick.join(formater.split("#recipient_nick"))
-        formater =  clock.join(formater.split("#clock"))
-        formater =  id.join(formater.split("#id"))
-        formater =  text.join(formater.split("#message"))
+        formater = sender_name.join(formater.split("#sender_name"))
+        formater = sender_nick.join(formater.split("#sender_nick"))
+        formater = to.join(formater.split("#to"))
+        formater = recipient_name.join(formater.split("#recipient_name"))
+        formater = recipient_nick.join(formater.split("#recipient_nick"))
+        formater = text.join(formater.split("#message"))
+        # Change clock word
+        word = [w for w in formater.split() if '#clock' in w][0]
+        delimiter = color_func(c['MESSAGE']['clock'])(
+            clock.join(word.split('#clock')))
+        formater = delimiter.join(formater.split(word))
+        # Change id word
+        word = [w for w in formater.split() if '#id' in w][0]
+        delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id')))
+        formater = delimiter.join(formater.split(word))
     except:
         printNicely(red('Wrong format in config.'))
         return
@@ -435,7 +453,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:
@@ -498,5 +516,25 @@ def print_list(group):
     printNicely('')
 
 
+def show_calendar(month, date, rel):
+    """
+    Show the calendar in rainbow mode
+    """
+    month = random_rainbow(month)
+    date = ' '.join([cycle_color(i) for i in date.split(' ')])
+    today = str(int(os.popen('date +\'%d\'').read().strip()))
+    # Display
+    printNicely(month)
+    printNicely(date)
+    for line in rel:
+        ary = line.split(' ')
+        ary = lmap(
+            lambda x: color_func(c['CAL']['today'])(x)
+            if x == today
+            else color_func(c['CAL']['days'])(x),
+            ary)
+        printNicely(' '.join(ary))
+
+
 # Start the color cycle
 start_cycle()