blob: 68544ac27b9b210978de0800090b99f0f10da518 [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
10
11# Location of the alias file
Antoine Pitrou0c70d2d2010-04-11 22:35:34 +000012LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000013
14def parse(filename):
15
16 f = open(filename)
17 lines = f.read().splitlines()
18 data = {}
19 for line in lines:
20 line = line.strip()
21 if not line:
22 continue
23 if line[:1] == '#':
24 continue
25 locale, alias = line.split()
26 # Strip ':'
27 if locale[-1] == ':':
28 locale = locale[:-1]
29 # Lower-case locale
30 locale = locale.lower()
31 # Ignore one letter locale mappings (except for 'c')
32 if len(locale) == 1 and locale != 'c':
33 continue
34 # Normalize encoding, if given
35 if '.' in locale:
36 lang, encoding = locale.split('.')[:2]
37 encoding = encoding.replace('-', '')
38 encoding = encoding.replace('_', '')
39 locale = lang + '.' + encoding
Marc-André Lemburgb4cebd42004-12-13 19:56:01 +000040 if encoding.lower() == 'utf8':
41 # Ignore UTF-8 mappings - this encoding should be
42 # available for all locales
43 continue
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000044 data[locale] = alias
45 return data
46
47def pprint(data):
Georg Brandlbf82e372008-05-16 17:02:34 +000048 items = sorted(data.items())
49 for k, v in items:
Collin Winter6afaeb72007-08-03 17:06:41 +000050 print(' %-40s%r,' % ('%r:' % k, v))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000051
52def print_differences(data, olddata):
Georg Brandlbf82e372008-05-16 17:02:34 +000053 items = sorted(olddata.items())
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000054 for k, v in items:
Georg Brandlbf82e372008-05-16 17:02:34 +000055 if k not in data:
Collin Winter6afaeb72007-08-03 17:06:41 +000056 print('# removed %r' % k)
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000057 elif olddata[k] != data[k]:
Collin Winter6afaeb72007-08-03 17:06:41 +000058 print('# updated %r -> %r to %r' % \
59 (k, olddata[k], data[k]))
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000060 # Additions are not mentioned
Tim Peters5a9fb3c2005-01-07 16:01:32 +000061
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000062if __name__ == '__main__':
63 data = locale.locale_alias.copy()
64 data.update(parse(LOCALE_ALIAS))
65 print_differences(data, locale.locale_alias)
Collin Winter6afaeb72007-08-03 17:06:41 +000066 print()
67 print('locale_alias = {')
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000068 pprint(data)
Collin Winter6afaeb72007-08-03 17:06:41 +000069 print('}')