]> jfr.im git - z_archive/twitter.git/blob - twitter/util.py
Fix unicorn decode bugs in ircbot.
[z_archive/twitter.git] / twitter / util.py
1 """
2 Internal utility functions.
3
4 `htmlentitydecode` came from here:
5 http://wiki.python.org/moin/EscapingHtml
6 """
7
8
9 import re
10 import sys
11 try:
12 from html.entities import name2codepoint
13 except ImportError:
14 from htmlentitydefs import name2codepoint
15
16 def htmlentitydecode(s):
17 return re.sub(
18 '&(%s);' % '|'.join(name2codepoint),
19 lambda m: chr(name2codepoint[m.group(1)]), s)
20
21 def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "):
22 inputs = []
23 while True:
24 if inputs:
25 prompt = ps2
26 else:
27 prompt = ps1
28 inputs.append(input(prompt))
29 try:
30 ret = eval('\n'.join(inputs), globals_, locals_)
31 if ret:
32 print(str(ret))
33 return
34 except SyntaxError:
35 pass
36
37 def printNicely(string):
38 if hasattr(sys.stdout, 'buffer'):
39 sys.stdout.buffer.write(string.encode('utf8'))
40 print()
41 else:
42 print(string.encode('utf8'))
43
44 __all__ = ["htmlentitydecode", "smrt_input"]