Skip to content

Commit b572235

Browse files
authored
locale: Use context manager for opening files in locale/grass_po_stats (OSGeo#6192)
1 parent 71d952f commit b572235

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

locale/grass_po_stats.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#
1515
#############################################################################
1616

17+
from __future__ import annotations
18+
1719
import codecs
1820
import glob
1921
import json
@@ -72,13 +74,14 @@ def read_msgfmt_statistics(msg, lgood, lfuzzy, lbad):
7274
return langdict, lgood, lfuzzy, lbad
7375

7476

75-
def langDefinition(fil):
76-
f = codecs.open(fil, encoding="utf-8", errors="replace", mode="r")
77-
for line in f.readlines():
78-
if '"Language-Team:' in line:
79-
lang = line.split(" ")[1:-1]
80-
break
81-
f.close()
77+
def langDefinition(fil: str) -> str:
78+
lang: str | list[str] = ""
79+
with codecs.open(fil, encoding="utf-8", errors="replace", mode="r") as f:
80+
for line in f.readlines():
81+
if '"Language-Team:' in line:
82+
lang = line.split(" ")[1:-1]
83+
break
84+
8285
if len(lang) == 2:
8386
return " ".join(lang)
8487
if len(lang) == 1:
@@ -139,10 +142,9 @@ def writejson(stats, outfile):
139142
# write a string with pretty style
140143
outjson = os.linesep.join([line.rstrip() for line in fjson.splitlines()])
141144
# write out file
142-
fout = open(outfile, "w")
143-
fout.write(outjson)
144-
fout.write(os.linesep)
145-
fout.close()
145+
with open(outfile, "w", encoding="utf-8") as fout:
146+
fout.write(outjson)
147+
fout.write(os.linesep)
146148
try:
147149
os.remove("messages.mo")
148150
except OSError:

0 commit comments

Comments
 (0)