def checkGlade(self, glade_tree): """Check the validity of Pango markup.""" lang = glade_tree.getroot().get("lang") if lang: lang_str = " for language %s" % lang else: lang_str = "" # Search for label properties on objects that have use_markup set to True for label in glade_tree.xpath(".//property[@name='label' and ../property[@name='use_markup']/text() = 'True']"): # Wrap the label text in <markup> tags and parse the tree try: # pylint: disable=unescaped-markup pango_tree = etree.fromstring("<markup>%s</markup>" % label.text) _validate_pango_markup(pango_tree) # Check if the markup is necessary self.assertTrue(markup_necessary(pango_tree), msg="Markup %s could be expressed as attributes at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline)) except etree.XMLSyntaxError: raise AssertionError("Unable to parse pango markup %s at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline)) except PangoElementException as px: raise AssertionError("Invalid pango element %s at %s%s:%d" % (px.element, label.base, lang_str, label.sourceline)) # If this is a translated node, check that the translated markup # has the same elements and attributes as the original. orig_markup = label.get("original_text") if orig_markup: self.assertTrue(markup_match(label.text, orig_markup), msg="Translated markup %s does not contain the same elements and attributes at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline))
def test_markup(poentry): # Unnecessary markup is markup applied to an entire string, such as # _("<b>Bold Text</b>"). This could be instead be translated as # "<b>%s</b>" % _("Bold Text"), and then the translator doesn't have to see # the markup at all. if is_markup(poentry.msgid): # Wrap the string in <markup> nodes, parse it, test it # The markup is unescaped on purpose # pylint: disable=unescaped-markup tree = ET.fromstring("<markup>%s</markup>" % poentry.msgid) if not markup_necessary(tree): raise AssertionError("Unnecessary markup")
def check_glade_file(glade_file_path, po_map=None): glade_success = True with open(glade_file_path) as glade_file: # Parse the XML glade_tree = etree.parse(glade_file) # Search for label properties on objects that have use_markup set to True for label in glade_tree.xpath(".//property[@name='label' and ../property[@name='use_markup']/text() = 'True']"): if po_map: try: label_texts = po_map.get(label.text, label.get("context")) except KeyError: continue lang_str = " for language %s" % po_map.metadata["Language"] else: label_texts = (label.text,) lang_str = "" # Wrap the label text in <markup> tags and parse the tree for label_text in label_texts: try: # pylint: disable=unescaped-markup pango_tree = etree.fromstring("<markup>%s</markup>" % label_text) _validate_pango_markup(pango_tree) # Check if the markup is necessary if not markup_necessary(pango_tree): print( "Markup could be expressed as attributes at %s%s:%d" % (glade_file_path, lang_str, label.sourceline) ) glade_success = False except etree.XMLSyntaxError: print("Unable to parse pango markup at %s%s:%d" % (glade_file_path, lang_str, label.sourceline)) glade_success = False except PangoElementException as px: print( "Invalid pango element %s at %s%s:%d" % (px.element, glade_file_path, lang_str, label.sourceline) ) glade_success = False else: if po_map: # Check that translated markup has the same elements and attributes if not markup_match(label.text, label_text): print( "Translated markup does not contain the same elements and attributes at %s%s:%d" % (glade_file_path, lang_str, label.sourceline) ) glade_success = False return glade_success
def check_glade_file(glade_file_path, po_map=None): glade_success = True with open(glade_file_path) as glade_file: # Parse the XML glade_tree = etree.parse(glade_file) # Search for label properties on objects that have use_markup set to True for label in glade_tree.xpath( ".//property[@name='label' and ../property[@name='use_markup']/text() = 'True']" ): if po_map: try: label_texts = po_map.get(label.text, label.get("context")) except KeyError: continue lang_str = " for language %s" % po_map.metadata['Language'] else: label_texts = (label.text, ) lang_str = "" # Wrap the label text in <markup> tags and parse the tree for label_text in label_texts: try: # pylint: disable=unescaped-markup pango_tree = etree.fromstring("<markup>%s</markup>" % label_text) _validate_pango_markup(pango_tree) # Check if the markup is necessary if not markup_necessary(pango_tree): print("Markup could be expressed as attributes at %s%s:%d" % \ (glade_file_path, lang_str, label.sourceline)) glade_success = False except etree.XMLSyntaxError: print("Unable to parse pango markup at %s%s:%d" % \ (glade_file_path, lang_str, label.sourceline)) glade_success = False except PangoElementException as px: print("Invalid pango element %s at %s%s:%d" % \ (px.element, glade_file_path, lang_str, label.sourceline)) glade_success = False else: if po_map: # Check that translated markup has the same elements and attributes if not markup_match(label.text, label_text): print("Translated markup does not contain the same elements and attributes at %s%s:%d" % \ (glade_file_path, lang_str, label.sourceline)) glade_success = False return glade_success
def _check_markup(self, glade_tree): """Check the validity of Pango markup.""" lang = glade_tree.getroot().get("lang") if lang: lang_str = " for language %s" % lang else: lang_str = "" # Search for label properties on objects that have use_markup set to True for label in glade_tree.xpath( ".//property[@name='label' and ../property[@name='use_markup']/text() = 'True']" ): # Wrap the label text in <markup> tags and parse the tree try: # pylint: disable=unescaped-markup pango_tree = etree.fromstring("<markup>%s</markup>" % label.text) _validate_pango_markup(pango_tree) # Check if the markup is necessary self.assertTrue( markup_necessary(pango_tree), msg="Markup %s could be expressed as attributes at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline)) except etree.XMLSyntaxError as xx: raise AssertionError( "Unable to parse pango markup %s at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline)) from xx except PangoElementException as px: raise AssertionError("Invalid pango element %s at %s%s:%d" % (px.element, label.base, lang_str, label.sourceline)) from px # If this is a translated node, check that the translated markup # has the same elements and attributes as the original. orig_markup = label.get("original_text") if orig_markup: self.assertTrue( markup_match(label.text, orig_markup), msg= "Translated markup %s does not contain the same elements and attributes at %s%s:%d" % (label.text, label.base, lang_str, label.sourceline))