updated succesfully
[rainbowstream.git] / rainbowstream / rainbow.py
index 233f441df1b0df9c5667792d06e689ee17a03668..c424e841e779c640b60a12c6364f80e62fa776ba 100644 (file)
@@ -64,6 +64,7 @@ cmdset = [
     'report',
     'list',
     'cal',
+    'config',
     'theme',
     'h',
     'c',
@@ -135,18 +136,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():
     """
@@ -1074,6 +1080,49 @@ def cal():
     show_calendar(month, date, rel)
 
 
+def config():
+    """
+    Browse and change config
+    """
+    all_config = get_all_config()
+    g['stuff'] = g['stuff'].strip()
+    # List all config
+    if not g['stuff']:
+        for k in all_config:
+            line = ' '*2 + light_green(k) + ': ' + light_yellow(str(all_config[k]))
+            printNicely(line)
+        guide = 'Detailed explanation can be found at ' + \
+            color_func(c['TWEET']['link'])('http://rainbowstream.readthedocs.org/en/latest/#config-explanation')
+        printNicely(guide)
+    # Print specific config
+    elif len(g['stuff'].split()) == 1:
+        if g['stuff'] in all_config:
+            k = g['stuff']
+            line = ' '*2 + light_green(k) + ': ' + light_yellow(str(all_config[k]))
+            printNicely(line)
+        else:
+            printNicely(red('No config key like this.'))
+    # Print specific config's default value
+    elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'default':
+        key = g['stuff'].split()[0]
+        value = get_default_config(key)
+        line = ' '*2 + light_green(key) + ': ' + light_magenta(value)
+        printNicely(line)
+    # Set specific config
+    elif len(g['stuff'].split()) == 3 and g['stuff'].split()[1] == '=' :
+        key = g['stuff'].split()[0]
+        value = g['stuff'].split()[-1]
+        try:
+            set_config(key,value)
+            printNicely(light_green('Updated successfully.'))
+        except:
+            printNicely(light_magenta('Not valid value.'))
+            return
+        reload_config()
+    else:
+        printNicely(light_magenta('Sorry I can\'s understand.'))
+
+
 def theme():
     """
     List and change theme
@@ -1089,24 +1138,7 @@ def theme():
             printNicely(line)
     elif g['stuff'] == 'current_as_default':
         # Set as default
-        def fixup(adict, k, v):
-            for key in adict.keys():
-                if key == k:
-                    adict[key] = v
-                elif type(adict[key]) is dict:
-                    fixup(adict[key], k, v)
-        # Modify
-        path = os.environ.get(
-            'HOME',
-            os.environ.get(
-                'USERPROFILE',
-                '')) + os.sep + '.rainbow_config.json'
-        data = load_config(rainbow_config)
-        fixup(data, 'THEME', c['THEME'])
-        # Save
-        with open(path, 'w') as out:
-            json.dump(data, out, indent = 4)
-        os.system('chmod 777 ' + path)
+        set_config('THEME',c['THEME'])
         printNicely(light_green('Okay it will be applied from next time :)'))
     else:
         # Change theme
@@ -1155,8 +1187,9 @@ def help_discover():
         magenta('@mdo') + '.\n'
     usage += s * 2 + light_green('view @mdo') + \
         ' will show ' + magenta('@mdo') + '\'s home.\n'
-    usage += s * 2 + light_green('s #AKB48') + ' will search for "' + \
-        light_yellow('AKB48') + '" and return 5 newest tweet.\n'
+    usage += s * 2 + light_green('s AKB48') + ' will search for "' + \
+        light_yellow('AKB48') + '" and return 5 newest tweet. ' + \
+        'Search can be performed with or without hashtag.\n'
     printNicely(usage)
 
 
@@ -1352,12 +1385,26 @@ def help():
     usage += s * 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \
         ' for current month.\n'
 
-    # Screening
+    # Config
     usage += '\n'
-    usage += s + grey(u'\u266A' + ' Screening \n')
-    usage += s * 2 + light_green('theme') + ' will list available theme.' + \
+    usage += s + grey(u'\u266A' + ' Config \n')
+    usage += s * 2 + light_green('theme') + ' will list available theme. ' + \
         light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \
         ' theme immediately.\n'
+    usage += s * 2 + light_green('config') + ' will list all config.\n'
+    usage += s * 3 + \
+        light_green('config ASCII_ART') + ' will output current value of ' +\
+        light_yellow('ASCII_ART') +' config key.\n'
+    usage += s * 3 + \
+        light_green('config ASCII_ART default') + ' will output default value of ' + \
+        light_yellow('ASCII_ART') +' config key.\n'
+    usage += s * 3 + \
+        light_green('config ASCII_ART = False') + ' will set value of ' + \
+        light_yellow('ASCII_ART') +' config key to ' + light_yellow('False') + '.\n'
+
+    # Screening
+    usage += '\n'
+    usage += s + grey(u'\u266A' + ' Screening \n')
     usage += s * 2 + light_green('h') + ' will show this help again.\n'
     usage += s * 2 + light_green('c') + ' will clear the screen.\n'
     usage += s * 2 + light_green('q') + ' will quit.\n'
@@ -1451,6 +1498,7 @@ def process(cmd):
             report,
             list,
             cal,
+            config,
             theme,
             help,
             clear,
@@ -1510,6 +1558,7 @@ def listen():
                 'del'
             ],  # list
             [],  # cal
+            [key for key in dict(get_all_config())], # config
             g['themes'] + ['current_as_default'],  # theme
             [
                 'discover',
@@ -1536,17 +1585,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'):
@@ -1606,6 +1660,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,
                 )