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