@@ -261,28 +261,10 @@ def natural_sort(list_to_sort: Iterable[str]) -> List[str]:
261
261
262
262
263
263
class StdSim (object ):
264
- """Class to simulate behavior of sys.stdout or sys.stderr.
265
-
264
+ """
265
+ Class to simulate behavior of sys.stdout or sys.stderr.
266
266
Stores contents in internal buffer and optionally echos to the inner stream it is simulating.
267
267
"""
268
- class ByteBuf (object ):
269
- """Inner class which stores an actual bytes buffer and does the actual output if echo is enabled."""
270
- def __init__ (self , inner_stream , echo : bool = False ,
271
- encoding : str = 'utf-8' , errors : str = 'replace' ) -> None :
272
- self .byte_buf = b''
273
- self .inner_stream = inner_stream
274
- self .echo = echo
275
- self .encoding = encoding
276
- self .errors = errors
277
-
278
- def write (self , b : bytes ) -> None :
279
- """Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
280
- if not isinstance (b , bytes ):
281
- raise TypeError ('a bytes-like object is required, not {}' .format (type (b )))
282
- self .byte_buf += b
283
- if self .echo :
284
- self .inner_stream .buffer .write (b )
285
-
286
268
def __init__ (self , inner_stream , echo : bool = False ,
287
269
encoding : str = 'utf-8' , errors : str = 'replace' ) -> None :
288
270
"""
@@ -292,17 +274,20 @@ def __init__(self, inner_stream, echo: bool = False,
292
274
:param encoding: codec for encoding/decoding strings (defaults to utf-8)
293
275
:param errors: how to handle encoding/decoding errors (defaults to replace)
294
276
"""
295
- self .buffer = self .ByteBuf (inner_stream , echo )
296
277
self .inner_stream = inner_stream
297
278
self .echo = echo
298
279
self .encoding = encoding
299
280
self .errors = errors
281
+ self .__store_output = True
282
+ self .buffer = ByteBuf (self )
300
283
301
284
def write (self , s : str ) -> None :
302
285
"""Add str to internal bytes buffer and if echo is True, echo contents to inner stream"""
303
286
if not isinstance (s , str ):
304
287
raise TypeError ('write() argument must be str, not {}' .format (type (s )))
305
- self .buffer .byte_buf += s .encode (encoding = self .encoding , errors = self .errors )
288
+
289
+ if self .__store_output :
290
+ self .buffer .byte_buf += s .encode (encoding = self .encoding , errors = self .errors )
306
291
if self .echo :
307
292
self .inner_stream .write (s )
308
293
@@ -330,13 +315,42 @@ def clear(self) -> None:
330
315
"""Clear the internal contents"""
331
316
self .buffer .byte_buf = b''
332
317
318
+ def get_store_output (self ) -> bool :
319
+ return self .__store_output
320
+
321
+ def set_store_output (self , store_output : bool ) -> None :
322
+ """
323
+ Set whether output should be saved in buffer.byte_buf
324
+ :param store_output: Store output if True, otherwise do not and clear the buffer
325
+ """
326
+ self .__store_output = self .buffer .store_output = store_output
327
+ self .clear ()
328
+
333
329
def __getattr__ (self , item : str ):
334
330
if item in self .__dict__ :
335
331
return self .__dict__ [item ]
336
332
else :
337
333
return getattr (self .inner_stream , item )
338
334
339
335
336
+ class ByteBuf (object ):
337
+ """
338
+ Used by StdSim to write binary data and stores the actual bytes written
339
+ """
340
+ def __init__ (self , std_sim_instance : StdSim ) -> None :
341
+ self .byte_buf = b''
342
+ self .std_sim_instance = std_sim_instance
343
+
344
+ def write (self , b : bytes ) -> None :
345
+ """Add bytes to internal bytes buffer and if echo is True, echo contents to inner stream."""
346
+ if not isinstance (b , bytes ):
347
+ raise TypeError ('a bytes-like object is required, not {}' .format (type (b )))
348
+ if self .std_sim_instance .get_store_output ():
349
+ self .byte_buf += b
350
+ if self .std_sim_instance .echo :
351
+ self .std_sim_instance .inner_stream .buffer .write (b )
352
+
353
+
340
354
def unquote_redirection_tokens (args : List [str ]) -> None :
341
355
"""
342
356
Unquote redirection tokens in a list of command-line arguments
0 commit comments