blob: b407a8a643be7c047d93c21405cb7ae0295ca10f [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +00002"""
3 Convert the X11 locale.alias file into a mapping dictionary suitable
4 for locale.py.
5
6 Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.
7
8"""
9import locale
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +020010import sys
Serhiy Storchakaea4f0572014-10-01 23:42:30 +030011_locale = locale
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000012
Serhiy Storchaka8276d872014-10-02 10:38:12 +030013# Location of the X11 alias file.
Antoine Pitrou0c70d2d2010-04-11 22:35:34 +000014LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
Serhiy Storchaka8276d872014-10-02 10:38:12 +030015# Location of the glibc SUPPORTED locales file.
16SUPPORTED = '/usr/share/i18n/SUPPORTED'
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000017
18def parse(filename):
19
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020020 with open(filename, encoding='latin1') as f:
21 lines = list(f)
Serhiy Storchakaa3f19c32018-05-06 10:52:38 +030022 # Remove mojibake in /usr/share/X11/locale/locale.alias.
23 # b'\xef\xbf\xbd' == '\ufffd'.encode('utf-8')
24 lines = [line for line in lines if '\xef\xbf\xbd' not in line]
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000025 data = {}
26 for line in lines:
27 line = line.strip()
28 if not line:
29 continue
30 if line[:1] == '#':
31 continue
32 locale, alias = line.split()
Serhiy Storchaka5eb01532013-12-26 21:20:59 +020033 # Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8
34 if '@' in alias:
35 alias_lang, _, alias_mod = alias.partition('@')
36 if '.' in alias_mod:
37 alias_mod, _, alias_enc = alias_mod.partition('.')
38 alias = alias_lang + '.' + alias_enc + '@' + alias_mod
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000039 # Strip ':'
40 if locale[-1] == ':':
41 locale = locale[:-1]
42 # Lower-case locale
43 locale = locale.lower()
44 # Ignore one letter locale mappings (except for 'c')
45 if len(locale) == 1 and locale != 'c':
46 continue
47 # Normalize encoding, if given
48 if '.' in locale:
49 lang, encoding = locale.split('.')[:2]
50 encoding = encoding.replace('-', '')
51 encoding = encoding.replace('_', '')
52 locale = lang + '.' + encoding
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000053 data[locale] = alias
54 return data
55
Serhiy Storchakaea4f0572014-10-01 23:42:30 +030056def parse_glibc_supported(filename):
57
58 with open(filename, encoding='latin1') as f:
59 lines = list(f)
60 data = {}
61 for line in lines:
62 line = line.strip()
63 if not line:
64 continue
65 if line[:1] == '#':
66 continue
Serhiy Storchaka8276d872014-10-02 10:38:12 +030067 line = line.replace('/', ' ').strip()
Serhiy Storchakaea4f0572014-10-01 23:42:30 +030068 line = line.rstrip('\\').rstrip()
Serhiy Storchaka8276d872014-10-02 10:38:12 +030069 words = line.split()
70 if len(words) != 2:
71 continue
72 alias, alias_encoding = words
Serhiy Storchakaea4f0572014-10-01 23:42:30 +030073 # Lower-case locale
74 locale = alias.lower()
75 # Normalize encoding, if given
76 if '.' in locale:
77 lang, encoding = locale.split('.')[:2]
78 encoding = encoding.replace('-', '')
79 encoding = encoding.replace('_', '')
80 locale = lang + '.' + encoding
81 # Add an encoding to alias
82 alias, _, modifier = alias.partition('@')
83 alias = _locale._replace_encoding(alias, alias_encoding)
84 if modifier and not (modifier == 'euro' and alias_encoding == 'ISO-8859-15'):
85 alias += '@' + modifier
86 data[locale] = alias
87 return data
88
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000089def pprint(data):
Georg Brandlbf82e372008-05-16 17:02:34 +000090 items = sorted(data.items())
91 for k, v in items:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020092 print(' %-40s%a,' % ('%a:' % k, v))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000093
94def print_differences(data, olddata):
Georg Brandlbf82e372008-05-16 17:02:34 +000095 items = sorted(olddata.items())
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000096 for k, v in items:
Georg Brandlbf82e372008-05-16 17:02:34 +000097 if k not in data:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020098 print('# removed %a' % k)
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000099 elif olddata[k] != data[k]:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +0200100 print('# updated %a -> %a to %a' % \
Collin Winter6afaeb72007-08-03 17:06:41 +0000101 (k, olddata[k], data[k]))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000102 # Additions are not mentioned
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000103
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200104def optimize(data):
105 locale_alias = locale.locale_alias
106 locale.locale_alias = data.copy()
107 for k, v in data.items():
108 del locale.locale_alias[k]
109 if locale.normalize(k) != v:
110 locale.locale_alias[k] = v
111 newdata = locale.locale_alias
112 errors = check(data)
113 locale.locale_alias = locale_alias
114 if errors:
115 sys.exit(1)
116 return newdata
117
118def check(data):
119 # Check that all alias definitions from the X11 file
120 # are actually mapped to the correct alias locales.
121 errors = 0
122 for k, v in data.items():
123 if locale.normalize(k) != v:
124 print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),
125 file=sys.stderr)
126 errors += 1
127 return errors
128
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000129if __name__ == '__main__':
Serhiy Storchakaea4f0572014-10-01 23:42:30 +0300130 import argparse
131 parser = argparse.ArgumentParser()
132 parser.add_argument('--locale-alias', default=LOCALE_ALIAS,
133 help='location of the X11 alias file '
134 '(default: %a)' % LOCALE_ALIAS)
Serhiy Storchaka8276d872014-10-02 10:38:12 +0300135 parser.add_argument('--glibc-supported', default=SUPPORTED,
136 help='location of the glibc SUPPORTED locales file '
137 '(default: %a)' % SUPPORTED)
Serhiy Storchakaea4f0572014-10-01 23:42:30 +0300138 args = parser.parse_args()
139
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000140 data = locale.locale_alias.copy()
Benjamin Peterson02371e02017-03-07 22:03:13 -0800141 data.update(parse_glibc_supported(args.glibc_supported))
Benjamin Petersondf828082017-03-19 23:49:43 -0700142 data.update(parse(args.locale_alias))
Serhiy Storchaka5189ee52014-10-02 10:21:43 +0300143 while True:
144 # Repeat optimization while the size is decreased.
145 n = len(data)
146 data = optimize(data)
147 if len(data) == n:
148 break
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000149 print_differences(data, locale.locale_alias)
Collin Winter6afaeb72007-08-03 17:06:41 +0000150 print()
151 print('locale_alias = {')
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000152 pprint(data)
Collin Winter6afaeb72007-08-03 17:06:41 +0000153 print('}')