]>
Commit | Line | Data |
---|---|---|
8ad2cf0b | 1 | """ |
2 | Internal utility functions. | |
3 | ||
4 | `htmlentitydecode` came from here: | |
5 | http://wiki.python.org/moin/EscapingHtml | |
6 | """ | |
7 | ||
8 | ||
9 | import re | |
098660ce | 10 | import sys |
3930cc7b MV |
11 | try: |
12 | from html.entities import name2codepoint | |
13 | except ImportError: | |
14 | from htmlentitydefs import name2codepoint | |
8ad2cf0b | 15 | |
16 | def htmlentitydecode(s): | |
17 | return re.sub( | |
a5e40197 | 18 | '&(%s);' % '|'.join(name2codepoint), |
f7e63802 | 19 | lambda m: chr(name2codepoint[m.group(1)]), s) |
8ad2cf0b | 20 | |
a5e40197 MV |
21 | def smrt_input(globals_, locals_, ps1=">>> ", ps2="... "): |
22 | inputs = [] | |
23 | while True: | |
24 | if inputs: | |
25 | prompt = ps2 | |
26 | else: | |
27 | prompt = ps1 | |
7bfe7d97 | 28 | inputs.append(input(prompt)) |
a5e40197 MV |
29 | try: |
30 | ret = eval('\n'.join(inputs), globals_, locals_) | |
31 | if ret: | |
30e61103 | 32 | print(str(ret)) |
a5e40197 MV |
33 | return |
34 | except SyntaxError: | |
35 | pass | |
36 | ||
098660ce MV |
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 | ||
a5e40197 | 44 | __all__ = ["htmlentitydecode", "smrt_input"] |