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