i18n/check-translation.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Wed, 05 Nov 2014 10:05:38 +0000
changeset 23251 85c634ff395a
parent 22203 35c2ea4ca26f
child 26261 8fb92ff63ccf
permissions -rw-r--r--
transaction: drop backupentries logic from startgroup and endgroup The `startgroup` and `endgroup` methods are used in a very specific context to wrap a very specific operation (revlog truncation). It does not make sense to perform any other operations during such a "group" (eg:file backup). There is currently no user of backupfile during a "group" so we drop the group-specific code and restrict authorized operations during "group".
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     1
#!/usr/bin/env python
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     3
# check-translation.py - check Mercurial specific translation problems
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     4
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     5
import polib
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     6
import re
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     8
checkers = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
    10
def levelchecker(level, msgidpat):
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
    def decorator(func):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    12
        if msgidpat:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    13
            match = re.compile(msgidpat).search
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    14
        else:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    15
            match = lambda msgid: True
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    16
        checkers.append((func, level))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    17
        func.match = match
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    18
        return func
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
    return decorator
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
def match(checker, pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
    """Examine whether POEntry "pe" is target of specified checker or not
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    23
    """
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
    if not checker.match(pe.msgid):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    25
        return
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
    # examine suppression by translator comment
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    27
    nochecker = 'no-%s-check' % checker.__name__
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    28
    for tc in pe.tcomment.split():
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    29
        if nochecker == tc:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    30
            return
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
    return True
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    32
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
def fatalchecker(msgidpat=None):
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
    36
    return levelchecker('fatal', msgidpat)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    38
@fatalchecker(r'\$\$')
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
def promptchoice(pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    40
    """Check translation of the string given to "ui.promptchoice()"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    41
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    42
    >>> pe = polib.POEntry(
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    43
    ...     msgid ='prompt$$missing &sep$$missing &amp$$followed by &none',
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    44
    ...     msgstr='prompt  missing &sep$$missing  amp$$followed by none&')
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    45
    >>> match(promptchoice, pe)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
    True
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
    >>> for e in promptchoice(pe): print e
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    48
    number of choices differs between msgid and msgstr
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    49
    msgstr has invalid choice missing '&'
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    50
    msgstr has invalid '&' followed by none
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    51
    """
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    52
    idchoices = [c.rstrip(' ') for c in pe.msgid.split('$$')[1:]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    53
    strchoices = [c.rstrip(' ') for c in pe.msgstr.split('$$')[1:]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    55
    if len(idchoices) != len(strchoices):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    56
        yield "number of choices differs between msgid and msgstr"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    57
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    58
    indices = [(c, c.find('&')) for c in strchoices]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    59
    if [c for c, i in indices if i == -1]:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
        yield "msgstr has invalid choice missing '&'"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    61
    if [c for c, i in indices if len(c) == i + 1]:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    62
        yield "msgstr has invalid '&' followed by none"
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    63
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    64
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    66
def warningchecker(msgidpat=None):
22203
35c2ea4ca26f cleanup: rename check-translation.py checker function - don't hide global var
Mads Kiilerich <madski@unity3d.com>
parents: 20515
diff changeset
    67
    return levelchecker('warning', msgidpat)
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
20514
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    69
@warningchecker()
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    70
def taildoublecolons(pe):
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    71
    """Check equality of tail '::'-ness between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    72
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    73
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    74
    ...     msgid ='ends with ::',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    75
    ...     msgstr='ends with ::')
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    76
    >>> for e in taildoublecolons(pe): print e
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    77
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    78
    ...     msgid ='ends with ::',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    79
    ...     msgstr='ends without double-colons')
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    80
    >>> for e in taildoublecolons(pe): print e
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    81
    tail '::'-ness differs between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    82
    >>> pe = polib.POEntry(
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    83
    ...     msgid ='ends without double-colons',
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    84
    ...     msgstr='ends with ::')
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    85
    >>> for e in taildoublecolons(pe): print e
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    86
    tail '::'-ness differs between msgid and msgstr
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    87
    """
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    88
    if pe.msgid.endswith('::') != pe.msgstr.endswith('::'):
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    89
        yield "tail '::'-ness differs between msgid and msgstr"
410c80539c5c i18n: check equality of tail '::'-ness between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20164
diff changeset
    90
20515
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    91
@warningchecker()
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    92
def indentation(pe):
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    93
    """Check equality of initial indentation between msgid and msgstr
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    94
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    95
    This may report unexpected warning, because this doesn't aware
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    96
    the syntax of rst document and the context of msgstr.
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    97
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    98
    >>> pe = polib.POEntry(
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
    99
    ...     msgid ='    indented text',
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   100
    ...     msgstr='  narrowed indentation')
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   101
    >>> for e in indentation(pe): print e
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   102
    initial indentation width differs betweeen msgid and msgstr
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   103
    """
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   104
    idindent = len(pe.msgid) - len(pe.msgid.lstrip())
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   105
    strindent = len(pe.msgstr) - len(pe.msgstr.lstrip())
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   106
    if idindent != strindent:
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   107
        yield "initial indentation width differs betweeen msgid and msgstr"
6afbfb9b1af1 i18n: check equality of initial indentation between msgid and msgstr
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20514
diff changeset
   108
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   109
####################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   110
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   111
def check(pofile, fatal=True, warning=False):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   112
    targetlevel = { 'fatal': fatal, 'warning': warning }
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   113
    targetcheckers = [(checker, level)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   114
                      for checker, level in checkers
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   115
                      if targetlevel[level]]
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   116
    if not targetcheckers:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
        return []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   118
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   119
    detected = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   120
    for pe in pofile.translated_entries():
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   121
        errors = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   122
        for checker, level in targetcheckers:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
            if match(checker, pe):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   124
                errors.extend((level, checker.__name__, error)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   125
                              for error in checker(pe))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   126
        if errors:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   127
            detected.append((pe, errors))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   128
    return detected
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   129
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   130
########################################
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   131
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   132
if __name__ == "__main__":
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   133
    import sys
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   134
    import optparse
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   135
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
    optparser = optparse.OptionParser("""%prog [options] pofile ...
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   137
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   138
This checks Mercurial specific translation problems in specified
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   139
'*.po' files.
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   140
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   141
Each detected problems are shown in the format below::
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   142
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   143
    filename:linenum:type(checker): problem detail .....
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   144
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   145
"type" is "fatal" or "warning". "checker" is the name of the function
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   146
detecting corresponded error.
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   147
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   148
Checking by checker "foo" on the specific msgstr can be suppressed by
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   149
the "translator comment" like below. Multiple "no-xxxx-check" should
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   150
be separated by whitespaces::
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   151
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   152
    # no-foo-check
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   153
    msgid = "....."
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   154
    msgstr = "....."
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   155
""")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   156
    optparser.add_option("", "--warning",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   157
                         help="show also warning level problems",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   158
                         action="store_true")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   159
    optparser.add_option("", "--doctest",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   160
                         help="run doctest of this tool, instead of check",
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   161
                         action="store_true")
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   162
    (options, args) = optparser.parse_args()
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   163
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   164
    if options.doctest:
20164
1ddf4409229f tests: fix missing import in check-translations
Matt Mackall <mpm@selenic.com>
parents: 20158
diff changeset
   165
        import os
20158
209e04a06467 tests: fix Mac doctest escape code garbage for check-translations
Matt Mackall <mpm@selenic.com>
parents: 20152
diff changeset
   166
        if 'TERM' in os.environ:
209e04a06467 tests: fix Mac doctest escape code garbage for check-translations
Matt Mackall <mpm@selenic.com>
parents: 20152
diff changeset
   167
            del os.environ['TERM']
20152
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   168
        import doctest
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   169
        failures, tests = doctest.testmod()
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   170
        sys.exit(failures and 1 or 0)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   171
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   172
    # replace polib._POFileParser to show linenum of problematic msgstr
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   173
    class ExtPOFileParser(polib._POFileParser):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   174
        def process(self, symbol, linenum):
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   175
            super(ExtPOFileParser, self).process(symbol, linenum)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   176
            if symbol == 'MS': # msgstr
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   177
                self.current_entry.linenum = linenum
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   178
    polib._POFileParser = ExtPOFileParser
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   179
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   180
    detected = []
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   181
    warning = options.warning
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   182
    for f in args:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   183
        detected.extend((f, pe, errors)
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   184
                        for pe, errors in check(polib.pofile(f),
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   185
                                                warning=warning))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   186
    if detected:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   187
        for f, pe, errors in detected:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   188
            for level, checker, error in errors:
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   189
                sys.stderr.write('%s:%d:%s(%s): %s\n'
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   190
                                 % (f, pe.linenum, level, checker, error))
84939b728749 i18n: add the tool to check Mercurial specific translation problems in *.po
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   191
        sys.exit(1)