blob: 10887ce338a96e62409c780c29161c96ff3356de [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
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000011
12# Location of the alias file
Antoine Pitrou0c70d2d2010-04-11 22:35:34 +000013LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000014
15def parse(filename):
16
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020017 with open(filename, encoding='latin1') as f:
18 lines = list(f)
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000019 data = {}
20 for line in lines:
21 line = line.strip()
22 if not line:
23 continue
24 if line[:1] == '#':
25 continue
26 locale, alias = line.split()
Serhiy Storchaka5eb01532013-12-26 21:20:59 +020027 # Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8
28 if '@' in alias:
29 alias_lang, _, alias_mod = alias.partition('@')
30 if '.' in alias_mod:
31 alias_mod, _, alias_enc = alias_mod.partition('.')
32 alias = alias_lang + '.' + alias_enc + '@' + alias_mod
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000033 # Strip ':'
34 if locale[-1] == ':':
35 locale = locale[:-1]
36 # Lower-case locale
37 locale = locale.lower()
38 # Ignore one letter locale mappings (except for 'c')
39 if len(locale) == 1 and locale != 'c':
40 continue
41 # Normalize encoding, if given
42 if '.' in locale:
43 lang, encoding = locale.split('.')[:2]
44 encoding = encoding.replace('-', '')
45 encoding = encoding.replace('_', '')
46 locale = lang + '.' + encoding
Marc-André Lemburgb4cebd42004-12-13 19:56:01 +000047 if encoding.lower() == 'utf8':
48 # Ignore UTF-8 mappings - this encoding should be
49 # available for all locales
50 continue
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000051 data[locale] = alias
52 return data
53
54def pprint(data):
Georg Brandlbf82e372008-05-16 17:02:34 +000055 items = sorted(data.items())
56 for k, v in items:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020057 print(' %-40s%a,' % ('%a:' % k, v))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000058
59def print_differences(data, olddata):
Georg Brandlbf82e372008-05-16 17:02:34 +000060 items = sorted(olddata.items())
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000061 for k, v in items:
Georg Brandlbf82e372008-05-16 17:02:34 +000062 if k not in data:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020063 print('# removed %a' % k)
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000064 elif olddata[k] != data[k]:
Serhiy Storchaka55c6cc42013-12-23 18:56:08 +020065 print('# updated %a -> %a to %a' % \
Collin Winter6afaeb72007-08-03 17:06:41 +000066 (k, olddata[k], data[k]))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000067 # Additions are not mentioned
Tim Peters5a9fb3c2005-01-07 16:01:32 +000068
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +020069def optimize(data):
70 locale_alias = locale.locale_alias
71 locale.locale_alias = data.copy()
72 for k, v in data.items():
73 del locale.locale_alias[k]
74 if locale.normalize(k) != v:
75 locale.locale_alias[k] = v
76 newdata = locale.locale_alias
77 errors = check(data)
78 locale.locale_alias = locale_alias
79 if errors:
80 sys.exit(1)
81 return newdata
82
83def check(data):
84 # Check that all alias definitions from the X11 file
85 # are actually mapped to the correct alias locales.
86 errors = 0
87 for k, v in data.items():
88 if locale.normalize(k) != v:
89 print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),
90 file=sys.stderr)
91 errors += 1
92 return errors
93
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000094if __name__ == '__main__':
95 data = locale.locale_alias.copy()
96 data.update(parse(LOCALE_ALIAS))
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +020097 data = optimize(data)
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000098 print_differences(data, locale.locale_alias)
Collin Winter6afaeb72007-08-03 17:06:41 +000099 print()
100 print('locale_alias = {')
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +0000101 pprint(data)
Collin Winter6afaeb72007-08-03 17:06:41 +0000102 print('}')