|
15 | 15 | import logging |
16 | 16 | import optparse |
17 | 17 | import os |
| 18 | +import pathlib |
18 | 19 | import re |
19 | 20 | import shutil |
20 | 21 | import sys |
@@ -201,44 +202,34 @@ def run(self): |
201 | 202 | self.log.error('%d errors encountered.', n_errors) |
202 | 203 | return (1 if n_errors else 0) |
203 | 204 |
|
204 | | - def _run_domain(self, domain): |
205 | | - po_files = [] |
206 | | - mo_files = [] |
207 | | - |
| 205 | + def _get_po_mo_triples(self, domain: str): |
208 | 206 | if not self.input_file: |
| 207 | + dir_path = pathlib.Path(self.directory) |
209 | 208 | if self.locale: |
210 | | - po_files.append((self.locale, |
211 | | - os.path.join(self.directory, self.locale, |
212 | | - 'LC_MESSAGES', |
213 | | - f"{domain}.po"))) |
214 | | - mo_files.append(os.path.join(self.directory, self.locale, |
215 | | - 'LC_MESSAGES', |
216 | | - f"{domain}.mo")) |
| 209 | + lc_messages_path = dir_path / self.locale / "LC_MESSAGES" |
| 210 | + po_file = lc_messages_path / f"{domain}.po" |
| 211 | + yield self.locale, po_file, po_file.with_suffix(".mo") |
217 | 212 | else: |
218 | | - for locale in os.listdir(self.directory): |
219 | | - po_file = os.path.join(self.directory, locale, |
220 | | - 'LC_MESSAGES', f"{domain}.po") |
221 | | - if os.path.exists(po_file): |
222 | | - po_files.append((locale, po_file)) |
223 | | - mo_files.append(os.path.join(self.directory, locale, |
224 | | - 'LC_MESSAGES', |
225 | | - f"{domain}.mo")) |
| 213 | + for locale_path in dir_path.iterdir(): |
| 214 | + po_file = locale_path / "LC_MESSAGES"/ f"{domain}.po" |
| 215 | + if po_file.exists(): |
| 216 | + yield locale_path.name, po_file, po_file.with_suffix(".mo") |
226 | 217 | else: |
227 | | - po_files.append((self.locale, self.input_file)) |
| 218 | + po_file = pathlib.Path(self.input_file) |
228 | 219 | if self.output_file: |
229 | | - mo_files.append(self.output_file) |
| 220 | + mo_file = pathlib.Path(self.output_file) |
230 | 221 | else: |
231 | | - mo_files.append(os.path.join(self.directory, self.locale, |
232 | | - 'LC_MESSAGES', |
233 | | - f"{domain}.mo")) |
| 222 | + mo_file = pathlib.Path(self.directory) / self.locale / "LC_MESSAGES" / f"{domain}.mo" |
| 223 | + yield self.locale, po_file, mo_file |
234 | 224 |
|
235 | | - if not po_files: |
236 | | - raise OptionError('no message catalogs found') |
| 225 | + def _run_domain(self, domain): |
| 226 | + locale_po_mo_triples = list(self._get_po_mo_triples(domain)) |
| 227 | + if not locale_po_mo_triples: |
| 228 | + raise OptionError(f'no message catalogs found for domain {domain!r}') |
237 | 229 |
|
238 | 230 | catalogs_and_errors = {} |
239 | 231 |
|
240 | | - for idx, (locale, po_file) in enumerate(po_files): |
241 | | - mo_file = mo_files[idx] |
| 232 | + for locale, po_file, mo_file in locale_po_mo_triples: |
242 | 233 | with open(po_file, 'rb') as infile: |
243 | 234 | catalog = read_po(infile, locale) |
244 | 235 |
|
@@ -622,8 +613,8 @@ def finalize_options(self): |
622 | 613 | if not self.output_file and not self.output_dir: |
623 | 614 | raise OptionError('you must specify the output directory') |
624 | 615 | if not self.output_file: |
625 | | - self.output_file = os.path.join(self.output_dir, self.locale, |
626 | | - 'LC_MESSAGES', f"{self.domain}.po") |
| 616 | + lc_messages_path = pathlib.Path(self.output_dir) / self.locale / "LC_MESSAGES" |
| 617 | + self.output_file = str(lc_messages_path / f"{self.domain}.po") |
627 | 618 |
|
628 | 619 | if not os.path.exists(os.path.dirname(self.output_file)): |
629 | 620 | os.makedirs(os.path.dirname(self.output_file)) |
@@ -744,36 +735,35 @@ def finalize_options(self): |
744 | 735 | if self.no_fuzzy_matching and self.previous: |
745 | 736 | self.previous = False |
746 | 737 |
|
747 | | - def run(self): |
748 | | - check_status = {} |
749 | | - po_files = [] |
| 738 | + def _get_locale_po_file_tuples(self): |
750 | 739 | if not self.output_file: |
| 740 | + output_path = pathlib.Path(self.output_dir) |
751 | 741 | if self.locale: |
752 | | - po_files.append((self.locale, |
753 | | - os.path.join(self.output_dir, self.locale, |
754 | | - 'LC_MESSAGES', |
755 | | - f"{self.domain}.po"))) |
| 742 | + lc_messages_path = output_path / self.locale / "LC_MESSAGES" |
| 743 | + yield self.locale, str(lc_messages_path / f"{self.domain}.po") |
756 | 744 | else: |
757 | | - for locale in os.listdir(self.output_dir): |
758 | | - po_file = os.path.join(self.output_dir, locale, |
759 | | - 'LC_MESSAGES', |
760 | | - f"{self.domain}.po") |
761 | | - if os.path.exists(po_file): |
762 | | - po_files.append((locale, po_file)) |
| 745 | + for locale_path in output_path.iterdir(): |
| 746 | + po_file = locale_path / "LC_MESSAGES" / f"{self.domain}.po" |
| 747 | + if po_file.exists(): |
| 748 | + yield locale_path.stem, po_file |
763 | 749 | else: |
764 | | - po_files.append((self.locale, self.output_file)) |
765 | | - |
766 | | - if not po_files: |
767 | | - raise OptionError('no message catalogs found') |
| 750 | + yield self.locale, self.output_file |
768 | 751 |
|
| 752 | + def run(self): |
769 | 753 | domain = self.domain |
770 | 754 | if not domain: |
771 | 755 | domain = os.path.splitext(os.path.basename(self.input_file))[0] |
772 | 756 |
|
| 757 | + check_status = {} |
| 758 | + locale_po_file_tuples = list(self._get_locale_po_file_tuples()) |
| 759 | + |
| 760 | + if not locale_po_file_tuples: |
| 761 | + raise OptionError(f'no message catalogs found for domain {domain!r}') |
| 762 | + |
773 | 763 | with open(self.input_file, 'rb') as infile: |
774 | 764 | template = read_po(infile) |
775 | 765 |
|
776 | | - for locale, filename in po_files: |
| 766 | + for locale, filename in locale_po_file_tuples: |
777 | 767 | if self.init_missing and not os.path.exists(filename): |
778 | 768 | if self.check: |
779 | 769 | check_status[filename] = False |
|
0 commit comments