]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/util.py
bug fix for profile image API
[z_archive/twitter.git] / twitter / util.py
index 70ff7a5f0b5b7541341e28723a576450bb9a28fb..27142af5636f22d2593994f6e2450533148a78fe 100644 (file)
@@ -5,13 +5,73 @@ Internal utility functions.
     http://wiki.python.org/moin/EscapingHtml
 """
 
+from __future__ import print_function
 
 import re
-from htmlentitydefs import name2codepoint
+import sys
+import time
+
+try:
+    from html.entities import name2codepoint
+    unichr = chr
+except ImportError:
+    from htmlentitydefs import name2codepoint
 
 def htmlentitydecode(s):
     return re.sub(
-        '&(%s);' % '|'.join(name2codepoint), 
+        '&(%s);' % '|'.join(name2codepoint),
         lambda m: unichr(name2codepoint[m.group(1)]), s)
 
-__all__ = ["htmlentitydecode"]
+def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
+    inputs = []
+    while True:
+        if inputs:
+            prompt = ps2
+        else:
+            prompt = ps1
+        inputs.append(input(prompt))
+        try:
+            ret = eval('\n'.join(inputs), globals_, locals_)
+            if ret:
+                print(str(ret))
+            return
+        except SyntaxError:
+            pass
+
+def printNicely(string):
+    if hasattr(sys.stdout, 'buffer'):
+        sys.stdout.buffer.write(string.encode('utf8'))
+        print()
+    else:
+        print(string.encode('utf8'))
+
+__all__ = ["htmlentitydecode", "smrt_input"]
+
+def err(msg=""):
+    print(msg, file=sys.stderr)
+
+class Fail(object):
+    """A class to count fails during a repetitive task.
+
+    Args:
+        maximum: An integer for the maximum of fails to allow.
+        exit: An integer for the exit code when maximum of fail is reached.
+
+    Methods:
+        count: Count a fail, exit when maximum of fails is reached.
+        wait: Same as count but also sleep for a given time in seconds.
+    """
+    def __init__(self, maximum=10, exit=1):
+        self.i = maximum
+        self.exit = exit
+
+    def count(self):
+        self.i -= 1
+        if self.i == 0:
+            err("Too many consecutive fails, exiting.")
+            raise SystemExit(self.exit)
+
+    def wait(self, delay=0):
+        self.count()
+        if delay > 0:
+            time.sleep(delay)