def main(argv=None): if argv is None: argv = sys.argv parser = argparse.ArgumentParser("Check for duplicated accelerators") parser.add_argument("-t", "--translate", action='store_true', help="Check translated strings") parser.add_argument("-p", "--podir", action='store', type=str, metavar='PODIR', help='Directory containing po files', default='./po') parser.add_argument("glade_files", nargs="+", metavar="GLADE-FILE", help='The glade file to check') args = parser.parse_args(args=argv) # First check the untranslated strings in each file for glade_file in args.glade_files: check_glade(glade_file) # Now loop over all of the translations if args.translate: import langtable podicts = translate_all(args.podir) for (lang, po_map) in ((key, podicts[key]) for key in podicts.keys()): # Set the locale so that we can use lower() on accelerator keys. # If the language is of the form xx_XX, use that as the # locale name. Otherwise use the first locale that # langtable returns for the language. If that doesn't work, # just use C and hope for the best. if '_' in lang: locale.setlocale(locale.LC_ALL, lang) else: locale_list = langtable.list_locales(languageId=lang) if locale_list: try: locale.setlocale(locale.LC_ALL, locale_list[0]) except locale.Error: print("No such locale %s, using C" % locale_list[0]) locale.setlocale(locale.LC_ALL, 'C') else: locale.setlocale(locale.LC_ALL, 'C') for glade_file in args.glade_files: check_glade(glade_file, po_map)
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 if __name__ == "__main__": parser = argparse.ArgumentParser("Check Pango markup validity") parser.add_argument("-t", "--translate", action='store_true', help="Check translated strings") parser.add_argument("-p", "--podir", action='store', type=str, metavar='PODIR', help='Directory containing po files', default='./po') parser.add_argument("glade_files", nargs="+", metavar="GLADE-FILE", help='The glade file to check') args = parser.parse_args(args=sys.argv[1:]) success = True for file_path in args.glade_files: if not check_glade_file(file_path): success = False # Now loop over all of the translations if args.translate: podicts = translate_all(args.podir) for po_dict in podicts.values(): for file_path in args.glade_files: if not check_glade_file(file_path, po_dict): success = False sys.exit(0 if success else 1)
def visit_const(self, node): if not isinstance(node.value, types.StringType) and not isinstance(node.value, types.UnicodeType): return if not is_markup(node.value): return self._validate_pango_markup_string(node, node.value) # Check translated versions of the string if requested if self.config.translate_markup: global podicts # Check if this is a translatable string curr = node i18nFunc = None while curr.parent: if isinstance(curr.parent, astroid.CallFunc) and \ getattr(curr.parent.func, "name", "") in i18n_funcs: i18nFunc = curr.parent break curr = curr.parent if i18nFunc: # If not done already, import polib and read the translations if not podicts: try: from translatepo import translate_all except ImportError: print("Unable to load po translation module") sys.exit(99) else: podicts = translate_all(os.path.join(os.environ.get('top_srcdir', '.'), 'po')) if i18nFunc.func.name in i18n_ctxt_funcs: msgctxt = i18nFunc.args[0].value else: msgctxt = None # Loop over all translations for the string for podict in podicts.values(): try: node_values = podict.get(node.value, msgctxt) except KeyError: continue for value in node_values: self._validate_pango_markup_string(node, value, podict.metadata['Language']) # Check that the markup matches, roughly if not markup_match(node.value, value): self.add_message("W9925", node=node, args=(podict.metadata['Language'],)) # Check if this the left side of a % operation curr = node formatOp = None while curr.parent: if isinstance(curr.parent, astroid.BinOp) and curr.parent.op == "%" and \ curr.parent.left == curr: formatOp = curr.parent break curr = curr.parent # Check whether the right side of the % operation is escaped if formatOp: if isinstance(formatOp.right, astroid.CallFunc): if getattr(formatOp.right.func, "name", "") not in escapeMethods: self.add_message("W9922", node=formatOp.right) # If a tuple, each item in the tuple must be escaped elif isinstance(formatOp.right, astroid.Tuple): for elt in formatOp.right.elts: if not isinstance(elt, astroid.CallFunc) or\ getattr(elt.func, "name", "") not in escapeMethods: self.add_message("W9922", node=elt) # If a dictionary, each value must be escaped elif isinstance(formatOp.right, astroid.Dict): for item in formatOp.right.items: if not isinstance(item[1], astroid.CallFunc) or\ getattr(item[1].func, "name", "") not in escapeMethods: self.add_message("W9922", node=item[1]) else: self.add_message("W9922", node=formatOp)
"--translate", action='store_true', help="Check translated strings") parser.add_argument("-p", "--podir", action='store', type=str, metavar='PODIR', help='Directory containing po files', default='./po') parser.add_argument("glade_files", nargs="+", metavar="GLADE-FILE", help='The glade file to check') args = parser.parse_args(args=sys.argv[1:]) success = True for file_path in args.glade_files: if not check_glade_file(file_path): success = False # Now loop over all of the translations if args.translate: podicts = translate_all(args.podir) for po_dict in podicts.values(): for file_path in args.glade_files: if not check_glade_file(file_path, po_dict): success = False sys.exit(0 if success else 1)
def visit_const(self, node): if type(node.value) not in (types.StringType, types.UnicodeType): return if not is_markup(node.value): return self._validate_pango_markup_string(node, node.value) # Check translated versions of the string if requested if self.config.translate_markup: global podicts # Check if this is a translatable string curr = node i18nFunc = None while curr.parent: if isinstance(curr.parent, astroid.CallFunc) and \ getattr(curr.parent.func, "name", "") in i18n_funcs: i18nFunc = curr.parent break curr = curr.parent if i18nFunc: # If not done already, import polib and read the translations if not podicts: try: from translatepo import translate_all except ImportError: print("Unable to load po translation module") sys.exit(99) else: podicts = translate_all( os.path.join(os.environ.get('top_srcdir', '.'), 'po')) if i18nFunc.func.name in i18n_ctxt_funcs: msgctxt = i18nFunc.args[0].value else: msgctxt = None # Loop over all translations for the string for podict in podicts.values(): try: node_values = podict.get(node.value, msgctxt) except KeyError: continue for value in node_values: self._validate_pango_markup_string( node, value, podict.metadata['Language']) # Check that the markup matches, roughly if not markup_match(node.value, value): self.add_message( "W9925", node=node, args=(podict.metadata['Language'], )) # Check if this the left side of a % operation curr = node formatOp = None while curr.parent: if isinstance(curr.parent, astroid.BinOp) and curr.parent.op == "%" and \ curr.parent.left == curr: formatOp = curr.parent break curr = curr.parent # Check whether the right side of the % operation is escaped if formatOp: if isinstance(formatOp.right, astroid.CallFunc): if getattr(formatOp.right.func, "name", "") not in escapeMethods: self.add_message("W9922", node=formatOp.right) # If a tuple, each item in the tuple must be escaped elif isinstance(formatOp.right, astroid.Tuple): for elt in formatOp.right.elts: if not isinstance(elt, astroid.CallFunc) or\ getattr(elt.func, "name", "") not in escapeMethods: self.add_message("W9922", node=elt) # If a dictionary, each value must be escaped elif isinstance(formatOp.right, astroid.Dict): for item in formatOp.right.items: if not isinstance(item[1], astroid.CallFunc) or\ getattr(item[1].func, "name", "") not in escapeMethods: self.add_message("W9922", node=item[1]) else: self.add_message("W9922", node=formatOp)