32
32
33
33
import argparse
34
34
import code
35
+ import os
35
36
import pprint
36
37
import re
38
+ import sys
37
39
import textwrap
38
40
import traceback
39
41
43
45
44
46
try :
45
47
from IPython import embed
48
+ from IPython .core import ultratb
46
49
from prompt_toolkit import PromptSession
47
50
except ImportError :
48
51
has_development_dependencies = False
@@ -75,6 +78,11 @@ def main():
75
78
if not has_development_dependencies :
76
79
parser .error ('development dependencies are not installed, install them with: pipenv install --dev' )
77
80
81
+ if os .environ .get ('NO_COLOR' ):
82
+ color_scheme = 'nocolor'
83
+ else :
84
+ color_scheme = 'neutral'
85
+
78
86
context = engine .Context ()
79
87
thing = None
80
88
if arguments .edit_console or arguments .edit_file :
@@ -95,11 +103,12 @@ def main():
95
103
namespace = {'context' : context , 'thing' : thing }
96
104
print ("Starting IPython shell..." )
97
105
print ("Edit the \' context\' and \' thing\' objects as necessary" )
98
- embed (colors = "neutral" , user_ns = namespace )
106
+ embed (colors = color_scheme , user_ns = namespace )
99
107
context = namespace .get ('context' , context )
100
108
thing = namespace .get ('thing' , thing )
101
109
debugging = arguments .debug
102
110
session = PromptSession ()
111
+ color_tb = ultratb .ColorTB (color_scheme = color_scheme )
103
112
104
113
while True :
105
114
try :
@@ -117,25 +126,21 @@ def main():
117
126
rule = engine .Rule (rule_text , context = context )
118
127
result = rule .evaluate (thing )
119
128
except errors .EngineError as error :
120
- print ("{}: {}" . format ( error .__class__ .__name__ , error .message ) )
129
+ print (f" { color_tb . Colors . excName } { error .__class__ .__name__ } : { color_tb . Colors . Normal } { error .message } " , file = sys . stderr )
121
130
if isinstance (error , (errors .AttributeResolutionError , errors .SymbolResolutionError )) and error .suggestion :
122
- print ("Did you mean '{}'?" . format ( error . suggestion ) )
131
+ print (f "Did you mean '{ error . suggestion } '?", file = sys . stderr )
123
132
elif isinstance (error , errors .RegexSyntaxError ):
124
- print (" Regex: {!r}" . format ( error .error .pattern ) )
125
- print (" Details: {} at position {}" . format ( error .error .msg , error . error . pos ) )
133
+ print (f " Regex: { error .error .pattern !r } " , file = sys . stderr )
134
+ print (f " Details: { error . error . msg } at position { error .error .pos } " , file = sys . stderr )
126
135
elif isinstance (error , errors .FunctionCallError ):
127
- print (" Function: {!r}" . format ( error . function_name ) )
136
+ print (f " Function: { error . function_name !r} ", file = sys . stderr )
128
137
if debugging and error .error :
129
- inner_exception = '' .join (traceback .format_exception (
130
- error .error ,
131
- error .error ,
132
- error .error .__traceback__
133
- ))
134
- print (textwrap .indent (inner_exception , ' ' * 4 ))
138
+ inner_exception = color_tb .text (error .error .__class__ , error .error , error .error .__traceback__ )
139
+ print (textwrap .indent (inner_exception , ' ' * 2 ), file = sys .stderr )
135
140
if debugging :
136
- traceback . print_exc ( )
141
+ print ( color_tb . text ( error . __class__ , error , error . __traceback__ ), file = sys . stderr )
137
142
except Exception as error :
138
- traceback . print_exc ( )
143
+ print ( color_tb . text ( error . __class__ , error , error . __traceback__ ), file = sys . stderr )
139
144
else :
140
145
print ('result: ' )
141
146
pprint .pprint (result , indent = 4 )
0 commit comments