Skip to content

Commit 9fbce3b

Browse files
17o2kvid
andcommitted
Avoid ResourceWarning: unclosed file (#395)
A number of such warnings showed up when running e.g. PYTHONWARNINGS=always python build_examples.py PYTHONWARNINGS=always wireviz ../../examples/demo0?.yml See #309 (comment) Fix: All open() calls should be in a "with open() as x" statement to ensure closing the file when exiting the block in any way. Otherwise, use the new file_read_text() or file_write_text() functions to read or write the whole utf-8 text file and closing it. Co-authored-by: kvid <kvid@users.noreply.github.com>
1 parent 38ca3e2 commit 9fbce3b

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

src/wireviz/wireviz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from wireviz.wv_harness import Harness
1717
from wireviz.wv_utils import (
1818
expand,
19+
file_read_text,
1920
get_single_key_and_value,
2021
is_arrow,
21-
open_file_read,
2222
smart_file_resolve,
2323
)
2424

@@ -419,7 +419,7 @@ def _get_yaml_data_and_path(inp: Union[str, Path, Dict]) -> Tuple[Dict, Path]:
419419
try:
420420
yaml_path = Path(inp).expanduser().resolve(strict=True)
421421
# if no FileNotFoundError exception happens, get file contents
422-
yaml_str = open_file_read(yaml_path).read()
422+
yaml_str = file_read_text(yaml_path)
423423
except (FileNotFoundError, OSError, ValueError) as e:
424424
# if inp is a long YAML string, Pathlib will normally raise
425425
# FileNotFoundError or OSError(errno = ENAMETOOLONG) when

src/wireviz/wv_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import wireviz.wireviz as wv
1313
from wireviz import APP_NAME, __version__
14-
from wireviz.wv_utils import open_file_read
14+
from wireviz.wv_utils import file_read_text
1515

1616
format_codes = {
1717
# "c": "csv", # TODO: support CSV
@@ -119,7 +119,7 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
119119
raise Exception(f"Path is not a file:\n{prepend_file}")
120120
print("Prepend file:", prepend_file)
121121

122-
prepend_input += open_file_read(prepend_file).read() + "\n"
122+
prepend_input += file_read_text(prepend_file) + "\n"
123123
else:
124124
prepend_input = ""
125125

@@ -140,7 +140,7 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
140140
"Output file: ", f"{Path(_output_dir / _output_name)}.{output_formats_str}"
141141
)
142142

143-
yaml_input = open_file_read(file).read()
143+
yaml_input = file_read_text(file)
144144
file_dir = file.parent
145145

146146
yaml_input = prepend_input + yaml_input

src/wireviz/wv_harness.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
embed_svg_images_file,
4141
generate_html_output,
4242
)
43-
from wireviz.wv_utils import OLD_CONNECTOR_ATTR, bom2tsv, check_old, open_file_write
43+
from wireviz.wv_utils import OLD_CONNECTOR_ATTR, bom2tsv, check_old, file_write_text
4444

4545

4646
@dataclass
@@ -424,7 +424,8 @@ def output(
424424
# bomlist = [[]]
425425
if "tsv" in fmt:
426426
tsv = bom2tsv(bomlist)
427-
open_file_write(f"{filename}.tsv").write(tsv)
427+
file_write_text(f"{filename}.bom.tsv", tsv)
428+
428429
if "csv" in fmt:
429430
# TODO: implement CSV output (preferrably using CSV library)
430431
print("CSV output is not yet supported")

src/wireviz/wv_output.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from wireviz import APP_NAME, APP_URL, __version__
1010
from wireviz.wv_dataclasses import Metadata, Options
1111
from wireviz.wv_utils import (
12+
file_read_text,
13+
file_write_text,
1214
html_line_breaks,
13-
open_file_read,
14-
open_file_write,
1515
smart_file_resolve,
1616
)
1717

@@ -95,14 +95,14 @@ def generate_html_output(
9595
# fall back to built-in simple template if no template was provided
9696
templatefile = Path(wireviz.__file__).parent / "templates/simple.html"
9797

98-
html = open_file_read(templatefile).read()
98+
html = file_read_text(templatefile)
9999

100100
# embed SVG diagram (only if used)
101101
def svgdata() -> str:
102102
return re.sub(
103103
"^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>",
104104
"<!-- XML and DOCTYPE declarations from SVG file removed -->",
105-
open_file_read(f"{filename}.tmp.svg").read(),
105+
file_read_text(f"{filename}.tmp.svg"),
106106
1,
107107
)
108108

@@ -186,4 +186,4 @@ def replacement_if_used(key: str, func: Callable[[], str]) -> None:
186186
pattern = re.compile("|".join(replacements_escaped))
187187
html = pattern.sub(lambda match: replacements[match.group(0)], html)
188188

189-
open_file_write(f"{filename}.html").write(html)
189+
file_write_text(f"{filename}.html", html)

src/wireviz/wv_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,31 @@ def clean_whitespace(inp):
151151

152152

153153
def open_file_read(filename):
154+
"""Open utf-8 encoded text file for reading - remember closing it when finished"""
154155
# TODO: Intelligently determine encoding
155156
return open(filename, "r", encoding="UTF-8")
156157

157158

158159
def open_file_write(filename):
160+
"""Open utf-8 encoded text file for writing - remember closing it when finished"""
159161
return open(filename, "w", encoding="UTF-8")
160162

161163

162164
def open_file_append(filename):
165+
"""Open utf-8 encoded text file for appending - remember closing it when finished"""
163166
return open(filename, "a", encoding="UTF-8")
164167

165168

169+
def file_read_text(filename: str) -> str:
170+
"""Read utf-8 encoded text file, close it, and return the text"""
171+
return Path(filename).read_text(encoding="utf-8")
172+
173+
174+
def file_write_text(filename: str, text: str) -> int:
175+
"""Write utf-8 encoded text file, close it, and return the number of characters written"""
176+
return Path(filename).write_text(text, encoding="utf-8")
177+
178+
166179
def is_arrow(inp):
167180
"""
168181
Matches strings of one or multiple `-` or `=` (but not mixed)

0 commit comments

Comments
 (0)