|
| 1 | +#!/usr/bin/python3 |
| 2 | +# This file is part of Cockpit. |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Red Hat, Inc. |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import argparse |
| 20 | +import collections |
| 21 | +import collections.abc |
| 22 | +import json |
| 23 | +import pathlib |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +PO_HEADER = r"""msgid "" |
| 27 | +msgstr "" |
| 28 | +"Project-Id-Version: PACKAGE_VERSION\n" |
| 29 | +"MIME-Version: 1.0\n" |
| 30 | +"Content-Type: text/plain; charset=UTF-8\n" |
| 31 | +"Content-Transfer-Encoding: 8bit\n" |
| 32 | +"X-Generator: Cockpit manifest2po\n" |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +def get_docs_strings(docs: list[dict[str, str]]) -> collections.abc.Iterable[str]: |
| 37 | + for doc in docs: |
| 38 | + assert 'label' in doc, 'doc entry without label' |
| 39 | + yield doc['label'] |
| 40 | + |
| 41 | + |
| 42 | +def get_keyword_strings(keywords: list[dict[str, list[str]]]) -> collections.abc.Iterable[str]: |
| 43 | + for keyword in keywords: |
| 44 | + assert 'matches' in keyword, 'keywords entry without matches' |
| 45 | + for match in keyword['matches']: |
| 46 | + yield match |
| 47 | + |
| 48 | + |
| 49 | +def get_menu_strings(menu: dict[str, Any]) -> collections.abc.Iterable[str]: |
| 50 | + for entry in menu.values(): |
| 51 | + if label := entry.get('label'): |
| 52 | + yield label |
| 53 | + if keywords := entry.get('keywords'): |
| 54 | + yield from get_keyword_strings(keywords) |
| 55 | + if docs := entry.get('docs'): |
| 56 | + yield from get_docs_strings(docs) |
| 57 | + |
| 58 | + |
| 59 | +def get_bridges_strings(bridges: list[dict[str, str]]) -> collections.abc.Iterable[str]: |
| 60 | + for bridge in bridges: |
| 61 | + if label := bridge.get('label'): |
| 62 | + yield label |
| 63 | + |
| 64 | + |
| 65 | +def get_manifest_strings(manifest: dict[str, Any]) -> collections.abc.Iterable[str]: |
| 66 | + if menu := manifest.get('menu'): |
| 67 | + yield from get_menu_strings(menu) |
| 68 | + if tools := manifest.get('tools'): |
| 69 | + yield from get_menu_strings(tools) |
| 70 | + if bridges := manifest.get('bridges'): |
| 71 | + yield from get_bridges_strings(bridges) |
| 72 | + |
| 73 | + |
| 74 | +def main() -> None: |
| 75 | + parser = argparse.ArgumentParser(prog='manifest2po', |
| 76 | + description='Extracts translatable strings from manifest.json files') |
| 77 | + parser.add_argument('-d', '--directory', help='Base directory for input files') |
| 78 | + parser.add_argument('-o', '--output', help='Output files', required=True) |
| 79 | + parser.add_argument('files', nargs='+', help='One or more input files', type=pathlib.Path, metavar='FILE') |
| 80 | + |
| 81 | + args = parser.parse_args() |
| 82 | + strings = collections.defaultdict[str, set[str]](set) |
| 83 | + |
| 84 | + files: collections.abc.Iterable[pathlib.Path] = args.files |
| 85 | + for file in files: |
| 86 | + if file.name != 'manifest.json': |
| 87 | + continue |
| 88 | + |
| 89 | + # Qualify the filename if necessary |
| 90 | + full_path = args.directory / file if args.directory else file |
| 91 | + with open(full_path, 'r') as fp: |
| 92 | + manifest = json.load(fp) |
| 93 | + for msgid in get_manifest_strings(manifest): |
| 94 | + strings[msgid].add(str(file)) |
| 95 | + |
| 96 | + # Write PO file |
| 97 | + with open(args.output, 'w') as fp: |
| 98 | + fp.write(PO_HEADER) |
| 99 | + for msgid in strings: |
| 100 | + manifest_filenames = ' '.join([f'{fname}:0' for fname in strings[msgid]]) |
| 101 | + fp.write(f"\n#: {manifest_filenames}\n") |
| 102 | + fp.write(f'msgid "{msgid}"\n') |
| 103 | + fp.write('msgstr ""\n') |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments