Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: rewrite manifest2po in Python #21532

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions pkg/lib/manifest2po
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/python3
# This file is part of Cockpit.
#
# Copyright (C) 2025 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import argparse
import json
import pathlib
from collections import defaultdict
from collections.abc import Iterable
from typing import Any

PO_HEADER = r"""msgid ""
msgstr ""
"Project-Id-Version: PACKAGE_VERSION\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Cockpit manifest2po\n"
"""


def get_docs_strings(docs: list[dict[str, str]]) -> Iterable[str]:
for doc in docs:
assert 'label' in doc, 'doc entry without label'
yield doc['label']


def get_keyword_strings(keywords: list[dict[str, list[str]]]) -> Iterable[str]:
for keyword in keywords:
assert 'matches' in keyword, 'keywords entry without matches'
for match in keyword['matches']:
yield match


def get_menu_strings(menu: dict[str, Any]) -> Iterable[str]:
for entry in menu.values():
if label := entry.get('label'):
yield label
if keywords := entry.get('keywords'):
yield from get_keyword_strings(keywords)
if docs := entry.get('docs'):
yield from get_docs_strings(docs)


def get_bridges_strings(bridges: list[dict[str, str]]) -> Iterable[str]:
for bridge in bridges:
if label := bridge.get('label'):
yield label


def get_manifest_strings(manifest: dict[str, Any]) -> Iterable[str]:
if menu := manifest.get('menu'):
yield from get_menu_strings(menu)
if tools := manifest.get('tools'):
yield from get_menu_strings(tools)
if bridges := manifest.get('bridges'):
yield from get_bridges_strings(bridges)


def main() -> None:
parser = argparse.ArgumentParser(prog='manifest2po',
description='Extracts translatable strings from manifest.json files')
parser.add_argument('-d', '--directory', help='Base directory for input files')
parser.add_argument('-o', '--output', help='Output files', required=True)
parser.add_argument('files', nargs='+', help='One or more input files', type=pathlib.Path, metavar='FILE')

args = parser.parse_args()
strings = defaultdict[str, set[str]](set)

files: Iterable[pathlib.Path] = args.files
for file in files:
if file.name != 'manifest.json':
continue

# Qualify the filename if necessary
full_path = args.directory / file if args.directory else file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strikes me as sort of odd.... are we just doing that because we want to get the correct relative paths in the comments in the output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose? We do:

mkdir -p po/ && \
./pkg/lib/html2po.js -d . -o po/cockpit.html.pot \
        $(cd . && find pkg/ -name '*.html')

with open(full_path, 'r') as fp:
manifest = json.load(fp)
for msgid in get_manifest_strings(manifest):
strings[msgid].add(str(file))

# Write PO file
with open(args.output, 'w') as fp:
fp.write(PO_HEADER)
for msgid in strings:
manifest_filenames = ' '.join([f'{fname}:0' for fname in strings[msgid]])
fp.write(f"\n#: {manifest_filenames}\n")
fp.write(f'msgid "{msgid}"\n')
fp.write('msgstr ""\n')


if __name__ == "__main__":
main()
179 changes: 0 additions & 179 deletions pkg/lib/manifest2po.js

This file was deleted.

4 changes: 2 additions & 2 deletions po/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ po/cockpit.js.pot:
$$( cd $(srcdir) && find pkg/ ! -name 'test-*' -name '*.[jt]s' -o -name '*.[jt]sx') | \
sed '/^#/ s/, c-format//' > $@

po/cockpit.manifest.pot: $(srcdir)/package-lock.json
po/cockpit.manifest.pot:
$(AM_V_GEN) mkdir -p $(dir $@) && \
$(srcdir)/pkg/lib/manifest2po.js -d $(srcdir) -o $@ \
$(srcdir)/pkg/lib/manifest2po -d $(srcdir) -o $@ \
$$(cd $(srcdir) && find pkg/ -name 'manifest.json')

po/cockpit.appstream.pot:
Expand Down
Loading