#!/usr/bin/env python3 # encoding=UTF-8 # Copyright © 2012-2025 Jakub Wilk # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import argparse import collections import functools import itertools import os import re import sys import polib int(0_0) # Python >= 3.6 is required def init_polib(): # Happily decode any file, even when encoding declaration is broken or # missing. polib.default_encoding = 'ISO-8859-1' is_sane_field_name = re.compile('^[a-zA-Z-]+$').match _extract_address = re.compile(r'''^(?: .*< ( [^<>]+@[^<>]+ ) >\s* | ( \S+@\S+ ) | .*< ( https?://[^<>]+ ) >\s* | .*[(] ( https?://[^()]+ ) [)]\s* | \s* (https?://\S+) \s* )$''', re.VERBOSE).match def extract_address(s): match = _extract_address(s) if match is None: return [result] = filter(None, match.groups()) return result def main(): init_polib() ap = argparse.ArgumentParser() ap.add_argument('files', metavar='FILE', nargs='+') ap.add_argument('-F', '--field', help='only this field') ap.add_argument('--insane', action='store_true', help='allow insane field names') ap.add_argument('--extract-addresses', action='store_true') ap.add_argument('--all-test-cases', action='store_true') ap.add_argument('--plural-only', action='store_true', help='consider only translations with plural forms') ap.add_argument('--stdin', action='store_true', help='read filenames from stdin') options = ap.parse_args() metadata = collections.defaultdict(collections.Counter) if options.all_test_cases: test_cases = collections.defaultdict(set) else: test_cases = {} files = options.files if options.stdin: files = itertools.chain( files, (l.rstrip() for l in sys.stdin) ) eprint = functools.partial(print, file=sys.stderr, flush=True) for path in files: eprint(path, end=' ... ') try: extension = os.path.splitext(path)[-1] if extension == '.po': constructor = polib.pofile elif extension in {'.mo', '.gmo'}: constructor = polib.mofile else: raise NotImplementedError(repr(extension)) file = constructor(path) if options.plural_only: for msg in file.translated_entries(): if msg.msgstr_plural: break else: eprint('skip') continue for k, v in file.metadata.items(): if (not is_sane_field_name(k)) and (not options.insane): continue if not (options.field is None or k == options.field): continue if options.extract_addresses: v = extract_address(v) metadata[k][v] += 1 if options.all_test_cases: test_cases[k, v].add(path) else: test_cases[k, v] = path file = None except Exception as exc: # pylint: disable=broad-except eprint('error:', exc) else: eprint('ok') for key, values in sorted(metadata.items()): print(f'{key!r}:') for value, n in values.most_common(): if options.all_test_cases: print(f' {n:6} {value!r}') for path in sorted(test_cases[key, value]): print(f' + {path!r}') else: path = test_cases[key, value] print(f' {n:6} {value!r}; test-case: {path!r}') if __name__ == '__main__': main() # vim:ts=4 sts=4 sw=4 et