| 1 |
#!/usr/bin/env python3 |
| 2 |
# encoding=UTF-8 |
| 3 |
|
| 4 |
# Copyright © 2012-2025 Jakub Wilk <[email protected]> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the “Software”), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
# SOFTWARE. |
| 23 |
|
| 24 |
import argparse |
| 25 |
import collections |
| 26 |
import functools |
| 27 |
import itertools |
| 28 |
import os |
| 29 |
import sys |
| 30 |
|
| 31 |
import polib |
| 32 |
|
| 33 |
int(0_0) # Python >= 3.6 is required |
| 34 |
|
| 35 |
def init_polib(): |
| 36 |
# don't assume UTF-8 encoding |
| 37 |
polib.default_encoding = 'ASCII' |
| 38 |
|
| 39 |
def main(): |
| 40 |
init_polib() |
| 41 |
ap = argparse.ArgumentParser() |
| 42 |
ap.add_argument('files', metavar='FILE', nargs='+') |
| 43 |
ap.add_argument('--skip-ascii', action='store_true', help='skip ASCII characters') |
| 44 |
ap.add_argument('--stdin', action='store_true', help='read filenames from stdin') |
| 45 |
options = ap.parse_args() |
| 46 |
files = options.files |
| 47 |
if options.stdin: |
| 48 |
files = itertools.chain( |
| 49 |
files, |
| 50 |
(l.rstrip() for l in sys.stdin) |
| 51 |
) |
| 52 |
char_counter = collections.Counter() |
| 53 |
char_files = collections.defaultdict(set) |
| 54 |
n_files = 0 |
| 55 |
eprint = functools.partial(print, file=sys.stderr, flush=True) |
| 56 |
for path in files: |
| 57 |
eprint(path, end=' ... ') |
| 58 |
try: |
| 59 |
extension = os.path.splitext(path)[-1] |
| 60 |
if extension == '.po': |
| 61 |
constructor = polib.pofile |
| 62 |
elif extension in {'.mo', '.gmo'}: |
| 63 |
constructor = polib.mofile |
| 64 |
else: |
| 65 |
raise NotImplementedError(repr(extension)) |
| 66 |
file = constructor(path) |
| 67 |
for message in file.translated_entries(): |
| 68 |
for text in itertools.chain([message.msgstr], message.msgstr_plural.values()): |
| 69 |
for ch in text: |
| 70 |
char_counter[ch] += 1 |
| 71 |
char_files[ch].add(path) |
| 72 |
file = None |
| 73 |
except Exception as exc: # pylint: disable=broad-except |
| 74 |
eprint('error:', exc) |
| 75 |
else: |
| 76 |
eprint('ok') |
| 77 |
n_files += 1 |
| 78 |
for n, ch in sorted(((n, ch) for ch, n in char_counter.items()), reverse=True): |
| 79 |
if options.skip_ascii and ord(ch) < 0x80: |
| 80 |
continue |
| 81 |
filecov = len(char_files[ch]) / n_files |
| 82 |
print(f'{n:6} [{filecov:6.1%}] {ch!r}') |
| 83 |
|
| 84 |
if __name__ == '__main__': |
| 85 |
main() |
| 86 |
|
| 87 |
# vim:ts=4 sts=4 sw=4 et |