def molint(filename): """Checks filename for superfluous fields in the translated messages. Returns True if there are no errors, otherwise prints messages to stderr and returns False. """ correct = True with open(filename, 'rb') as f: buf = f.read() for context, messages, translations in mofile.parse_mo_decode(buf): # collect fields in messages s = set() for m in messages: try: s |= fields(m) except ValueError: pass if not s: continue # collect superfluous fields in translations errors = [] for t in translations: try: superfluous = fields(t) - s except ValueError: errors.append((t, "Erroneous format string")) else: if superfluous: errors.append((t, "Field{0} {1} not in message".format( 's' if len(superfluous) > 1 else '', ', '.join('{{{0}}}'.format(name) for name in superfluous)))) # write out errors if any if errors: correct = False sys.stderr.write("\n{0}: Translation contains errors!\n" " Message{1}:\n".format( filename, '' if len(messages) == 1 else "s")) for m in messages: sys.stderr.write(" {0}\n".format(m)) sys.stderr.write(" Offending translation{0}:\n".format( '' if len(errors) == 1 else "s")) for t, errmsg in errors: sys.stderr.write(" {0}:\n {1}\n".format(errmsg, t)) return correct
def molint(filename): """Checks filename for superfluous fields in the translated messages. Returns True if there are no errors, otherwise prints messages to stderr and returns False. """ correct = True with open(filename, 'rb') as f: buf = f.read() for context, messages, translations in mofile.parse_mo_decode(buf): # collect fields in messages s = set() for m in messages: try: s |= fields(m) except ValueError: pass if not s: continue # collect superfluous fields in translations errors = [] for t in translations: try: superfluous = fields(t) - s except ValueError: errors.append((t, "Erroneous format string")) else: if superfluous: errors.append((t, "Field{0} {1} not in message".format( 's' if len(superfluous) > 1 else '', ', '.join('{{{0}}}'.format(name) for name in superfluous)))) # write out errors if any if errors: correct = False sys.stderr.write( "\n{0}: Translation contains errors!\n" " Message{1}:\n".format(filename, '' if len(messages) == 1 else "s")) for m in messages: sys.stderr.write(" {0}\n".format(m)) sys.stderr.write(" Offending translation{0}:\n".format('' if len(errors) == 1 else "s")) for t, errmsg in errors: sys.stderr.write(" {0}:\n {1}\n".format(errmsg, t)) return correct