1414import gi
1515import koji
1616
17+ from collections import defaultdict
1718from babel .messages import Catalog
19+ from datetime import datetime
1820
1921gi .require_version ('Modulemd' , '1.0' )
2022from 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