Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 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 | """ |
| 9 | import locale |
| 10 | |
| 11 | # Location of the alias file |
| 12 | LOCALE_ALIAS = '/usr/lib/X11/locale/locale.alias' |
| 13 | |
| 14 | def 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é Lemburg | b4cebd4 | 2004-12-13 19:56:01 +0000 | [diff] [blame] | 40 | if encoding.lower() == 'utf8': |
| 41 | # Ignore UTF-8 mappings - this encoding should be |
| 42 | # available for all locales |
| 43 | continue |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 44 | data[locale] = alias |
| 45 | return data |
| 46 | |
| 47 | def pprint(data): |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 48 | items = sorted(data.items()) |
| 49 | for k, v in items: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 50 | print(' %-40s%r,' % ('%r:' % k, v)) |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 51 | |
| 52 | def print_differences(data, olddata): |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 53 | items = sorted(olddata.items()) |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 54 | for k, v in items: |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 55 | if k not in data: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 56 | print('# removed %r' % k) |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 57 | elif olddata[k] != data[k]: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 58 | print('# updated %r -> %r to %r' % \ |
| 59 | (k, olddata[k], data[k])) |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 60 | # Additions are not mentioned |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 61 | |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 62 | if __name__ == '__main__': |
| 63 | data = locale.locale_alias.copy() |
| 64 | data.update(parse(LOCALE_ALIAS)) |
| 65 | print_differences(data, locale.locale_alias) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 66 | print() |
| 67 | print('locale_alias = {') |
Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 68 | pprint(data) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 69 | print('}') |