Skip to content

Commit 57e122e

Browse files
committed
Produce modulemd-translations from Zanata
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
1 parent 9f1d249 commit 57e122e

2 files changed

Lines changed: 94 additions & 6 deletions

File tree

generate_modulemd_translations.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import requests
1818
import sys
1919

20+
import mmdtranslations
21+
2022
from babel.messages import pofile
2123
from io import BytesIO
2224

@@ -30,6 +32,8 @@
3032

3133

3234
@click.command()
35+
@click.option('-d', '--debug', default=False, is_flag= True,
36+
help="Add debugging output")
3337
@click.option('-z', '--zanata-rest-url',
3438
default="https://fedora.zanata.org/rest",
3539
type=str, help="""
@@ -54,7 +58,7 @@
5458
The name of the translated file in Zanata.
5559
(Default: fedora-modularity-translations)
5660
""")
57-
def main(zanata_rest_url, zanata_project,
61+
def main(debug, zanata_rest_url, zanata_project,
5862
zanata_translation_file, zanata_project_version):
5963
"""
6064
:param zanata_rest_url: The base URL to the zanata instance
@@ -98,18 +102,26 @@ def main(zanata_rest_url, zanata_project,
98102
file=sys.stderr)
99103
continue
100104

105+
if debug:
106+
print(r.text)
107+
101108
# Read the po file into a catalog, indexed by the locale
102109
catalogs[loc] = pofile.read_po(
103110
BytesIO(r.content),
104111
domain="fedora-modularity-translations")
105112

106-
print(catalogs[loc].get(u"A command-line wrapper for git with "
107-
u"github "
108-
u"shortcuts").string)
113+
translations = mmdtranslations.get_modulemd_translations_from_catalog_dict(
114+
catalogs)
115+
116+
if debug:
117+
for translation in translations:
118+
print(translation.dumps())
119+
120+
Modulemd.dump(sorted(translations), "modulemd-translations.yaml")
109121

110122

111123

112124

113125

114126
if __name__ == "__main__":
115-
main()
127+
main()

mmdtranslations.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import gi
1515
import koji
1616

17+
from collections import defaultdict
1718
from babel.messages import Catalog
19+
from datetime import datetime
1820

1921
gi.require_version('Modulemd', '1.0')
2022
from gi.repository import Modulemd
@@ -119,4 +121,78 @@ def get_module_catalog(session, builds):
119121
3))
120122
catalog.add(profile.props.description, locations=locations)
121123

122-
return catalog
124+
return catalog
125+
126+
127+
def get_modulemd_translations_from_catalog_dict(catalog_dict):
128+
now = datetime.utcnow()
129+
modified = int("%04d%02d%02d%02d%02d%02d" % (
130+
now.date().year,
131+
now.date().month,
132+
now.date().day,
133+
now.time().hour,
134+
now.time().minute,
135+
now.time().second
136+
))
137+
138+
# Translation entries keyed by name, stream and locale
139+
entries = dict()
140+
141+
mmd_translations = dict()
142+
for locale, catalog in catalog_dict.items():
143+
for msg in catalog:
144+
if not msg.locations or not msg.string:
145+
# Skip any message that doesn't actually contain a message
146+
continue
147+
148+
for location, _ in msg.locations:
149+
split_location = location.split(';')
150+
if len(split_location) < 3 or len(split_location) > 5:
151+
print("Invalid location clue in translation data: %s" % (
152+
location), file=sys.stderr)
153+
154+
module_name = split_location[0]
155+
module_stream = split_location[1]
156+
157+
try:
158+
entry = entries[(module_name, module_stream, locale)]
159+
except KeyError:
160+
entry = Modulemd.TranslationEntry.new(locale)
161+
162+
# Summary Translation
163+
if split_location[2] == "summary":
164+
entry.set_summary(msg.string)
165+
166+
# Description Translation
167+
elif split_location[2] == "description":
168+
entry.set_description(msg.string)
169+
170+
# Translation of profile descriptions
171+
elif split_location[2] == "profile":
172+
entry.set_profile_description(split_location[3],
173+
msg.string)
174+
175+
entries[(module_name, module_stream, locale)] = entry
176+
177+
for (module_name, module_stream, locale), entry in entries.items():
178+
# Validate that the translation entry has both summary and description
179+
# which are mandatory.
180+
if not entry.get_summary() or not entry.get_description():
181+
continue
182+
183+
# Otherwise, add or update the translation for this module and stream
184+
try:
185+
mmdtranslation = mmd_translations[(module_name,
186+
module_stream)]
187+
except KeyError:
188+
mmdtranslation = Modulemd.Translation.new_full(
189+
module_name=module_name,
190+
module_stream=module_stream,
191+
mdversion=1,
192+
modified=modified
193+
)
194+
195+
mmdtranslation.add_entry(entry)
196+
mmd_translations[(module_name, module_stream)] = mmdtranslation
197+
198+
return mmd_translations.values()

0 commit comments

Comments
 (0)