Skip to content

Commit 115d413

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "IMPR: replace codecs.open() with pathlib methods in make_i18n_dict.py"
2 parents 864b728 + d59736e commit 115d413

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

scripts/maintenance/make_i18n_dict.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@
3030
>>> bot.to_json()
3131
"""
3232
#
33-
# (C) Pywikibot team, 2013-2024
33+
# (C) Pywikibot team, 2013-2025
3434
#
3535
# Distributed under the terms of the MIT license.
3636
#
3737
from __future__ import annotations
3838

39-
import codecs
4039
import json
41-
import os
4240
from importlib import import_module
41+
from pathlib import Path
4342

4443
from pywikibot import config
4544

@@ -132,24 +131,23 @@ def to_json(self, quiet=True):
132131

133132
if not self.dict:
134133
self.run(quiet)
135-
json_dir = os.path.join(
136-
config.base_dir, 'scripts/i18n', self.scriptname)
137-
if not os.path.exists(json_dir):
138-
os.makedirs(json_dir)
134+
json_dir = Path(config.base_dir, 'scripts/i18n', self.scriptname)
135+
json_dir.mkdir(exist_ok=True)
136+
139137
for lang in self.dict:
140-
file_name = os.path.join(json_dir, f'{lang}.json')
141-
if os.path.isfile(file_name):
142-
with codecs.open(file_name, 'r', 'utf-8') as json_file:
143-
new_dict = json.load(json_file)
144-
else:
145-
new_dict = {}
138+
new_dict = {}
139+
140+
file_path = json_dir / f'{lang}.json'
141+
if file_path.is_file():
142+
new_dict = json.load(file_path.read_text(encoding='utf-8'))
143+
146144
new_dict['@metadata'] = new_dict.get('@metadata', {'authors': []})
147-
with codecs.open(file_name, 'w', 'utf-8') as json_file:
148-
new_dict.update(self.dict[lang])
149-
s = json.dumps(new_dict, ensure_ascii=False, sort_keys=True,
150-
indent=indent, separators=(',', ': '))
151-
s = s.replace(' ' * indent, '\t')
152-
json_file.write(s)
145+
new_dict.update(self.dict[lang])
146+
s = json.dumps(new_dict, ensure_ascii=False, sort_keys=True,
147+
indent=indent, separators=(',', ': '))
148+
s = s.replace(' ' * indent, '\t')
149+
150+
file_path.write_text(s, encoding='utf-8')
153151

154152

155153
if __name__ == '__main__':

0 commit comments

Comments
 (0)