|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import inspect |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | +from distutils.sysconfig import get_python_lib |
| 8 | +from os.path import abspath, join |
| 9 | +from termcolor import colored |
| 10 | +from traceback import extract_tb, format_list, format_exception_only, format_exception |
| 11 | + |
| 12 | + |
| 13 | +class flushfile(): |
| 14 | + """ |
| 15 | + Disable buffering for standard output and standard error. |
| 16 | +
|
| 17 | + http://stackoverflow.com/a/231216 |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, f): |
| 21 | + self.f = f |
| 22 | + |
| 23 | + def __getattr__(self, name): |
| 24 | + return object.__getattribute__(self.f, name) |
| 25 | + |
| 26 | + def write(self, x): |
| 27 | + self.f.write(x) |
| 28 | + self.f.flush() |
| 29 | + |
| 30 | + |
| 31 | +sys.stderr = flushfile(sys.stderr) |
| 32 | +sys.stdout = flushfile(sys.stdout) |
| 33 | + |
| 34 | + |
| 35 | +def eprint(*args, **kwargs): |
| 36 | + """ |
| 37 | + Print an error message to standard error, prefixing it with |
| 38 | + file name and line number from which method was called. |
| 39 | + """ |
| 40 | + end = kwargs.get("end", "\n") |
| 41 | + sep = kwargs.get("sep", " ") |
| 42 | + (filename, lineno) = inspect.stack()[1][1:3] |
| 43 | + print("{}:{}: ".format(filename, lineno), end="") |
| 44 | + print(*args, end=end, file=sys.stderr, sep=sep) |
| 45 | + |
| 46 | + |
| 47 | +def formatException(type, value, tb): |
| 48 | + """ |
| 49 | + Format traceback, darkening entries from global site-packages directories |
| 50 | + and user-specific site-packages directory. |
| 51 | +
|
| 52 | + https://stackoverflow.com/a/46071447/5156190 |
| 53 | + """ |
| 54 | + |
| 55 | + # Absolute paths to site-packages |
| 56 | + packages = tuple(join(abspath(p), "") for p in sys.path[1:]) |
| 57 | + |
| 58 | + # Highlight lines not referring to files in site-packages |
| 59 | + lines = [] |
| 60 | + for line in format_exception(type, value, tb): |
| 61 | + matches = re.search(r"^ File \"([^\"]+)\", line \d+, in .+", line) |
| 62 | + if matches and matches.group(1).startswith(packages): |
| 63 | + lines += line |
| 64 | + else: |
| 65 | + matches = re.search(r"^(\s*)(.*?)(\s*)$", line, re.DOTALL) |
| 66 | + lines.append(matches.group(1) + colored(matches.group(2), "yellow") + matches.group(3)) |
| 67 | + return "".join(lines).rstrip() |
| 68 | + |
| 69 | + |
| 70 | +sys.excepthook = lambda type, value, tb: print(formatException(type, value, tb), file=sys.stderr) |
| 71 | + |
| 72 | + |
| 73 | +def get_char(prompt=None): |
| 74 | + """ |
| 75 | + Read a line of text from standard input and return the equivalent char; |
| 76 | + if text is not a single char, user is prompted to retry. If line can't |
| 77 | + be read, return None. |
| 78 | + """ |
| 79 | + while True: |
| 80 | + s = get_string(prompt) |
| 81 | + if s is None: |
| 82 | + return None |
| 83 | + if len(s) == 1: |
| 84 | + return s[0] |
| 85 | + |
| 86 | + # temporarily here for backwards compatibility |
| 87 | + if prompt is None: |
| 88 | + print("Retry: ", end="") |
| 89 | + |
| 90 | + |
| 91 | +def get_float(prompt=None): |
| 92 | + """ |
| 93 | + Read a line of text from standard input and return the equivalent float |
| 94 | + as precisely as possible; if text does not represent a double, user is |
| 95 | + prompted to retry. If line can't be read, return None. |
| 96 | + """ |
| 97 | + while True: |
| 98 | + s = get_string(prompt) |
| 99 | + if s is None: |
| 100 | + return None |
| 101 | + if len(s) > 0 and re.search(r"^[+-]?\d*(?:\.\d*)?$", s): |
| 102 | + try: |
| 103 | + return float(s) |
| 104 | + except ValueError: |
| 105 | + pass |
| 106 | + |
| 107 | + # temporarily here for backwards compatibility |
| 108 | + if prompt is None: |
| 109 | + print("Retry: ", end="") |
| 110 | + |
| 111 | + |
| 112 | +def get_int(prompt=None): |
| 113 | + """ |
| 114 | + Read a line of text from standard input and return the equivalent int; |
| 115 | + if text does not represent an int, user is prompted to retry. If line |
| 116 | + can't be read, return None. |
| 117 | + """ |
| 118 | + while True: |
| 119 | + s = get_string(prompt) |
| 120 | + if s is None: |
| 121 | + return None |
| 122 | + if re.search(r"^[+-]?\d+$", s): |
| 123 | + try: |
| 124 | + i = int(s, 10) |
| 125 | + if type(i) is int: # could become long in Python 2 |
| 126 | + return i |
| 127 | + except ValueError: |
| 128 | + pass |
| 129 | + |
| 130 | + # temporarily here for backwards compatibility |
| 131 | + if prompt is None: |
| 132 | + print("Retry: ", end="") |
| 133 | + |
| 134 | + |
| 135 | +if sys.version_info.major != 3: |
| 136 | + def get_long(prompt=None): |
| 137 | + """ |
| 138 | + Read a line of text from standard input and return the equivalent long; |
| 139 | + if text does not represent a long, user is prompted to retry. If line |
| 140 | + can't be read, return None. |
| 141 | + """ |
| 142 | + while True: |
| 143 | + s = get_string(prompt) |
| 144 | + if s is None: |
| 145 | + return None |
| 146 | + if re.search(r"^[+-]?\d+$", s): |
| 147 | + try: |
| 148 | + return long(s, 10) |
| 149 | + except ValueError: |
| 150 | + pass |
| 151 | + |
| 152 | + # temporarily here for backwards compatibility |
| 153 | + if prompt is None: |
| 154 | + print("Retry: ", end="") |
| 155 | + |
| 156 | + |
| 157 | +def get_string(prompt=None): |
| 158 | + """ |
| 159 | + Read a line of text from standard input and return it as a string, |
| 160 | + sans trailing line ending. Supports CR (\r), LF (\n), and CRLF (\r\n) |
| 161 | + as line endings. If user inputs only a line ending, returns "", not None. |
| 162 | + Returns None upon error or no input whatsoever (i.e., just EOF). Exits |
| 163 | + from Python altogether on SIGINT. |
| 164 | + """ |
| 165 | + try: |
| 166 | + if prompt is not None: |
| 167 | + print(prompt, end="") |
| 168 | + s = sys.stdin.readline() |
| 169 | + if not s: |
| 170 | + return None |
| 171 | + return re.sub(r"(?:\r|\r\n|\n)$", "", s) |
| 172 | + except KeyboardInterrupt: |
| 173 | + sys.exit("") |
| 174 | + except ValueError: |
| 175 | + return None |
0 commit comments