i18n/check-translation.py
changeset 43076 2372284d9457
parent 40185 19fc5a986669
child 45849 c102b704edb5
--- a/i18n/check-translation.py	Sat Oct 05 10:29:34 2019 -0400
+++ b/i18n/check-translation.py	Sun Oct 06 09:45:02 2019 -0400
@@ -10,12 +10,15 @@
 scanners = []
 checkers = []
 
+
 def scanner():
     def decorator(func):
         scanners.append(func)
         return func
+
     return decorator
 
+
 def levelchecker(level, msgidpat):
     def decorator(func):
         if msgidpat:
@@ -25,8 +28,10 @@
         checkers.append((func, level))
         func.match = match
         return func
+
     return decorator
 
+
 def match(checker, pe):
     """Examine whether POEntry "pe" is target of specified checker or not
     """
@@ -39,11 +44,14 @@
             return
     return True
 
+
 ####################
 
+
 def fatalchecker(msgidpat=None):
     return levelchecker('fatal', msgidpat)
 
+
 @fatalchecker(r'\$\$')
 def promptchoice(pe):
     """Check translation of the string given to "ui.promptchoice()"
@@ -70,7 +78,10 @@
     if [c for c, i in indices if len(c) == i + 1]:
         yield "msgstr has invalid '&' followed by none"
 
+
 deprecatedpe = None
+
+
 @scanner()
 def deprecatedsetup(pofile):
     pes = [p for p in pofile if p.msgid == '(DEPRECATED)' and p.msgstr]
@@ -78,6 +89,7 @@
         global deprecatedpe
         deprecatedpe = pes[0]
 
+
 @fatalchecker(r'\(DEPRECATED\)')
 def deprecated(pe):
     """Check for DEPRECATED
@@ -109,16 +121,20 @@
     ...     msgstr= 'something (DETACERPED, foo bar)')
     >>> match(deprecated, pe)
     """
-    if not ('(DEPRECATED)' in pe.msgstr or
-            (deprecatedpe and
-             deprecatedpe.msgstr in pe.msgstr)):
+    if not (
+        '(DEPRECATED)' in pe.msgstr
+        or (deprecatedpe and deprecatedpe.msgstr in pe.msgstr)
+    ):
         yield "msgstr inconsistently translated (DEPRECATED)"
 
+
 ####################
 
+
 def warningchecker(msgidpat=None):
     return levelchecker('warning', msgidpat)
 
+
 @warningchecker()
 def taildoublecolons(pe):
     """Check equality of tail '::'-ness between msgid and msgstr
@@ -141,6 +157,7 @@
     if pe.msgid.endswith('::') != pe.msgstr.endswith('::'):
         yield "tail '::'-ness differs between msgid and msgstr"
 
+
 @warningchecker()
 def indentation(pe):
     """Check equality of initial indentation between msgid and msgstr
@@ -159,13 +176,15 @@
     if idindent != strindent:
         yield "initial indentation width differs betweeen msgid and msgstr"
 
+
 ####################
 
+
 def check(pofile, fatal=True, warning=False):
-    targetlevel = { 'fatal': fatal, 'warning': warning }
-    targetcheckers = [(checker, level)
-                      for checker, level in checkers
-                      if targetlevel[level]]
+    targetlevel = {'fatal': fatal, 'warning': warning}
+    targetcheckers = [
+        (checker, level) for checker, level in checkers if targetlevel[level]
+    ]
     if not targetcheckers:
         return []
 
@@ -176,19 +195,22 @@
         errors = []
         for checker, level in targetcheckers:
             if match(checker, pe):
-                errors.extend((level, checker.__name__, error)
-                              for error in checker(pe))
+                errors.extend(
+                    (level, checker.__name__, error) for error in checker(pe)
+                )
         if errors:
             detected.append((pe, errors))
     return detected
 
+
 ########################################
 
 if __name__ == "__main__":
     import sys
     import optparse
 
-    optparser = optparse.OptionParser("""%prog [options] pofile ...
+    optparser = optparse.OptionParser(
+        """%prog [options] pofile ...
 
 This checks Mercurial specific translation problems in specified
 '*.po' files.
@@ -207,32 +229,44 @@
     # no-foo-check
     msgid = "....."
     msgstr = "....."
-""")
-    optparser.add_option("", "--warning",
-                         help="show also warning level problems",
-                         action="store_true")
-    optparser.add_option("", "--doctest",
-                         help="run doctest of this tool, instead of check",
-                         action="store_true")
+"""
+    )
+    optparser.add_option(
+        "",
+        "--warning",
+        help="show also warning level problems",
+        action="store_true",
+    )
+    optparser.add_option(
+        "",
+        "--doctest",
+        help="run doctest of this tool, instead of check",
+        action="store_true",
+    )
     (options, args) = optparser.parse_args()
 
     if options.doctest:
         import os
+
         if 'TERM' in os.environ:
             del os.environ['TERM']
         import doctest
+
         failures, tests = doctest.testmod()
         sys.exit(failures and 1 or 0)
 
     detected = []
     warning = options.warning
     for f in args:
-        detected.extend((f, pe, errors)
-                        for pe, errors in check(polib.pofile(f),
-                                                warning=warning))
+        detected.extend(
+            (f, pe, errors)
+            for pe, errors in check(polib.pofile(f), warning=warning)
+        )
     if detected:
         for f, pe, errors in detected:
             for level, checker, error in errors:
-                sys.stderr.write('%s:%d:%s(%s): %s\n'
-                                 % (f, pe.linenum, level, checker, error))
+                sys.stderr.write(
+                    '%s:%d:%s(%s): %s\n'
+                    % (f, pe.linenum, level, checker, error)
+                )
         sys.exit(1)