File: make_translation.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (150 lines) | stat: -rw-r--r-- 5,623 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# Copyright (c), 2016-2022, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
# type: ignore
#
"""Translation files generator utility."""

if __name__ == '__main__':
    import argparse
    import os
    import sys
    import subprocess
    from pathlib import Path

    COPYRIGHT_HOLDER = r", 2016, SISSA (International School for Advanced Studies)."

    parser = argparse.ArgumentParser(
        description="Translation files generator utility for xmlschema"
    )
    parser.add_argument(
        '-L', '--directory', metavar='LOCALE-DIR', type=str, default=None,
        help="use a custom locale directory (for extra local translations)"
    )
    parser.add_argument(
        '-t', '--template', action='store_true', default=False,
        help="generate xmlschema.pot template file"
    )
    parser.add_argument(
        '-u', '--update', action='store_true', default=False,
        help="update locale xmlschema.po file from xmlschema.pot template"
    )
    parser.add_argument(
        '-c', '--compile', action='store_true', default=False,
        help="generate xmlschema.mo file from locale xmlschema.po"
    )
    parser.add_argument('languages', type=str, nargs='*',
                        help="process locale files for languages")
    args = parser.parse_args()

    if args.directory is not None:
        locale_dir = Path(args.directory).resolve()
        os.chdir(Path(__file__).parent.parent)
        try:
            locale_dir = locale_dir.relative_to(os.getcwd())
        except ValueError:
            pass  # Not a subdir, use the absolute path.
    else:
        os.chdir(Path(__file__).parent.parent)
        locale_dir = Path('xmlschema/locale')
    assert locale_dir.is_dir(), 'locale directory not found!'

    package_dir = Path('xmlschema')
    assert package_dir.is_dir(), 'xmlschema/ package directory not found!'

    template_file = locale_dir.joinpath('xmlschema.pot')
    if args.template:
        print("+++ Generate the template file ...")

        status, xgettext_cmd = subprocess.getstatusoutput('which xgettext')
        assert status == 0, "xgettext command is not available!"

        cmd = [xgettext_cmd,
               f'--copyright-holder={COPYRIGHT_HOLDER}',
               '--package-name=xmlschema',
               '--from-code=UTF-8',
               '-o', str(template_file)]
        cmd.extend(str(path) for path in package_dir.glob('**/*.py'))
        process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        stderr = process.stderr.decode('utf-8').strip()
        if stderr:
            print(stderr)
            sys.exit(1)

        # .POT template file fixes
        with template_file.open() as fp:
            text = fp.read().replace('charset=CHARSET', 'charset=UTF-8', 1)
        with template_file.open(mode='w') as fp:
            fp.write(text)

        print(f'  ... file {str(template_file)} written\n')

    if not args.languages:
        print("No language code provided, exit ...")
        sys.exit()

    if args.update:
        status, msgmerge_cmd = subprocess.getstatusoutput('which msgmerge')
        assert status == 0, "msgmerge command is not available!"

        for lang in args.languages:
            print(f"+++ Update the .po file for language {lang!r}")

            po_file = locale_dir.joinpath(f'{lang}/LC_MESSAGES/xmlschema.po')
            if not po_file.exists():
                po_file.parent.mkdir(parents=True, exist_ok=True)

                status, msginit_cmd = subprocess.getstatusoutput('which msginit')
                assert status == 0, "msginit command is not available!"

                cmd = [msginit_cmd,
                       '-l', f'{lang}',
                       '-o', str(po_file),
                       '-i', str(template_file)]
                process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

                stderr = process.stderr.decode('utf-8').strip()
                if stderr:
                    print(stderr)

                print(f'  ... file {str(po_file)} initialized\n')

            cmd = [msgmerge_cmd, '-o', str(po_file), str(po_file), str(template_file)]
            process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

            stderr = process.stderr.decode('utf-8').strip()
            if 'done' not in stderr:
                print(stderr)
                sys.exit(1)

            print(f'  ... file {str(po_file)} updated\n')

    if args.compile:
        status, msgfmt_cmd = subprocess.getstatusoutput('which msgfmt')
        assert status == 0, "msgfmt command is not available!"

        for lang in args.languages:
            print(f"+++ Generate the .mo file for language {lang!r}")

            po_file = locale_dir.joinpath(f'{lang}/LC_MESSAGES/xmlschema.po')
            mo_file = locale_dir.joinpath(f'{lang}/LC_MESSAGES/xmlschema.mo')
            if not po_file.exists():
                print(f"  ... file {str(po_file)} doesn't exist!")
                sys.exit(1)

            cmd = [msgfmt_cmd, '-o', str(mo_file), str(po_file)]
            process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

            stderr = process.stderr.decode('utf-8').strip()
            if stderr:
                print(stderr)
                sys.exit(1)

            print(f'  ... file {str(mo_file)} written\n')