Skip to content

Commit 45b10e4

Browse files
committed
Use to close file handles
1 parent 32b8d48 commit 45b10e4

5 files changed

Lines changed: 20 additions & 15 deletions

File tree

build_parameters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ def replace_anchors(source_file, dest_file, version_tag):
350350
For each parameter file generate by param_parse.py, it inserts a version tag in anchors
351351
to do not make confusing in sphinx toctrees.
352352
"""
353-
file_in = open(source_file, "r")
354-
file_out = open(dest_file, "w")
353+
file_in = open(source_file, "r") # noqa: SIM115
354+
file_out = open(dest_file, "w") # noqa: SIM115
355355
found_original_title = False
356356
if "latest" not in version_tag:
357357
file_out.write(':orphan:\n\n')

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ ignore-words-list = "bu,collets,diamon,dout,empy,extint,fram,inh,minite,ned,parm
44
skip = "ARCHIVED/*,LICENSE,*.ai,*.pdf,*.svg"
55

66
[tool.ruff]
7-
target-version = "py310"
7+
target-version = "py38"
88
line-length = 127
99
lint.extend-select = [
1010
"FURB", # refurb
1111
"Q003", # avoidable-escaped-quote
12+
"SIM115", # open-file-with-context-handler
1213
"UP031", # printf-string-formatting
1314
"UP032", # f-string
1415
]

scripts/cap_params.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
for f in args.files:
1616
print(f"Processing {f}")
17-
txt = open(f, 'r').read()
17+
with open(f, 'r') as in_file:
18+
txt = in_file.read()
1819
matches = re.findall(r'[,.\s][A-Z][A-Z0-9]+_[A-Z_]+[,.\s]', txt)
1920
matches = re.findall(r'[,.\s][A-Z]+_[A-Z_]+[,.\s]', txt)
2021
changed = False
@@ -29,4 +30,5 @@
2930
else:
3031
print(f"Found [{s}]")
3132
if changed:
32-
open(f, 'w').write(txt)
33+
with open(f, 'w') as out_file:
34+
out_file.write(txt)

scripts/rename_params.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626

2727
def load_param_map(fname):
28-
lines = open(fname, 'r').readlines()
28+
with open(fname, 'r') as in_file:
29+
lines = in_file.readlines()
2930
ret = {}
3031
for line in lines:
3132
if line.startswith("#"):
@@ -47,7 +48,8 @@ def process_file(fname, param_map):
4748
print(f"Skipping common file {fname}")
4849
return
4950
needs_write = False
50-
txt = open(fname, "r").read()
51+
with open(fname, "r") as in_file:
52+
txt = in_file.read()
5153

5254
replacements = [":ref:`PARAMNAME <PARAMNAME>`",
5355
":ref:`PARAMNAME<PARAMNAME>`"]
@@ -65,7 +67,8 @@ def process_file(fname, param_map):
6567
if not needs_write:
6668
return
6769
print(f"Updating {fname}")
68-
open(fname, "w").write(txt)
70+
with open(fname, "w") as out_file:
71+
out_file.write(txt)
6972

7073

7174
param_map = load_param_map(args.param_map)

update.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,8 @@ def copy_common_source_files(start_dir=COMMON_DIR, clean_common=False):
479479
if file.endswith(".rst"):
480480
# debug(" FILE: %s" % file)
481481
source_file_path = os.path.join(root, file)
482-
source_file = open(source_file_path, 'r', encoding='utf-8')
483-
source_content = source_file.read()
484-
source_file.close()
482+
with open(source_file_path, 'r', encoding='utf-8') as source_file:
483+
source_content = source_file.read()
485484
targets = get_copy_targets(source_content)
486485
for wiki in targets:
487486
content = strip_content(source_content, wiki)
@@ -512,9 +511,8 @@ def copy_common_source_files(start_dir=COMMON_DIR, clean_common=False):
512511
shutil.copy2(src, dst)
513512
elif file.endswith(".js"):
514513
source_file_path = os.path.join(root, file)
515-
source_file = open(source_file_path, 'r', encoding='utf-8')
516-
source_content = source_file.read()
517-
source_file.close()
514+
with open(source_file_path, 'r', encoding='utf-8') as source_file:
515+
source_content = source_file.read()
518516
targets = get_copy_targets(source_content)
519517
for wiki in targets:
520518
content = strip_content(source_content, wiki)
@@ -885,7 +883,8 @@ def create_features_pages(site):
885883
# fetch and load most-recently-built features.json
886884
remove_if_exists("features.json.gz")
887885
fetch_url("https://firmware.ardupilot.org/features.json.gz")
888-
features_json = json.load(gzip.open("features.json.gz"))
886+
with gzip.open("features.json.gz") as in_file:
887+
features_json = json.load(in_file)
889888
if features_json["format-version"] != "1.0.0":
890889
progress("bad format version")
891890
return

0 commit comments

Comments
 (0)