From d19c260940b840b2ac50ce5bee60e16046f9e3d6 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 16 Oct 2021 18:41:24 +0200 Subject: [PATCH] Simplify file access operations --- src/wireviz/Harness.py | 3 +-- src/wireviz/wv_cli.py | 6 ++---- src/wireviz/wv_html.py | 6 ++---- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 01038165..5acda02c 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -680,8 +680,7 @@ def output( # BOM output bomlist = bom_list(self.bom()) if "tsv" in fmt: - with open_file_write(f"{filename}.bom.tsv") as file: - file.write(tuplelist2tsv(bomlist)) + open_file_write(f"{filename}.bom.tsv").write(tuplelist2tsv(bomlist)) if "csv" in fmt: # TODO: implement CSV output (preferrably using CSV library) print("CSV output is not yet supported") diff --git a/src/wireviz/wv_cli.py b/src/wireviz/wv_cli.py index 83ff164b..73150720 100644 --- a/src/wireviz/wv_cli.py +++ b/src/wireviz/wv_cli.py @@ -107,8 +107,7 @@ def wireviz(file, format, prepend, output_dir, output_name, version): raise Exception(f"File does not exist:\n{prepend_file}") print("Prepend file:", prepend_file) - with open_file_read(prepend_file) as file_handle: - prepend_input += file_handle.read() + "\n" + prepend_input += open_file_read(prepend_file).read() + "\n" else: prepend_input = "" @@ -127,8 +126,7 @@ def wireviz(file, format, prepend, output_dir, output_name, version): "Output file: ", f"{Path(_output_dir / _output_name)}.{output_formats_str}" ) - with open_file_read(file) as file_handle: - yaml_input = file_handle.read() + yaml_input = open_file_read(file).read() file_dir = file.parent yaml_input = prepend_input + yaml_input diff --git a/src/wireviz/wv_html.py b/src/wireviz/wv_html.py index 6b85ef71..b30bdeeb 100644 --- a/src/wireviz/wv_html.py +++ b/src/wireviz/wv_html.py @@ -34,8 +34,7 @@ def generate_html_output( # fall back to built-in simple template if no template was provided templatefile = Path(__file__).parent / "templates/simple.html" - with open_file_read(templatefile) as file: - html = file.read() + html = open_file_read(templatefile).read() # embed SVG diagram with open_file_read(f"{filename}.svg") as file: @@ -117,5 +116,4 @@ def generate_html_output( pattern = re.compile("|".join(replacements_escaped)) html = pattern.sub(lambda match: replacements[match.group(0)], html) - with open_file_write(f"{filename}.html") as file: - file.write(html) + open_file_write(f"{filename}.html").write(html)