From ce0ae694518ca5f6d2c2550390ca9cfd206d5796 Mon Sep 17 00:00:00 2001 From: Jeremy Weinstein Date: Fri, 4 Mar 2022 15:40:18 -0800 Subject: [PATCH] feat: add exclude_paths option to translations filter --- grow/commands/shared.py | 8 ++++++++ grow/commands/subcommands/translations_filter.py | 5 +++-- grow/translations/catalog_holder.py | 6 ++++-- grow/translations/catalog_holder_test.py | 9 +++++++++ grow/translations/catalogs.py | 4 +++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/grow/commands/shared.py b/grow/commands/shared.py index 617854c5d..471c3b518 100644 --- a/grow/commands/shared.py +++ b/grow/commands/shared.py @@ -24,6 +24,14 @@ def _decorator(func): return _decorator +def exclude_path_option(func): + """Option for excluding a path for extraction.""" + return click.option( + '--exclude-path', + type=str, multiple=True, + help='Which paths to exclude from the generated messages file.')(func) + + def force_untranslated_option(config): """Option for forcing untranslated string deployment.""" shared_default = CFG.get('force-untranslated', False) diff --git a/grow/commands/subcommands/translations_filter.py b/grow/commands/subcommands/translations_filter.py index 4d9a7f00e..336793eff 100644 --- a/grow/commands/subcommands/translations_filter.py +++ b/grow/commands/subcommands/translations_filter.py @@ -31,7 +31,8 @@ ' The path must be relative to the pod\'s root. This option' ' is only applicable when using --localized.')) @shared.path_option -def translations_filter(pod_path, locale, o, include_obsolete, localized, path, +@shared.exclude_path_option +def translations_filter(pod_path, locale, o, include_obsolete, localized, path, exclude_path, include_header, out_dir, f): """Filters untranslated messages from catalogs into new catalogs.""" root = os.path.abspath(os.path.join(os.getcwd(), pod_path)) @@ -47,6 +48,6 @@ def translations_filter(pod_path, locale, o, include_obsolete, localized, path, 'specified directory.'.format(out_dir)) catalogs.filter(out_path=o, out_dir=out_dir, include_obsolete=include_obsolete, - localized=localized, paths=path, + localized=localized, paths=path, exclude_paths=exclude_path, include_header=include_header, locales=locale) return pod diff --git a/grow/translations/catalog_holder.py b/grow/translations/catalog_holder.py index 47380dfff..68846bddc 100644 --- a/grow/translations/catalog_holder.py +++ b/grow/translations/catalog_holder.py @@ -568,17 +568,19 @@ def get_gettext_translations(self, locale): def filter(self, out_path=None, out_dir=None, include_obsolete=True, localized=False, - paths=None, include_header=None, locales=None): + paths=None, exclude_paths=None, include_header=None, locales=None): if localized and out_dir is None: raise UsageError('Must specify --out_dir when using --localized in ' 'order to generate localized catalogs.') if not localized and out_path is None: raise UsageError('Must specify -o when not using --localized.') + if not locales: + raise UsageError('Must specify locales to filter.') filtered_catalogs = [] messages_to_locales = {} for locale in locales: locale_catalog = self.get(locale) - missing_messages = locale_catalog.list_untranslated(paths=paths) + missing_messages = locale_catalog.list_untranslated(paths=paths, exclude_paths=exclude_paths) num_missing = len(missing_messages) num_total = len(locale_catalog) for message in missing_messages: diff --git a/grow/translations/catalog_holder_test.py b/grow/translations/catalog_holder_test.py index aa4d6bf3a..3bd036cc0 100644 --- a/grow/translations/catalog_holder_test.py +++ b/grow/translations/catalog_holder_test.py @@ -219,6 +219,7 @@ def test_filter(self): de_catalog = catalogs[0] self.assertEqual(3, len(de_catalog)) + paths = [ '/content/pages/yaml_test.html', ] @@ -230,6 +231,14 @@ def test_filter(self): de_catalog = catalogs[0] self.assertEqual(1, len(de_catalog)) + catalogs = self.pod.catalogs.filter( + out_path='./untranslated.po', + locales=locales, + exclude_paths=paths, + localized=False) + de_catalog = catalogs[0] + self.assertEqual(2, len(de_catalog)) + def test_filter_localized(self): locales = ['de', 'fr'] catalogs = self.pod.catalogs.filter( diff --git a/grow/translations/catalogs.py b/grow/translations/catalogs.py index eb4d8335e..62c1d2f1d 100644 --- a/grow/translations/catalogs.py +++ b/grow/translations/catalogs.py @@ -227,12 +227,14 @@ def _message_in_paths(cls, message, paths): return True return False - def list_untranslated(self, paths=None): + def list_untranslated(self, paths=None, exclude_paths=None): """Returns untranslated messages, including fuzzy translations.""" untranslated = [] for message in self: if paths and not Catalog._message_in_paths(message, paths): continue + if exclude_paths and Catalog._message_in_paths(message, exclude_paths): + continue # Ensure fuzzy messages have a message.id otherwise we'd include # the header as part of the results, which we don't want. if not message.string or (message.fuzzy and message.id):