source/tools/black/__main__.py

52 lines
1.1 KiB
Python
Raw Normal View History

2021-09-03 04:24:50 +00:00
"""A shim to black which knows how to tee output for --output-file."""
2021-09-03 04:10:12 +00:00
import argparse
import sys
from black import nullcontext, patched_main
2021-09-03 04:10:12 +00:00
parser = argparse.ArgumentParser()
parser.add_argument("--output-file", default=None)
class Tee(object):
2021-09-03 04:24:50 +00:00
"""Something that looks like a File/Writeable but does teed writes."""
2021-09-03 04:10:12 +00:00
def __init__(self, name, mode):
self._file = open(name, mode)
self._stdout = sys.stdout
def __enter__(self):
sys.stdout = self
return self
def __exit__(self, *args, **kwargs):
sys.stdout = self._stdout
self.close()
def write(self, data):
self._file.write(data)
self._stdout.write(data)
def flush(self):
self._file.flush()
self._stdout.flush()
def close(self):
self._file.close()
if __name__ == "__main__":
opts, args = parser.parse_known_args()
if opts.output_file:
print("Teeig output....")
ctx = Tee(opts.output_file, "w")
else:
2021-09-03 04:24:50 +00:00
ctx = nullcontext()
2021-09-03 04:10:12 +00:00
with ctx:
sys.argv = [sys.argv[0]] + args
patched_main()