1
1
import itertools
2
- import re
3
2
import sys
4
3
from collections import namedtuple
5
4
from traceback import format_tb
6
5
7
6
from pipenv import environments
8
7
from pipenv ._compat import decode_for_output
9
- from pipenv .patched import crayons
10
- from pipenv .vendor import vistir
8
+ from pipenv .vendor import click , vistir
11
9
from pipenv .vendor .click .exceptions import ClickException , FileError , UsageError
12
10
from pipenv .vendor .vistir .misc import echo as click_echo
13
11
14
- ANSI_REMOVAL_RE = re .compile (r"\033\[((?:\d|;)*)([a-zA-Z])" , re .MULTILINE )
15
- STRING_TYPES = ((str ,), crayons .ColoredString )
16
-
17
12
if sys .version_info [:2 ] >= (3 , 7 ):
18
13
KnownException = namedtuple (
19
14
"KnownException" ,
@@ -62,7 +57,7 @@ def handle_exception(exc_type, exception, traceback, hook=sys.excepthook):
62
57
63
58
64
59
class PipenvException (ClickException ):
65
- message = "{0}: {{0}}" .format (crayons . red ("ERROR" , bold = True ))
60
+ message = "{0}: {{0}}" .format (click . style ("ERROR" , fg = "red " , bold = True ))
66
61
67
62
def __init__ (self , message = None , ** kwargs ):
68
63
if not message :
@@ -76,15 +71,14 @@ def show(self, file=None):
76
71
if file is None :
77
72
file = vistir .misc .get_text_stderr ()
78
73
if self .extra :
79
- if isinstance (self .extra , STRING_TYPES ):
74
+ if isinstance (self .extra , str ):
80
75
self .extra = [self .extra ]
81
76
for extra in self .extra :
82
77
extra = "[pipenv.exceptions.{!s}]: {}" .format (
83
78
self .__class__ .__name__ , extra
84
79
)
85
- extra = decode_for_output (extra , file )
86
- click_echo (extra , file = file )
87
- click_echo (decode_for_output (f"{ self .message } " , file ), file = file )
80
+ click .echo (extra , file = file )
81
+ click .echo (f"{ self .message } " , file = file )
88
82
89
83
90
84
class PipenvCmdError (PipenvException ):
@@ -99,25 +93,24 @@ def __init__(self, cmd, out="", err="", exit_code=1):
99
93
def show (self , file = None ):
100
94
if file is None :
101
95
file = vistir .misc .get_text_stderr ()
102
- click_echo (
96
+ click . echo (
103
97
"{} {}" .format (
104
- crayons . red ("Error running command: " ),
105
- crayons . normal ( decode_for_output ( f"$ { self .cmd } " , file ) , bold = True ),
98
+ click . style ("Error running command: " , fg = "red " ),
99
+ click . style ( f"$ { self .cmd } " , bold = True ),
106
100
),
107
101
err = True ,
102
+ file = file ,
108
103
)
109
104
if self .out :
110
- click_echo (
111
- "{} {}" .format (
112
- crayons .normal ("OUTPUT: " ), decode_for_output (self .out , file )
113
- ),
105
+ click .echo (
106
+ "{} {}" .format ("OUTPUT: " , self .out ),
107
+ file = file ,
114
108
err = True ,
115
109
)
116
110
if self .err :
117
111
click_echo (
118
- "{} {}" .format (
119
- crayons .normal ("STDERR: " ), decode_for_output (self .err , file )
120
- ),
112
+ "{} {}" .format ("STDERR: " , self .err ),
113
+ file = file ,
121
114
err = True ,
122
115
)
123
116
@@ -131,14 +124,14 @@ def show(self, file=None):
131
124
if file is None :
132
125
file = vistir .misc .get_text_stderr ()
133
126
message = "{}\n {}" .format (
134
- crayons . normal ("Failed parsing JSON results:" , bold = True ),
127
+ click . style ("Failed parsing JSON results:" , bold = True ),
135
128
decode_for_output (self .message .strip (), file ),
136
129
)
137
130
click_echo (message , err = True )
138
131
if self .error_text :
139
132
click_echo (
140
133
"{} {}" .format (
141
- crayons . normal ("ERROR TEXT:" , bold = True ),
134
+ click . style ("ERROR TEXT:" , bold = True ),
142
135
decode_for_output (self .error_text , file ),
143
136
),
144
137
err = True ,
@@ -148,10 +141,10 @@ def show(self, file=None):
148
141
class PipenvUsageError (UsageError ):
149
142
def __init__ (self , message = None , ctx = None , ** kwargs ):
150
143
formatted_message = "{0}: {1}"
151
- msg_prefix = crayons . red ("ERROR:" , bold = True )
144
+ msg_prefix = click . style ("ERROR:" , fg = "red " , bold = True )
152
145
if not message :
153
146
message = "Pipenv encountered a problem and had to exit."
154
- message = formatted_message .format (msg_prefix , crayons . normal (message , bold = True ))
147
+ message = formatted_message .format (msg_prefix , click . style (message , bold = True ))
155
148
self .message = message
156
149
extra = kwargs .pop ("extra" , [])
157
150
UsageError .__init__ (self , decode_for_output (message ), ctx )
@@ -164,32 +157,34 @@ def show(self, file=None):
164
157
if self .ctx is not None :
165
158
color = self .ctx .color
166
159
if self .extra :
167
- if isinstance (self .extra , STRING_TYPES ):
160
+ if isinstance (self .extra , str ):
168
161
self .extra = [self .extra ]
169
162
for extra in self .extra :
170
163
if color :
171
- extra = getattr ( crayons , color , "blue" )( extra )
172
- click_echo ( decode_for_output ( extra , file ) , file = file )
164
+ extra = click . style ( extra , fg = color )
165
+ click . echo ( extra , file = file )
173
166
hint = ""
174
167
if self .cmd is not None and self .cmd .get_help_option (self .ctx ) is not None :
175
168
hint = 'Try "%s %s" for help.\n ' % (
176
169
self .ctx .command_path ,
177
170
self .ctx .help_option_names [0 ],
178
171
)
179
172
if self .ctx is not None :
180
- click_echo (self .ctx .get_usage () + "\n %s" % hint , file = file , color = color )
181
- click_echo (self .message , file = file )
173
+ click . echo (self .ctx .get_usage () + "\n %s" % hint , file = file , color = color )
174
+ click . echo (self .message , file = file )
182
175
183
176
184
177
class PipenvFileError (FileError ):
185
- formatted_message = "{0} {{0}} {{1}}" .format (crayons .red ("ERROR:" , bold = True ))
178
+ formatted_message = "{0} {{0}} {{1}}" .format (
179
+ click .style ("ERROR:" , fg = "red" , bold = True )
180
+ )
186
181
187
182
def __init__ (self , filename , message = None , ** kwargs ):
188
183
extra = kwargs .pop ("extra" , [])
189
184
if not message :
190
- message = crayons . normal ("Please ensure that the file exists!" , bold = True )
185
+ message = click . style ("Please ensure that the file exists!" , bold = True )
191
186
message = self .formatted_message .format (
192
- crayons . normal (f"{ filename } not found!" , bold = True ), message
187
+ click . style (f"{ filename } not found!" , bold = True ), message
193
188
)
194
189
FileError .__init__ (
195
190
self , filename = filename , hint = decode_for_output (message ), ** kwargs
@@ -200,7 +195,7 @@ def show(self, file=None):
200
195
if file is None :
201
196
file = vistir .misc .get_text_stderr ()
202
197
if self .extra :
203
- if isinstance (self .extra , STRING_TYPES ):
198
+ if isinstance (self .extra , str ):
204
199
self .extra = [self .extra ]
205
200
for extra in self .extra :
206
201
click_echo (decode_for_output (extra , file ), file = file )
@@ -211,8 +206,8 @@ class PipfileNotFound(PipenvFileError):
211
206
def __init__ (self , filename = "Pipfile" , extra = None , ** kwargs ):
212
207
extra = kwargs .pop ("extra" , [])
213
208
message = "{} {}" .format (
214
- crayons . red ("Aborting!" , bold = True ),
215
- crayons . normal (
209
+ click . style ("Aborting!" , bold = True , fg = "red" ),
210
+ click . style (
216
211
"Please ensure that the file exists and is located in your"
217
212
" project root directory." ,
218
213
bold = True ,
@@ -225,17 +220,17 @@ class LockfileNotFound(PipenvFileError):
225
220
def __init__ (self , filename = "Pipfile.lock" , extra = None , ** kwargs ):
226
221
extra = kwargs .pop ("extra" , [])
227
222
message = "{} {} {}" .format (
228
- crayons . normal ("You need to run" , bold = True ),
229
- crayons . red ("$ pipenv lock" , bold = True ),
230
- crayons . normal ("before you can continue." , bold = True ),
223
+ click . style ("You need to run" , bold = True ),
224
+ click . style ("$ pipenv lock" , bold = True , fg = "red" ),
225
+ click . style ("before you can continue." , bold = True ),
231
226
)
232
227
super ().__init__ (filename , message = message , extra = extra , ** kwargs )
233
228
234
229
235
230
class DeployException (PipenvUsageError ):
236
231
def __init__ (self , message = None , ** kwargs ):
237
232
if not message :
238
- message = str ( crayons . normal ("Aborting deploy" , bold = True ) )
233
+ message = click . style ("Aborting deploy" , bold = True )
239
234
extra = kwargs .pop ("extra" , [])
240
235
PipenvUsageError .__init__ (self , message = message , extra = extra , ** kwargs )
241
236
@@ -254,12 +249,12 @@ def __init__(self, option_name="system", message=None, ctx=None, **kwargs):
254
249
extra += [
255
250
"{}: --system is intended to be used for Pipfile installation, "
256
251
"not installation of specific packages. Aborting." .format (
257
- crayons . red ("Warning" , bold = True )
252
+ click . style ("Warning" , bold = True , fg = "red" )
258
253
),
259
254
]
260
255
if message is None :
261
- message = str (
262
- crayons . cyan ("See also: {}" . format ( crayons . normal ( "--deploy flag." )))
256
+ message = "{} --deploy flag" . format (
257
+ click . style ("See also: {}" , fg = "cyan" ),
263
258
)
264
259
super ().__init__ (option_name , message = message , ctx = ctx , extra = extra , ** kwargs )
265
260
@@ -296,14 +291,11 @@ def __init__(self, message=None, **kwargs):
296
291
message = "Failed to create virtual environment."
297
292
self .message = message
298
293
extra = kwargs .pop ("extra" , None )
299
- if extra is not None and isinstance (extra , STRING_TYPES ):
300
- # note we need the format interpolation because ``crayons.ColoredString``
301
- # is not an actual string type but is only a preparation for interpolation
302
- # so replacement or parsing requires this step
303
- extra = ANSI_REMOVAL_RE .sub ("" , f"{ extra } " )
294
+ if extra is not None and isinstance (extra , str ):
295
+ extra = click .unstyle (f"{ extra } " )
304
296
if "KeyboardInterrupt" in extra :
305
- extra = str (
306
- crayons . red ( "Virtualenv creation interrupted by user" , bold = True )
297
+ extra = click . style (
298
+ "Virtualenv creation interrupted by user" , fg = "red" , bold = True
307
299
)
308
300
self .extra = extra = [extra ]
309
301
VirtualenvException .__init__ (self , message , extra = extra )
@@ -313,16 +305,18 @@ class UninstallError(PipenvException):
313
305
def __init__ (self , package , command , return_values , return_code , ** kwargs ):
314
306
extra = [
315
307
"{} {}" .format (
316
- crayons . cyan ("Attempted to run command: " ),
317
- crayons . yellow (f"$ { command !r} " , bold = True ),
308
+ click . style ("Attempted to run command: " , fg = "cyan " ),
309
+ click . style (f"$ { command !r} " , bold = True , fg = "yellow" ),
318
310
)
319
311
]
320
- extra .extend ([crayons .cyan (line .strip ()) for line in return_values .splitlines ()])
312
+ extra .extend (
313
+ [click .style (line .strip (), fg = "cyan" ) for line in return_values .splitlines ()]
314
+ )
321
315
if isinstance (package , (tuple , list , set )):
322
316
package = " " .join (package )
323
317
message = "{!s} {!s}..." .format (
324
- crayons . normal ("Failed to uninstall package(s)" ),
325
- crayons . yellow (f"{ package } !s" , bold = True ),
318
+ click . style ("Failed to uninstall package(s)" , fg = "reset " ),
319
+ click . style (f"{ package } !s" , bold = True , fg = "yellow" ),
326
320
)
327
321
self .exit_code = return_code
328
322
PipenvException .__init__ (self , message = message , extra = extra )
@@ -334,10 +328,11 @@ def __init__(self, package, **kwargs):
334
328
package_message = ""
335
329
if package is not None :
336
330
package_message = "Couldn't install package: {}\n " .format (
337
- crayons . normal (f"{ package !s} " , bold = True )
331
+ click . style (f"{ package !s} " , bold = True )
338
332
)
339
333
message = "{} {}" .format (
340
- f"{ package_message } " , crayons .yellow ("Package installation failed..." )
334
+ f"{ package_message } " ,
335
+ click .style ("Package installation failed..." , fg = "yellow" ),
341
336
)
342
337
extra = kwargs .pop ("extra" , [])
343
338
PipenvException .__init__ (self , message = message , extra = extra , ** kwargs )
@@ -346,9 +341,9 @@ def __init__(self, package, **kwargs):
346
341
class CacheError (PipenvException ):
347
342
def __init__ (self , path , ** kwargs ):
348
343
message = "{} {}\n {}" .format (
349
- crayons . cyan ("Corrupt cache file" ),
350
- crayons . normal (f"{ path !s} " ),
351
- crayons . normal ('Consider trying "pipenv lock --clear" to clear the cache.' ),
344
+ click . style ("Corrupt cache file" , fg = "cyan " ),
345
+ click . style (f"{ path !s} " , fg = "reset" , bg = "reset " ),
346
+ click . style ('Consider trying "pipenv lock --clear" to clear the cache.' ),
352
347
)
353
348
PipenvException .__init__ (self , message = message )
354
349
@@ -357,9 +352,10 @@ class DependencyConflict(PipenvException):
357
352
def __init__ (self , message ):
358
353
extra = [
359
354
"{} {}" .format (
360
- crayons .red ("The operation failed..." , bold = True ),
361
- crayons .red (
362
- "A dependency conflict was detected and could not be resolved."
355
+ click .style ("The operation failed..." , bold = True , fg = "red" ),
356
+ click .style (
357
+ "A dependency conflict was detected and could not be resolved." ,
358
+ fg = "red" ,
363
359
),
364
360
)
365
361
]
@@ -375,21 +371,22 @@ def __init__(self, message, no_version_found=False):
375
371
"{} to inspect the situation.\n "
376
372
"Hint: try {} if it is a pre-release dependency."
377
373
"" .format (
378
- crayons . red ("Warning" , bold = True ),
379
- crayons . yellow ("$ pipenv install --skip-lock" ),
380
- crayons . yellow ("$ pipenv graph" ),
381
- crayons . yellow ("$ pipenv lock --pre" ),
374
+ click . style ("Warning" , fg = "red " , bold = True ),
375
+ click . style ("$ pipenv install --skip-lock" , fg = "yellow " ),
376
+ click . style ("$ pipenv graph" , fg = "yellow " ),
377
+ click . style ("$ pipenv lock --pre" , fg = "yellow " ),
382
378
),
383
379
)
384
380
if "no version found at all" in message :
385
381
no_version_found = True
386
- message = crayons . yellow (f"{ message } " )
382
+ message = click . style (f"{ message } " , fg = "yellow " )
387
383
if no_version_found :
388
384
message = "{}\n {}" .format (
389
385
message ,
390
- crayons . cyan (
386
+ click . style (
391
387
"Please check your version specifier and version number. "
392
- "See PEP440 for more information."
388
+ "See PEP440 for more information." ,
389
+ fg = "cyan" ,
393
390
),
394
391
)
395
392
PipenvException .__init__ (self , message , extra = extra )
@@ -428,9 +425,11 @@ def __init__(self, req=None):
428
425
req_value = "\n " .join ([f" { k } : { v } " for k , v in values ])
429
426
else :
430
427
req_value = getattr (req .line_instance , "line" , None )
431
- message = "{} {}" .format (
432
- crayons .normal (decode_for_output ("Failed creating requirement instance" )),
433
- crayons .normal (decode_for_output (f"{ req_value !r} " )),
428
+ message = click .style (
429
+ f"Failed creating requirement instance { req_value } " ,
430
+ bold = False ,
431
+ fg = "reset" ,
432
+ bg = "reset" ,
434
433
)
435
434
extra = [str (req )]
436
435
PipenvException .__init__ (self , message , extra = extra )
0 commit comments