tfparse
tfparse copied to clipboard
configure logging
I would love a way to tell the logging to not report being unable to parse import blocks. While using tfparse, I run it in directories with many import blocks and it generates a lot of useless logging. I can ignore and delete the blocks once they are no longer needed, but it will still happen every once in a while.
I worked my way around this with a context manager:
@contextmanager
def suppress_stdout_stderr():
"""
Context manager to suppress stdout and stderr at the OS file descriptor level
"""
devnull = os.open(os.devnull, os.O_WRONLY)
old_stdout_fd = os.dup(1)
old_stderr_fd = os.dup(2)
try:
os.dup2(devnull, 1)
os.dup2(devnull, 2)
yield
finally:
os.dup2(old_stdout_fd, 1)
os.dup2(old_stderr_fd, 2)
os.close(devnull)
os.close(old_stdout_fd)
os.close(old_stderr_fd)
You can then use it like so:
with suppress_stdout_stderr():
data = load_from_path(self.path)