]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/cmdline.py
Make the code python 2 and 3 compatible.
[z_archive/twitter.git] / twitter / cmdline.py
index fdf01fc4c0a558d47d90eef654a0ea360e42e379..34e3b58ee61221e8bb4fc03508f78d9cadc77f15 100644 (file)
@@ -16,6 +16,8 @@ ACTIONS:
  mylist         get list of your lists; give a list name to get tweets
                     from that list
  public         get latest public tweets
+ pyprompt       start a Python prompt for interacting with the twitter
+                    object directly
  replies        get latest replies to you
  search         search twitter (Beware: octothorpe, escape it)
  set            set your twitter status
@@ -59,6 +61,8 @@ prompt: <twitter_shell_prompt e.g. '[cyan]twitter[R]> '>
  home directory.
 """
 
+from __future__ import print_function
+
 CONSUMER_KEY='uS6hO2sV6tDKIOeVjhnFnQ'
 CONSUMER_SECRET='MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk'
 
@@ -68,15 +72,22 @@ from getopt import gnu_getopt as getopt, GetoptError
 from getpass import getpass
 import re
 import os.path
-from configparser import SafeConfigParser
+try:
+    from ConfigParser import SafeConfigParser
+except ImportError:
+    from configparser import ConfigParser as SafeConfigParser
 import datetime
-from urllib.parse import quote
+try:
+    from urllib.parse import quote
+except ImportError:
+    from urllib2 import quote
 import webbrowser
 
 from .api import Twitter, TwitterError
 from .oauth import OAuth, write_token_file, read_token_file
 from .oauth_dance import oauth_dance
 from . import ansi
+from .util import smrt_input
 
 OPTIONS = {
     'action': 'friends',
@@ -350,8 +361,11 @@ class NoSuchAction(Action):
         raise NoSuchActionError("No such action: %s" %(options['action']))
 
 def printNicely(string):
-    sys.stdout.buffer.write(string.encode('utf8'))
-    print()
+    if hasattr(sys.stdout, 'buffer'):
+        sys.stdout.buffer.write(string.encode('utf8'))
+        print()
+    else:
+        print(string.encode('utf8'))
 
 class StatusAction(Action):
     def __call__(self, twitter, options):
@@ -500,6 +514,14 @@ class TwitterShell(Action):
                 else:
                     raise SystemExit(0)
 
+class PythonPromptAction(Action):
+    def __call__(self, twitter, options):
+        try:
+            while True:
+                smrt_input(globals(), locals())
+        except EOFError:
+            pass
+
 class HelpAction(Action):
     def __call__(self, twitter, options):
         print(__doc__)
@@ -517,6 +539,7 @@ actions = {
     'help'      : HelpAction,
     'leave'     : LeaveAction,
     'public'    : PublicAction,
+    'pyprompt'  : PythonPromptAction,
     'replies'   : RepliesAction,
     'search'    : SearchAction,
     'set'       : SetStatusAction,