From d532da90710df33752bca88959efa61fbfe456cc Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 9 Apr 2023 16:04:31 +0200 Subject: [PATCH] Add Rich Progressbar Wrapper --- freqtrade/util/rich_progress.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 freqtrade/util/rich_progress.py diff --git a/freqtrade/util/rich_progress.py b/freqtrade/util/rich_progress.py new file mode 100644 index 000000000..7fe00b4d8 --- /dev/null +++ b/freqtrade/util/rich_progress.py @@ -0,0 +1,27 @@ +import logging +import sys +from contextlib import contextmanager + +from rich.progress import Progress + + +@contextmanager +def FtProgress(*args, **kwargs): + """ + Wrapper around rich.progress.Progress to fix issues with logging. + """ + try: + __logger = kwargs.pop('logger', None) + streamhandlers = [x for x in __logger.root.handlers if type(x) == logging.StreamHandler] + __prior_stderr = [] + + with Progress(*args, **kwargs) as progress: + for handler in streamhandlers: + __prior_stderr.append(handler.stream) + handler.setStream(sys.stderr) + + yield progress + + finally: + for idx, handler in enumerate(streamhandlers): + handler.setStream(__prior_stderr[idx])