2010-03-11 18:53:45 -04:00
|
|
|
#!/usr/bin/env python3
|
2004-12-10 17:58:14 -04:00
|
|
|
"""
|
|
|
|
Convert the X11 locale.alias file into a mapping dictionary suitable
|
|
|
|
for locale.py.
|
|
|
|
|
|
|
|
Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import locale
|
2013-12-26 18:56:53 -04:00
|
|
|
import sys
|
2014-10-01 17:42:30 -03:00
|
|
|
_locale = locale
|
2004-12-10 17:58:14 -04:00
|
|
|
|
2014-10-02 04:38:12 -03:00
|
|
|
# Location of the X11 alias file.
|
2010-04-11 19:35:34 -03:00
|
|
|
LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
|
2014-10-02 04:38:12 -03:00
|
|
|
# Location of the glibc SUPPORTED locales file.
|
|
|
|
SUPPORTED = '/usr/share/i18n/SUPPORTED'
|
2004-12-10 17:58:14 -04:00
|
|
|
|
|
|
|
def parse(filename):
|
|
|
|
|
2013-12-23 12:56:08 -04:00
|
|
|
with open(filename, encoding='latin1') as f:
|
|
|
|
lines = list(f)
|
2018-05-06 04:52:38 -03:00
|
|
|
# Remove mojibake in /usr/share/X11/locale/locale.alias.
|
|
|
|
# b'\xef\xbf\xbd' == '\ufffd'.encode('utf-8')
|
|
|
|
lines = [line for line in lines if '\xef\xbf\xbd' not in line]
|
2004-12-10 17:58:14 -04:00
|
|
|
data = {}
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:1] == '#':
|
|
|
|
continue
|
|
|
|
locale, alias = line.split()
|
2013-12-26 15:20:59 -04:00
|
|
|
# Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8
|
|
|
|
if '@' in alias:
|
|
|
|
alias_lang, _, alias_mod = alias.partition('@')
|
|
|
|
if '.' in alias_mod:
|
|
|
|
alias_mod, _, alias_enc = alias_mod.partition('.')
|
|
|
|
alias = alias_lang + '.' + alias_enc + '@' + alias_mod
|
2004-12-10 17:58:14 -04:00
|
|
|
# Strip ':'
|
|
|
|
if locale[-1] == ':':
|
|
|
|
locale = locale[:-1]
|
|
|
|
# Lower-case locale
|
|
|
|
locale = locale.lower()
|
|
|
|
# Ignore one letter locale mappings (except for 'c')
|
|
|
|
if len(locale) == 1 and locale != 'c':
|
|
|
|
continue
|
|
|
|
# Normalize encoding, if given
|
|
|
|
if '.' in locale:
|
|
|
|
lang, encoding = locale.split('.')[:2]
|
|
|
|
encoding = encoding.replace('-', '')
|
|
|
|
encoding = encoding.replace('_', '')
|
|
|
|
locale = lang + '.' + encoding
|
|
|
|
data[locale] = alias
|
|
|
|
return data
|
|
|
|
|
2014-10-01 17:42:30 -03:00
|
|
|
def parse_glibc_supported(filename):
|
|
|
|
|
|
|
|
with open(filename, encoding='latin1') as f:
|
|
|
|
lines = list(f)
|
|
|
|
data = {}
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
if line[:1] == '#':
|
|
|
|
continue
|
2014-10-02 04:38:12 -03:00
|
|
|
line = line.replace('/', ' ').strip()
|
2014-10-01 17:42:30 -03:00
|
|
|
line = line.rstrip('\\').rstrip()
|
2014-10-02 04:38:12 -03:00
|
|
|
words = line.split()
|
|
|
|
if len(words) != 2:
|
|
|
|
continue
|
|
|
|
alias, alias_encoding = words
|
2014-10-01 17:42:30 -03:00
|
|
|
# Lower-case locale
|
|
|
|
locale = alias.lower()
|
|
|
|
# Normalize encoding, if given
|
|
|
|
if '.' in locale:
|
|
|
|
lang, encoding = locale.split('.')[:2]
|
|
|
|
encoding = encoding.replace('-', '')
|
|
|
|
encoding = encoding.replace('_', '')
|
|
|
|
locale = lang + '.' + encoding
|
|
|
|
# Add an encoding to alias
|
|
|
|
alias, _, modifier = alias.partition('@')
|
|
|
|
alias = _locale._replace_encoding(alias, alias_encoding)
|
|
|
|
if modifier and not (modifier == 'euro' and alias_encoding == 'ISO-8859-15'):
|
|
|
|
alias += '@' + modifier
|
|
|
|
data[locale] = alias
|
|
|
|
return data
|
|
|
|
|
2004-12-10 17:58:14 -04:00
|
|
|
def pprint(data):
|
2008-05-16 14:02:34 -03:00
|
|
|
items = sorted(data.items())
|
|
|
|
for k, v in items:
|
2013-12-23 12:56:08 -04:00
|
|
|
print(' %-40s%a,' % ('%a:' % k, v))
|
2004-12-10 17:58:14 -04:00
|
|
|
|
|
|
|
def print_differences(data, olddata):
|
2008-05-16 14:02:34 -03:00
|
|
|
items = sorted(olddata.items())
|
2004-12-10 17:58:14 -04:00
|
|
|
for k, v in items:
|
2008-05-16 14:02:34 -03:00
|
|
|
if k not in data:
|
2013-12-23 12:56:08 -04:00
|
|
|
print('# removed %a' % k)
|
2004-12-10 17:58:14 -04:00
|
|
|
elif olddata[k] != data[k]:
|
2013-12-23 12:56:08 -04:00
|
|
|
print('# updated %a -> %a to %a' % \
|
2007-08-03 14:06:41 -03:00
|
|
|
(k, olddata[k], data[k]))
|
2004-12-10 17:58:14 -04:00
|
|
|
# Additions are not mentioned
|
2005-01-07 12:01:32 -04:00
|
|
|
|
2013-12-26 18:56:53 -04:00
|
|
|
def optimize(data):
|
|
|
|
locale_alias = locale.locale_alias
|
|
|
|
locale.locale_alias = data.copy()
|
|
|
|
for k, v in data.items():
|
|
|
|
del locale.locale_alias[k]
|
|
|
|
if locale.normalize(k) != v:
|
|
|
|
locale.locale_alias[k] = v
|
|
|
|
newdata = locale.locale_alias
|
|
|
|
errors = check(data)
|
|
|
|
locale.locale_alias = locale_alias
|
|
|
|
if errors:
|
|
|
|
sys.exit(1)
|
|
|
|
return newdata
|
|
|
|
|
|
|
|
def check(data):
|
|
|
|
# Check that all alias definitions from the X11 file
|
|
|
|
# are actually mapped to the correct alias locales.
|
|
|
|
errors = 0
|
|
|
|
for k, v in data.items():
|
|
|
|
if locale.normalize(k) != v:
|
|
|
|
print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),
|
|
|
|
file=sys.stderr)
|
|
|
|
errors += 1
|
|
|
|
return errors
|
|
|
|
|
2004-12-10 17:58:14 -04:00
|
|
|
if __name__ == '__main__':
|
2014-10-01 17:42:30 -03:00
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--locale-alias', default=LOCALE_ALIAS,
|
|
|
|
help='location of the X11 alias file '
|
|
|
|
'(default: %a)' % LOCALE_ALIAS)
|
2014-10-02 04:38:12 -03:00
|
|
|
parser.add_argument('--glibc-supported', default=SUPPORTED,
|
|
|
|
help='location of the glibc SUPPORTED locales file '
|
|
|
|
'(default: %a)' % SUPPORTED)
|
2014-10-01 17:42:30 -03:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2004-12-10 17:58:14 -04:00
|
|
|
data = locale.locale_alias.copy()
|
2017-03-08 02:03:13 -04:00
|
|
|
data.update(parse_glibc_supported(args.glibc_supported))
|
2017-03-20 03:49:43 -03:00
|
|
|
data.update(parse(args.locale_alias))
|
2014-10-02 04:21:43 -03:00
|
|
|
while True:
|
|
|
|
# Repeat optimization while the size is decreased.
|
|
|
|
n = len(data)
|
|
|
|
data = optimize(data)
|
|
|
|
if len(data) == n:
|
|
|
|
break
|
2004-12-10 17:58:14 -04:00
|
|
|
print_differences(data, locale.locale_alias)
|
2007-08-03 14:06:41 -03:00
|
|
|
print()
|
|
|
|
print('locale_alias = {')
|
2004-12-10 17:58:14 -04:00
|
|
|
pprint(data)
|
2007-08-03 14:06:41 -03:00
|
|
|
print('}')
|