|
30 | 30 | >>> bot.to_json() |
31 | 31 | """ |
32 | 32 | # |
33 | | -# (C) Pywikibot team, 2013-2024 |
| 33 | +# (C) Pywikibot team, 2013-2025 |
34 | 34 | # |
35 | 35 | # Distributed under the terms of the MIT license. |
36 | 36 | # |
37 | 37 | from __future__ import annotations |
38 | 38 |
|
39 | | -import codecs |
40 | 39 | import json |
41 | | -import os |
42 | 40 | from importlib import import_module |
| 41 | +from pathlib import Path |
43 | 42 |
|
44 | 43 | from pywikibot import config |
45 | 44 |
|
@@ -132,24 +131,23 @@ def to_json(self, quiet=True): |
132 | 131 |
|
133 | 132 | if not self.dict: |
134 | 133 | 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 | + |
139 | 137 | 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 | + |
146 | 144 | 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') |
153 | 151 |
|
154 | 152 |
|
155 | 153 | if __name__ == '__main__': |
|
0 commit comments