ยปCore Development>Code coverage>Tools/i18n/makelocalealias.py

Python code coverage for Tools/i18n/makelocalealias.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a"""
3n/a Convert the X11 locale.alias file into a mapping dictionary suitable
4n/a for locale.py.
5n/a
6n/a Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.
7n/a
8n/a"""
9n/aimport locale
10n/aimport sys
11n/a_locale = locale
12n/a
13n/a# Location of the X11 alias file.
14n/aLOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
15n/a# Location of the glibc SUPPORTED locales file.
16n/aSUPPORTED = '/usr/share/i18n/SUPPORTED'
17n/a
18n/adef parse(filename):
19n/a
20n/a with open(filename, encoding='latin1') as f:
21n/a lines = list(f)
22n/a data = {}
23n/a for line in lines:
24n/a line = line.strip()
25n/a if not line:
26n/a continue
27n/a if line[:1] == '#':
28n/a continue
29n/a locale, alias = line.split()
30n/a # Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8
31n/a if '@' in alias:
32n/a alias_lang, _, alias_mod = alias.partition('@')
33n/a if '.' in alias_mod:
34n/a alias_mod, _, alias_enc = alias_mod.partition('.')
35n/a alias = alias_lang + '.' + alias_enc + '@' + alias_mod
36n/a # Strip ':'
37n/a if locale[-1] == ':':
38n/a locale = locale[:-1]
39n/a # Lower-case locale
40n/a locale = locale.lower()
41n/a # Ignore one letter locale mappings (except for 'c')
42n/a if len(locale) == 1 and locale != 'c':
43n/a continue
44n/a # Normalize encoding, if given
45n/a if '.' in locale:
46n/a lang, encoding = locale.split('.')[:2]
47n/a encoding = encoding.replace('-', '')
48n/a encoding = encoding.replace('_', '')
49n/a locale = lang + '.' + encoding
50n/a data[locale] = alias
51n/a return data
52n/a
53n/adef parse_glibc_supported(filename):
54n/a
55n/a with open(filename, encoding='latin1') as f:
56n/a lines = list(f)
57n/a data = {}
58n/a for line in lines:
59n/a line = line.strip()
60n/a if not line:
61n/a continue
62n/a if line[:1] == '#':
63n/a continue
64n/a line = line.replace('/', ' ').strip()
65n/a line = line.rstrip('\\').rstrip()
66n/a words = line.split()
67n/a if len(words) != 2:
68n/a continue
69n/a alias, alias_encoding = words
70n/a # Lower-case locale
71n/a locale = alias.lower()
72n/a # Normalize encoding, if given
73n/a if '.' in locale:
74n/a lang, encoding = locale.split('.')[:2]
75n/a encoding = encoding.replace('-', '')
76n/a encoding = encoding.replace('_', '')
77n/a locale = lang + '.' + encoding
78n/a # Add an encoding to alias
79n/a alias, _, modifier = alias.partition('@')
80n/a alias = _locale._replace_encoding(alias, alias_encoding)
81n/a if modifier and not (modifier == 'euro' and alias_encoding == 'ISO-8859-15'):
82n/a alias += '@' + modifier
83n/a data[locale] = alias
84n/a return data
85n/a
86n/adef pprint(data):
87n/a items = sorted(data.items())
88n/a for k, v in items:
89n/a print(' %-40s%a,' % ('%a:' % k, v))
90n/a
91n/adef print_differences(data, olddata):
92n/a items = sorted(olddata.items())
93n/a for k, v in items:
94n/a if k not in data:
95n/a print('# removed %a' % k)
96n/a elif olddata[k] != data[k]:
97n/a print('# updated %a -> %a to %a' % \
98n/a (k, olddata[k], data[k]))
99n/a # Additions are not mentioned
100n/a
101n/adef optimize(data):
102n/a locale_alias = locale.locale_alias
103n/a locale.locale_alias = data.copy()
104n/a for k, v in data.items():
105n/a del locale.locale_alias[k]
106n/a if locale.normalize(k) != v:
107n/a locale.locale_alias[k] = v
108n/a newdata = locale.locale_alias
109n/a errors = check(data)
110n/a locale.locale_alias = locale_alias
111n/a if errors:
112n/a sys.exit(1)
113n/a return newdata
114n/a
115n/adef check(data):
116n/a # Check that all alias definitions from the X11 file
117n/a # are actually mapped to the correct alias locales.
118n/a errors = 0
119n/a for k, v in data.items():
120n/a if locale.normalize(k) != v:
121n/a print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),
122n/a file=sys.stderr)
123n/a errors += 1
124n/a return errors
125n/a
126n/aif __name__ == '__main__':
127n/a import argparse
128n/a parser = argparse.ArgumentParser()
129n/a parser.add_argument('--locale-alias', default=LOCALE_ALIAS,
130n/a help='location of the X11 alias file '
131n/a '(default: %a)' % LOCALE_ALIAS)
132n/a parser.add_argument('--glibc-supported', default=SUPPORTED,
133n/a help='location of the glibc SUPPORTED locales file '
134n/a '(default: %a)' % SUPPORTED)
135n/a args = parser.parse_args()
136n/a
137n/a data = locale.locale_alias.copy()
138n/a data.update(parse_glibc_supported(args.glibc_supported))
139n/a data.update(parse(args.locale_alias))
140n/a while True:
141n/a # Repeat optimization while the size is decreased.
142n/a n = len(data)
143n/a data = optimize(data)
144n/a if len(data) == n:
145n/a break
146n/a print_differences(data, locale.locale_alias)
147n/a print()
148n/a print('locale_alias = {')
149n/a pprint(data)
150n/a print('}')