118
118
drives reading the GRIT translation files "XTB"."""
119
119
#logging.debug("Parsing %s", file_name)
121
tree = ET.parse(file_name)
122
tree = ET.parse(file_name, parser=OrderedXMLParser())
123
except FileNotFoundError as exc:
124
logging.warn("Can't find %s as requested by %s", file_name, toplevel_grd)
122
126
root = tree.getroot()
123
127
parsegrd_walker(txln_info, file_name, root, conditions, toplevel_grd)
166
170
if "refs" not in txln_info[grdsid]:
167
171
txln_info[grdsid]["refs"] = list()
169
txln_info[grdsid]["refs"].append(dict(note=node.attrib.get("desc", ""), id=node.attrib["name"], conditions=conditions, file=file_name))
173
assert file_name.endswith((".grd", ".grdp")), file_name
174
txln_info[grdsid]["refs"].append(
176
note=node.attrib.get("desc", ""),
177
id=node.attrib["name"],
178
conditions=conditions,
179
grdfile=file_name # back reference to GRD who wants that GRDID
171
182
def parsegrd_file(txln_info, file_name, node, conditions, toplevel_grd):
172
183
"""This is where we jump into parsing a translation "XTB" file for some
176
187
xtb_filename = os.path.normpath(os.path.join(os.path.dirname(file_name), node.attrib["path"]))
178
189
lang = node.attrib["lang"]
179
load_xtb(lang, txln_info, toplevel_grd, xtb_filename, conditions)
191
load_xtb(lang, txln_info, toplevel_grd, xtb_filename, conditions)
192
except FileNotFoundError as exc:
193
logging.warn("file %s, asked for by %s, could not be found, so we didn't import anything from it.", xtb_filename, toplevel_grd)
181
195
def parsegrd_if(txln_info, file_name, node, conditions, toplevel_grd):
182
196
"""GRDs can have conditions, and we pluck them out and store them with the
211
225
return list(temp_txln_info.keys())
213
227
def store_new_xtb_file(txln_info, dest_filename, ref_grd_filename, lang_to_add, grdsid_list_to_insert):
228
"""We just updated the referring GRD file for this language, so now we dump
229
all known strings for this language."""
231
assert ref_grd_filename.endswith((".grd", ".grdp")), ref_grd_filename
215
233
root = ET.Element("translationbundle")
225
243
if "langs" not in template_info:
226
logging.warning("In writing XTB for %s, grdsid %s has no 'langs'. Skipping! This might be discarding information!", lang_to_add, grdsid)
244
logging.debug("In writing XTB for %s, grdsid %s is apparently unwanted.", lang_to_add, grdsid)
229
247
txln = template_info["langs"].get(lang_to_add)
237
255
logging.error("Wanted to find the best ref for %r, for %r, but there are no refs!", dest_filename, grdsid)
240
for ref in template_info["refs"]:
241
if ref_grd_filename.endswith(os.path.dirname(ref["file"])):
258
refs = template_info["refs"]
259
# order most matching to least matching on referring grdfile filename
260
refs.sort(key=lambda ref: -len(set(os.path.split(ref["grdfile"])) & set(os.path.split(ref_grd_filename))))
245
logging.error("Tried to find the best ref in %s for %r, for %r, but failed!", template_info["refs"], dest_filename, grdsid)
248
264
translation = ET.Element("translation", dict(id=best_ref["id"]))
249
translation.text = txln
265
assert isinstance(txln[0][0], str), txln
266
translation.text = txln[0][0] # string from first available tuple TODO(cm) Decide if this is best.
250
267
root.append(translation)
263
280
translations_tags = root.findall("translations")
264
281
if len(translations_tags) == 0:
265
logging.info("No translations tag in %r, so not exporting. Skipping it.", source_grd_filename,)
282
logging.debug("No translations tag in %r, so not exporting. Skipping it.", source_grd_filename,)
267
284
elif len(translations_tags) > 1:
268
285
logging.error("%s translations tag to update in %r. I don't know what to do with these.", len(translations_tags), source_grd_filename)
300
317
iflpxln_tag.tail = "\n"
301
318
translations_tag.append(iflpxln_tag)
303
for lang_to_add in languages_in_grdandgettext-languages_in_grd:
304
assert "." not in lang_to_add
305
assert "/" not in lang_to_add
320
for gettext_lang_to_add in languages_in_grdandgettext-languages_in_grd:
321
assert "." not in gettext_lang_to_add
322
assert "/" not in gettext_lang_to_add
307
324
grd_container = os.path.dirname(dest_grd_filename)
308
xtb_filename = os.path.join(destination_container, os.path.splitext(os.path.basename(dest_grd_filename))[0], lang_to_add + ".xtb")
325
xtb_filename = os.path.join(destination_container, os.path.splitext(os.path.basename(dest_grd_filename))[0], gettext_lang_to_add + ".xtb")
309
326
xtb_filename_relative_to_grd = os.path.relpath(xtb_filename, start=grd_container)
312
file_tag = ET.Element("file", dict(lang=lang_to_add, path=xtb_filename_relative_to_grd))
329
file_tag = ET.Element("file", dict(lang=gettext_lang_to_add, path=xtb_filename_relative_to_grd))
313
330
file_tag.tail = "\n "
314
331
iflpxln_tag.append(file_tag)
317
store_new_xtb_file(source_txln_info, xtb_filename, source_grd_filename, lang_to_add, grdsid_list_to_insert)
334
store_new_xtb_file(source_txln_info, xtb_filename, source_grd_filename, gettext_lang_to_add, grdsid_list_to_insert)
319
336
# for lang_to_update in languages_in_grdandgettext & languages_in_grd:
320
# assert "." not in lang_to_add
321
# assert "/" not in lang_to_add
337
# assert "." not in gettext_lang_to_add
338
# assert "/" not in gettext_lang_to_add
323
# xtb_file_name = os.path.join(, os.path.splitext(os.path.basename(dest_grd_filename))[0] + "_" + lang_to_add + ".xtb")
340
# xtb_file_name = os.path.join(, os.path.splitext(os.path.basename(dest_grd_filename))[0] + "_" + gettext_lang_to_add + ".xtb")
324
341
# xtb_file_name_relative = os.path.join(os.path.dirname(dest_grd_filename), xtb_file_name)
326
# update_existing_xtb_file(source_txln_info, xtb_file_name_relative, lang_to_add, grdsid_list_to_insert)
343
# update_existing_xtb_file(source_txln_info, xtb_file_name_relative, gettext_lang_to_add, grdsid_list_to_insert)
328
345
def save_grd(txln_info, source_file_name, source_container, destination_container):
329
346
"""Write out a GRIT GRD file. We use the same files we started with, and
337
354
source_file_relative_to_source_container = source_file_name[len(source_container):].lstrip(os.sep)
338
355
dest_filename = os.path.join(destination_container, source_file_relative_to_source_container)
340
tree = ET.parse(source_file_name)
357
tree = ET.parse(source_file_name, parser=OrderedXMLParser())
341
358
root = tree.getroot()
343
360
add_missing_grd_files_and_export_xtb(txln_info, root, source_file_name, dest_filename, destination_container)