def _process_file(filename): """Builds list of translation units from input file. Each translation unit in the input file includes: - an id (opaquely generated by Soy) - the Blockly name for the message - the text in the source language (generally English) - a description for the translator The Soy and Blockly ids are joined with a hyphen and serve as the keys in both output files. The value is the corresponding text (in the <lang>.json file) or the description (in the qqq.json file). Args: filename: The name of an .xlf file produced by Closure. Raises: IOError: An I/O error occurred with an input or output file. InputError: The input file could not be parsed or lacked required fields. Returns: A list of dictionaries produced by parse_trans_unit(). """ try: results = [] # list of dictionaries (return value) names = [] # list of names of encountered keys (local variable) try: parsed_xml = minidom.parse(filename) except IOError: # Don't get caught by below handler raise except Exception, e: print raise InputError(filename, str(e)) # Make sure needed fields are present and non-empty. for trans_unit in parsed_xml.getElementsByTagName("trans-unit"): unit = parse_trans_unit(trans_unit) for key in ["description", "meaning", "source"]: if not key in unit or not unit[key]: raise InputError(filename + ":" + unit["key"], key + " not found") if unit["description"].lower() == "ibid": if unit["meaning"] not in names: # If the term has not already been described, the use of 'ibid' # is an error. raise InputError( filename, "First definition of: " + unit["meaning"] + " has definition: " + unit["description"] ) else: # If term has already been described, 'ibid' was used correctly, # and we output nothing. pass else: if unit["meaning"] in names: raise InputError(filename, "Second definition of: " + unit["meaning"]) names.append(unit["meaning"]) results.append(unit) return results
def _process_file(filename): """Builds list of translation units from input file. Each translation unit in the input file includes: - an id (opaquely generated by Soy) - the Blockly name for the message - the text in the source language (generally English) - a description for the translator The Soy and Blockly ids are joined with a hyphen and serve as the keys in both output files. The value is the corresponding text (in the <lang>.json file) or the description (in the qqq.json file). Args: filename: The name of an .xlf file produced by Closure. Raises: IOError: An I/O error occurred with an input or output file. InputError: The input file could not be parsed or lacked required fields. Returns: A list of dictionaries produced by parse_trans_unit(). """ try: results = [] # list of dictionaries (return value) names = [] # list of names of encountered keys (local variable) try: parsed_xml = minidom.parse(filename) except IOError: # Don't get caught by below handler raise except Exception, e: print raise InputError(filename, str(e)) # Make sure needed fields are present and non-empty. for trans_unit in parsed_xml.getElementsByTagName('trans-unit'): unit = parse_trans_unit(trans_unit) for key in ['description', 'meaning', 'source']: if not key in unit or not unit[key]: raise InputError(filename + ':' + unit['key'], key + ' not found') # Exclude entries whose description is elsewhere ('ibid'). if not unit['description'].lower() == 'ibid': if unit['meaning'] in names: raise InputError(filename, 'Second definition of: ' + unit['meaning']) names.append(unit['meaning']) results.append(unit) return results
def _process_file(filename): """Builds list of translation units from input file. Each translation unit in the input file includes: - an id (opaquely generated by Soy) - the Blockly name for the message - the text in the source language (generally English) - a description for the translator The Soy and Blockly ids are joined with a hyphen and serve as the keys in both output files. The value is the corresponding text (in the <lang>.json file) or the description (in the qqq.json file). Args: filename: The name of an .xlf file produced by Closure. Raises: IOError: An I/O error occurred with an input or output file. InputError: The input file could not be parsed or lacked required fields. Returns: A list of dictionaries produced by parse_trans_unit(). """ try: results = [] try: parsed_xml = minidom.parse(filename) except IOError: # Don't get caught by below handler raise except Exception, e: print raise InputError(filename, str(e)) # Make sure needed fields are present and non-empty. for trans_unit in parsed_xml.getElementsByTagName('trans-unit'): unit = parse_trans_unit(trans_unit) for key in ['description', 'meaning', 'source']: if not key in unit or not unit[key]: raise InputError(filename + ':' + unit['key'], key + ' not found') results.append(unit) return results
def _process_file(filename): """Builds list of translation units from input file. Each translation unit in the input file includes: - an id (opaquely generated by Soy) - the Blockly name for the message - the text in the source language (generally English) - a description for the translator The Soy and Blockly ids are joined with a hyphen and serve as the keys in both output files. The value is the corresponding text (in the <lang>.json file) or the description (in the qqq.json file). Args: filename: The name of an .xlf file produced by Closure. Raises: IOError: An I/O error occurred with an input or output file. InputError: The input file could not be parsed or lacked required fields. Returns: A list of dictionaries produced by parse_trans_unit(). """ try: results = [] # list of dictionaries (return value) names = [] # list of names of encountered keys (local variable) try: parsed_xml = minidom.parse(filename) except IOError: # Don't get caught by below handler raise except Exception, e: print raise InputError(filename, str(e)) # Make sure needed fields are present and non-empty. for trans_unit in parsed_xml.getElementsByTagName('trans-unit'): unit = parse_trans_unit(trans_unit) for key in ['description', 'meaning', 'source']: if not key in unit or not unit[key]: raise InputError(filename + ':' + unit['key'], key + ' not found') if unit['description'].lower() == 'ibid': if unit['meaning'] not in names: # If the term has not already been described, the use of 'ibid' # is an error. raise InputError( filename, 'First definition of: ' + unit['meaning'] + ' has definition: ' + unit['description']) else: # If term has already been described, 'ibid' was used correctly, # and we output nothing. pass else: if unit['meaning'] in names: raise InputError( filename, 'Second definition of: ' + unit['meaning']) names.append(unit['meaning']) results.append(unit) return results