]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | #!/usr/bin/env python\r |
2 | """\r | |
3 | Convert the X11 locale.alias file into a mapping dictionary suitable\r | |
4 | for locale.py.\r | |
5 | \r | |
6 | Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.\r | |
7 | \r | |
8 | """\r | |
9 | import locale\r | |
10 | \r | |
11 | # Location of the alias file\r | |
12 | LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'\r | |
13 | \r | |
14 | def parse(filename):\r | |
15 | \r | |
16 | f = open(filename)\r | |
17 | lines = f.read().splitlines()\r | |
18 | data = {}\r | |
19 | for line in lines:\r | |
20 | line = line.strip()\r | |
21 | if not line:\r | |
22 | continue\r | |
23 | if line[:1] == '#':\r | |
24 | continue\r | |
25 | locale, alias = line.split()\r | |
26 | # Strip ':'\r | |
27 | if locale[-1] == ':':\r | |
28 | locale = locale[:-1]\r | |
29 | # Lower-case locale\r | |
30 | locale = locale.lower()\r | |
31 | # Ignore one letter locale mappings (except for 'c')\r | |
32 | if len(locale) == 1 and locale != 'c':\r | |
33 | continue\r | |
34 | # Normalize encoding, if given\r | |
35 | if '.' in locale:\r | |
36 | lang, encoding = locale.split('.')[:2]\r | |
37 | encoding = encoding.replace('-', '')\r | |
38 | encoding = encoding.replace('_', '')\r | |
39 | locale = lang + '.' + encoding\r | |
40 | if encoding.lower() == 'utf8':\r | |
41 | # Ignore UTF-8 mappings - this encoding should be\r | |
42 | # available for all locales\r | |
43 | continue\r | |
44 | data[locale] = alias\r | |
45 | return data\r | |
46 | \r | |
47 | def pprint(data):\r | |
48 | \r | |
49 | items = data.items()\r | |
50 | items.sort()\r | |
51 | for k,v in items:\r | |
52 | print ' %-40s%r,' % ('%r:' % k, v)\r | |
53 | \r | |
54 | def print_differences(data, olddata):\r | |
55 | \r | |
56 | items = olddata.items()\r | |
57 | items.sort()\r | |
58 | for k, v in items:\r | |
59 | if not data.has_key(k):\r | |
60 | print '# removed %r' % k\r | |
61 | elif olddata[k] != data[k]:\r | |
62 | print '# updated %r -> %r to %r' % \\r | |
63 | (k, olddata[k], data[k])\r | |
64 | # Additions are not mentioned\r | |
65 | \r | |
66 | if __name__ == '__main__':\r | |
67 | data = locale.locale_alias.copy()\r | |
68 | data.update(parse(LOCALE_ALIAS))\r | |
69 | print_differences(data, locale.locale_alias)\r | |
70 | print\r | |
71 | print 'locale_alias = {'\r | |
72 | pprint(data)\r | |
73 | print '}'\r |