Debian Codegraph @

private/online-check-gettext-data

raw
1 #!/usr/bin/env python3
2 # encoding=UTF-8
3
4 # Copyright © 2012-2025 Jakub Wilk <[email protected]>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the “Software”), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
23
24 import re
25 import sys
26 import urllib.request
27
28 sys.path[0] += '/..'
29
30 from lib import gettext
31 from lib import ling
32
33 int(0_0) # Python >= 3.6 is required
34
35 def strip_plural_forms(s):
36 # Remove superfluous parentheses around the plural formula:
37 return re.sub(r'\bplural=\((.*?)\);$', r'plural=\1;', s)
38
39 def do_plurals():
40 regexp = re.compile(r'\s+{\s*"([a-zA-Z_]+)".*"(nplurals.*?)"')
41 url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/plural-table.c'
42 okay = set()
43 with urllib.request.urlopen(url) as file:
44 for line in file:
45 line = line.decode('ASCII').rstrip()
46 match = regexp.match(line)
47 if match is None:
48 continue
49 language, gettext_plural_forms = match.groups()
50 language = ling.parse_language(language)
51 gettext_plural_forms = strip_plural_forms(gettext_plural_forms)
52 our_plural_forms = language.get_plural_forms() or []
53 if gettext_plural_forms not in our_plural_forms:
54 print(f'[{language}]')
55 if our_plural_forms:
56 if len(our_plural_forms) == 1:
57 print('# plural-forms =', *our_plural_forms)
58 else:
59 print('# plural-forms =')
60 for pf in our_plural_forms:
61 print('# ', pf)
62 print('plural-forms =', gettext_plural_forms)
63 print()
64 else:
65 okay.add(str(language))
66 n_okay = len(okay)
67 print(f'# No plural-forms changes required for {n_okay} languages')
68 print()
69
70 blacklist = {
71 # https://lists.gnu.org/archive/html/bug-gettext/2015-06/msg00057.html
72 ('pap', 'AN'),
73 }
74
75 def do_languages():
76 url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/msginit.c'
77 okay = set()
78 with urllib.request.urlopen(url) as file:
79 contents = file.read()
80 match = re.compile(br'locales_with_principal_territory\s*\[\]\s*=\s*[{](.*?)[}]', re.DOTALL).search(contents)
81 if match is None:
82 raise ValueError
83 contents = match.group(1)
84 contents = re.compile(br'/[*].*?[*]/', re.DOTALL).sub(b'', contents)
85 contents = contents.decode('ASCII')
86 data = {}
87 primary_languages = set(ling.get_primary_languages())
88 for item in re.findall(r'"(\w+)"', contents):
89 language, gettext_cc = item.split('_')
90 if language not in primary_languages:
91 continue
92 data[language] = gettext_cc
93 our_cc = ling.parse_language(language).get_principal_territory_code()
94 if our_cc != gettext_cc:
95 if (language, gettext_cc) in blacklist:
96 if our_cc is not None:
97 okay.add(language)
98 continue
99 print(f'[{language}]')
100 if our_cc is not None:
101 print('# WAS: principal-territory =', our_cc)
102 if ling.lookup_territory_code(gettext_cc) is None:
103 print('# BAD: principal-territory =', gettext_cc)
104 else:
105 print('principal-territory =', gettext_cc)
106 print()
107 else:
108 okay.add(language)
109 for language in sorted(primary_languages):
110 if '_' in language:
111 continue
112 cc = ling.parse_language(language).get_principal_territory_code()
113 if cc is None:
114 continue
115 if language not in data:
116 print(f'# WAS: [{language}]')
117 print('# WAS: principal-territory =', cc)
118 print()
119 n_okay = len(okay)
120 print(f'# No principal-territory changes required for {n_okay} languages')
121 print()
122
123 def do_string_formats():
124 url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/message.c'
125 with urllib.request.urlopen(url) as file:
126 contents = file.read()
127 match = re.compile(br'\sformat_language\s*\[NFORMATS\]\s*=\s*[{](.*?)[}]', re.DOTALL).search(contents)
128 if match is None:
129 raise ValueError
130 contents = match.group(1)
131 contents = re.compile(br'/[*].*?[*]/', re.DOTALL).sub(b'', contents)
132 contents = contents.decode('ASCII')
133 their_formats = frozenset(
134 re.findall(r'"([\w-]+)"', contents)
135 )
136 our_formats = frozenset(
137 gettext.string_formats.keys()
138 )
139 difference = their_formats ^ our_formats
140 print('[formats]')
141 for fmt in difference:
142 if fmt in our_formats:
143 print(f'# MISSING: {fmt} = ...')
144 else:
145 print(f'{fmt} = # TODO')
146 n_okay = len(our_formats - difference)
147 print(f'# No changes required for {n_okay} string formats')
148 print()
149
150 def main():
151 do_plurals()
152 do_languages()
153 do_string_formats()
154
155 if __name__ == '__main__':
156 main()
157
158 # vim:ts=4 sts=4 sw=4 et