File: l10n_reset_en_GB.py

package info (click to toggle)
trac 1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,452 kB
  • ctags: 8,602
  • sloc: python: 71,206; makefile: 358; sh: 79; xml: 10
file content (70 lines) | stat: -rwxr-xr-x 2,185 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Edgewall Software
# Copyright (C) 2013 Christian Boos <cboos@edgewall.org>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/.

"""

L10N tool which copies all msgid to the msgstr.

This can be useful to verify the actual changes in the en_UK message
catalogs.

"""

import re


msgid_msgstr_re = re.compile(r'''
    (                                             # \1 "en_US"
                                          \r?\n
                msgid \s ".*"             \r?\n
        (?: (?: msgid_plural \s )? ".*"   \r?\n
        )*
    )
    (                                             # \2 "en_GB"
                msgstr.* \s ".*"          \r?\n
        (?: (?: msgstr.* \s )? ".*"       \r?\n
        )*                                \r?\n
    )
    ''', re.MULTILINE | re.VERBOSE)

def reset_file(path):
    with open(path, 'rb+') as f:
        eol = '\r\n'
        content = f.read()
        if eol not in content:
            eol = '\n'
        def reset_msgstr(m):
            msgid, msgstr = m.groups()
            if '\nmsgid_plural' in msgid:
                msgstr = (msgid
                          .replace(eol + 'msgid_plural', eol + 'msgstr[1]')
                          .replace(eol + 'msgid', 'msgstr[0]'))
            else:
                msgstr = msgid.replace(eol + 'msgid', 'msgstr')
            return msgid + msgstr + eol
        sanitized, nsub = msgid_msgstr_re.subn(reset_msgstr, content)
        if nsub:
            print("reset %d messages to en_US in %s" % (nsub, path))
            f.seek(0)
            f.write(sanitized)
            f.truncate()
        else:
            print("no messages found in %s" % (path,))


if __name__ == '__main__':
    import sys
    for path in sys.argv[1:]:
        reset_file(path)