1
1
from __future__ import print_function
2
+
2
3
import inspect
3
4
import re
4
5
import sys
5
6
7
+ from os .path import abspath , join
8
+ from site import getsitepackages , getusersitepackages
9
+ from termcolor import cprint
10
+ from traceback import extract_tb , format_list , format_exception_only
11
+
12
+
13
+ def excepthook (type , value , tb ):
14
+ """
15
+ Format traceback, darkening entries from global site-packages and user-specific site-packages directory.
16
+
17
+ https://stackoverflow.com/a/33042323/5156190
18
+ """
19
+ packages = tuple (join (abspath (p ), "" ) for p in getsitepackages () + [getusersitepackages ()])
20
+ for entry in extract_tb (tb ):
21
+ fmt = format_list ((entry ,))
22
+ if (entry .filename .startswith (packages )):
23
+ cprint ("" .join (fmt ), attrs = ["dark" ], end = "" , file = sys .stderr )
24
+ else :
25
+ cprint ("" .join (fmt ), end = "" , file = sys .stderr )
26
+ cprint ("" .join (format_exception_only (type , value )), end = "" )
27
+
28
+
29
+ sys .excepthook = excepthook
30
+
31
+
6
32
class flushfile ():
7
33
"""
8
34
Disable buffering for standard output and standard error.
9
35
10
36
http://stackoverflow.com/a/231216
11
37
"""
38
+
12
39
def __init__ (self , f ):
13
40
self .f = f
14
41
@@ -18,9 +45,12 @@ def __getattr__(self, name):
18
45
def write (self , x ):
19
46
self .f .write (x )
20
47
self .f .flush ()
48
+
49
+
21
50
sys .stderr = flushfile (sys .stderr )
22
51
sys .stdout = flushfile (sys .stdout )
23
52
53
+
24
54
def eprint (* args , ** kwargs ):
25
55
"""
26
56
Print an error message to standard error, prefixing it with
@@ -32,6 +62,7 @@ def eprint(*args, **kwargs):
32
62
print ("{}:{}: " .format (filename , lineno ), end = "" )
33
63
print (* args , end = end , file = sys .stderr , sep = sep )
34
64
65
+
35
66
def get_char (prompt = None ):
36
67
"""
37
68
Read a line of text from standard input and return the equivalent char;
@@ -49,6 +80,7 @@ def get_char(prompt=None):
49
80
if prompt is None :
50
81
print ("Retry: " , end = "" )
51
82
83
+
52
84
def get_float (prompt = None ):
53
85
"""
54
86
Read a line of text from standard input and return the equivalent float
@@ -69,20 +101,21 @@ def get_float(prompt=None):
69
101
if prompt is None :
70
102
print ("Retry: " , end = "" )
71
103
104
+
72
105
def get_int (prompt = None ):
73
106
"""
74
107
Read a line of text from standard input and return the equivalent int;
75
108
if text does not represent an int, user is prompted to retry. If line
76
109
can't be read, return None.
77
110
"""
78
111
while True :
79
- s = get_string (prompt );
112
+ s = get_string (prompt )
80
113
if s is None :
81
114
return None
82
115
if re .search (r"^[+-]?\d+$" , s ):
83
116
try :
84
117
i = int (s , 10 )
85
- if type (i ) is int : # could become long in Python 2
118
+ if type (i ) is int : # could become long in Python 2
86
119
return i
87
120
except ValueError :
88
121
pass
@@ -91,6 +124,7 @@ def get_int(prompt=None):
91
124
if prompt is None :
92
125
print ("Retry: " , end = "" )
93
126
127
+
94
128
if sys .version_info .major != 3 :
95
129
def get_long (prompt = None ):
96
130
"""
@@ -112,6 +146,7 @@ def get_long(prompt=None):
112
146
if prompt is None :
113
147
print ("Retry: " , end = "" )
114
148
149
+
115
150
def get_string (prompt = None ):
116
151
"""
117
152
Read a line of text from standard input and return it as a string,
0 commit comments