Permalink
Browse files

making sure output capturing is only enabled when we have a running p…

…rogress bar
  • Loading branch information...
WoLpH committed Jul 28, 2017
1 parent bbba9f4 commit 4eb44b211702b682c1735f84f0f5403655d6d3cd
Showing with 54 additions and 11 deletions.
  1. +8 −0 README.rst
  2. +2 −1 progressbar/bar.py
  3. +44 −10 progressbar/utils.py
View
@@ -118,6 +118,14 @@ environment variable, on Linux/Unix systems this can be done through:
# WRAP_STDERR=true python your_script.py
If you need to flush manually while wrapping, you can do so using:
.. code:: python
import progressbar
progressbar.streams.flush()
In most cases the following will work as well, as long as you initialize the
`StreamHandler` after the wrapping has taken place.
View
@@ -123,6 +123,7 @@ def start(self, *args, **kwargs):
self.stdout = utils.streams.stdout
self.stderr = utils.streams.stderr
utils.streams.start_capturing()
DefaultFdMixin.start(self, *args, **kwargs)
def update(self, value=None):
@@ -132,7 +133,7 @@ def update(self, value=None):
def finish(self, end='\n'):
DefaultFdMixin.finish(self, end=end)
utils.streams.flush()
utils.streams.stop_capturing()
if self.redirect_stdout:
utils.streams.unwrap_stdout()
View
@@ -13,6 +13,25 @@
epoch = datetime.datetime(year=1970, month=1, day=1)
class WrappingIO:
def __init__(self, target, capturing=False):
self.buffer = six.StringIO()
self.target = target
self.capturing = capturing
def write(self, value):
if self.capturing:
self.buffer.write(value)
else:
self.target.write(value)
def flush(self):
self.target.write(self.buffer.getvalue())
self.buffer.seek(0)
self.buffer.truncate(0)
class StreamWrapper(object):
'''Wrap stdout and stderr globally'''
@@ -23,13 +42,32 @@ def __init__(self):
self.wrapped_stdout = 0
self.wrapped_stderr = 0
self.wrapped_excepthook = 0
self.capturing = 0
if os.environ.get('WRAP_STDOUT'): # pragma: no cover
self.wrap_stdout()
if os.environ.get('WRAP_STDERR'): # pragma: no cover
self.wrap_stderr()
def start_capturing(self):
self.capturing += 1
self.update_capturing()
def stop_capturing(self):
self.capturing -= 1
self.update_capturing()
def update_capturing(self): # pragma: no cover
if isinstance(self.stdout, WrappingIO):
self.stdout.capturing = self.capturing > 0
if isinstance(self.stderr, WrappingIO):
self.stderr.capturing = self.capturing > 0
if self.capturing <= 0:
self.flush()
def wrap(self, stdout=False, stderr=False):
if stdout:
self.wrap_stdout()
@@ -41,7 +79,7 @@ def wrap_stdout(self):
self.wrap_excepthook()
if not self.wrapped_stdout:
self.stdout = sys.stdout = six.StringIO()
self.stdout = sys.stdout = WrappingIO(self.original_stdout)
self.wrapped_stdout += 1
return sys.stdout
@@ -50,7 +88,7 @@ def wrap_stderr(self):
self.wrap_excepthook()
if not self.wrapped_stderr:
self.stderr = sys.stderr = six.StringIO()
self.stderr = sys.stderr = WrappingIO(self.original_stderr)
self.wrapped_stderr += 1
return sys.stderr
@@ -88,22 +126,18 @@ def unwrap_stderr(self):
self.wrapped_stderr = 0
def flush(self):
if self.wrapped_stdout:
if self.wrapped_stdout: # pragma: no branch
try:
self.original_stdout.write(self.stdout.getvalue())
self.stdout.seek(0)
self.stdout.truncate(0)
self.stdout.flush()
except (io.UnsupportedOperation,
AttributeError): # pragma: no cover
self.wrapped_stdout = False
logger.warn('Disabling stdout redirection, %r is not seekable',
sys.stdout)
if self.wrapped_stderr:
if self.wrapped_stderr: # pragma: no branch
try:
self.original_stderr.write(self.stderr.getvalue())
self.stderr.seek(0)
self.stderr.truncate(0)
self.stderr.flush()
except (io.UnsupportedOperation,
AttributeError): # pragma: no cover
self.wrapped_stderr = False

0 comments on commit 4eb44b2

Please sign in to comment.