File: i18n_update.py

package info (click to toggle)
linux-show-player 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,776 kB
  • sloc: python: 12,388; sh: 152; makefile: 17; xml: 8
file content (124 lines) | stat: -rwxr-xr-x 4,214 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import re
import subprocess
import sys

try:
    from os import scandir
except ImportError:
    from scandir import scandir


parser = argparse.ArgumentParser(description='i18n utility for LiSP')
parser.add_argument('locales', nargs='*',
                    help='Locales of witch generate translations')
parser.add_argument('-n', '--noobsolete', help='Discard obsolete strings',
                    action='store_true')
parser.add_argument('-q', '--qm', help='Release .qm files', action='store_true')


args = parser.parse_args()

# pylupdate command with arguments
PYLUPDATE_CMD = ['pylupdate5']
if args.noobsolete:
    PYLUPDATE_CMD.append('-noobsolete')


def existing_locales():
    for entry in scandir('lisp/i18n'):
        if entry.name.startswith('lisp_') and entry.name.endswith('.ts'):
            yield entry.name[5:-3]

# Locales of which generate translations files
LOCALES = args.locales
if not LOCALES:
    LOCALES = list(existing_locales())
    print('>>> UPDATE EXISTING:', ', '.join(LOCALES))


def search_files(root, exclude=(), extensions=()):
    exc_regex = '^(' + '|'.join(exclude) + ').*' if exclude else '$^'
    ext_regex = '.*\.(' + '|'.join(extensions) + ')$' if extensions else '.*'

    for path, directories, filenames in os.walk(root):
        if re.match(exc_regex, path):
            continue

        path = os.path.relpath(path, root)

        for filename in filenames:
            if re.match(ext_regex, filename):
                if path:
                    filename = os.path.join(path, filename)
                yield filename


def create_pro_file(root, exclude=(), extensions=('py',)):
    base_name = os.path.basename(os.path.normpath(root))
    translations = 'TRANSLATIONS = '
    for local in LOCALES:
        translations += os.path.join('i18n', base_name + '_' + local + '.ts ')

    files = 'SOURCES = ' + ' '.join(search_files(root, exclude, extensions))

    with open(os.path.join(root, base_name + '.pro'), mode='w') as pro_file:
        pro_file.write(translations)
        pro_file.write(os.linesep)
        pro_file.write(files)


def generate_for_submodules(path, qm=False):
    # Here "modules" is used generically
    modules = [entry.path for entry in scandir(path) if entry.is_dir()]
    for module in modules:
        if os.path.exists(os.path.join(module, 'i18n')):
            create_pro_file(module)
            p_file = os.path.join(module, os.path.basename(module) + '.pro')
            if qm:
                subprocess.run(['lrelease', p_file],
                               stdout=sys.stdout,
                               stderr=sys.stderr)
            else:
                subprocess.run(PYLUPDATE_CMD + [p_file],
                               stdout=sys.stdout,
                               stderr=sys.stderr)


print('>>> UPDATE TRANSLATIONS FOR APPLICATION')
create_pro_file('lisp', exclude=('lisp/modules/', 'lisp/plugins/'))
if args.qm:
    subprocess.run(['lrelease', 'lisp/lisp.pro'],
                   stdout=sys.stdout,
                   stderr=sys.stderr)
else:
    subprocess.run(PYLUPDATE_CMD + ['lisp/lisp.pro'],
                   stdout=sys.stdout,
                   stderr=sys.stderr)

print('>>> UPDATE TRANSLATIONS FOR MODULES')
generate_for_submodules('lisp/modules', qm=args.qm)

print('>>> UPDATE TRANSLATIONS FOR PLUGINS')
generate_for_submodules('lisp/plugins', qm=args.qm)