/usr/share/pyshared/LanguageSelector/macros.py is in language-selector-common 0.79.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | '''macros.py: Generate macro values from configuration values and provide
substitution functions.
The following macros are available:
LCODE CCODE PKGCODE LOCALE
'''
import os
import re
def _file_map(file, key, sep = None):
'''Look up key in given file ("key value" lines). Throw an exception if
key was not found.'''
val = None
for l in open(file):
try:
(k, v) = l.split(sep)
except ValueError:
continue
# sort out comments
if k.find('#') >= 0 or v.find('#') >= 0:
continue
if k == key:
val = v.strip()
if val == None:
raise KeyError, 'Key %s not found in %s' % (key, file)
return val
class LangcodeMacros:
LANGCODE_TO_LOCALE = '/usr/share/language-selector/data/langcode2locale'
def __init__(self, langCode):
self.macros = {}
locales = {}
for l in open(self.LANGCODE_TO_LOCALE):
try:
l = l.rstrip()
(k, v) = l.split(':')
except ValueError:
continue
if k.find('#') >= 0 or v.find('#') >= 0:
continue
if not k in locales:
locales[k] = []
locales[k].append(v)
self['LOCALES'] = locales[langCode]
def __getitem__(self, item):
# return empty string as default
return self.macros.get(item, '')
def __setitem__(self, item, value):
self.macros[item] = value
def __contains__(self, item):
return self.macros.__contains__(item)
class LangpackMacros:
def __init__(self, datadir,locale):
'''Initialize values of macros.
This uses information from maps/, config/, some hardcoded aggregate
strings (such as package names), and some external input:
- locale: Standard locale representation (e. g. pt_BR.UTF-8)
Format is: ll[_CC][.UTF-8][@variant]
'''
self.LOCALE_TO_LANGPACK = os.path.join(datadir, 'data', 'locale2langpack')
self.macros = {}
self['LCODE'] = '' # the language code
self['CCODE'] = '' # the country code if present
self['VARIANT'] = '' # the part behind the @ if present
self['LOCALE'] = '' # the locale with the .UTF-8 stripped off
self['PKGCODE'] = '' # the language code used in the language-packs
self['SYSLOCALE'] = '' # a generated full locale identifier, e.g. ca_ES.UTF-8@valencia
# 'C' and 'POSIX' are not supported as locales, fall back to 'en_US'
if locale == 'C' or locale == 'POSIX':
locale = 'en_US'
if '@' in locale:
(locale, self['VARIANT']) = locale.split('@')
if '.' in locale:
locale = locale.split('.')[0]
if '_' in locale:
(self['LCODE'], self['CCODE']) = locale.split('_')
else:
self['LCODE'] = locale
if len(self['VARIANT']) > 0:
self['LOCALE'] = "%s@%s" % (locale, self['VARIANT'])
else:
self['LOCALE'] = locale
# generate a SYSLOCALE from given components
if len(self['LCODE']) > 0:
if len(self['CCODE']) > 0:
self['SYSLOCALE'] = "%s_%s.UTF-8" % (self["LCODE"], self["CCODE"])
else:
self['SYSLOCALE'] = "%s.UTF-8" % self['LCODE']
if len(self['VARIANT']) > 0:
self['SYSLOCALE'] = "%s@%s" % (self['SYSLOCALE'], self['VARIANT'])
# package code
try:
self['PKGCODE'] = _file_map(self.LOCALE_TO_LANGPACK, self['LOCALE'], ':')
except KeyError:
self['PKGCODE'] = self['LCODE']
def __getitem__(self, item):
# return empty string as default
return self.macros.get(item, '')
def __setitem__(self, item, value):
self.macros[item] = value
def __contains__(self, item):
return self.macros.__contains__(item)
def subst_string(self, s):
'''Substitute all macros in given string.'''
re_macro = re.compile('%([A-Z]+)%')
while 1:
m = re_macro.search(s)
if m:
s = s[:m.start(1)-1] + self[m.group(1)] + s[m.end(1)+1:]
else:
break
return s
def subst_file(self, file):
'''Substitute all macros in given file.'''
s = open(file).read()
open(file, 'w').write(self.subst_string(s))
def subst_tree(self, root):
'''Substitute all macros in given directory tree.'''
for path, dirs, files in os.walk(root):
for f in files:
self.subst_file(os.path.join(root, path, f))
if __name__ == '__main__':
datadir = '/usr/share/language-selector'
for locale in ['de', 'de_DE', 'de_DE.UTF-8', 'de_DE.UTF-8@euro', 'fr_BE@latin', 'zh_CN.UTF-8', 'zh_TW.UTF-8', 'zh_HK.UTF-8', 'invalid_Locale']:
l = LangpackMacros(datadir, locale)
print '-------', locale, '---------------'
template = '"%PKGCODE%: %LCODE% %CCODE% %VARIANT% %LOCALE% %SYSLOCALE%"'
print 'string:', l.subst_string(template)
open('testtest', 'w').write(template)
l.subst_file('testtest')
print 'file :', open('testtest').read()
os.unlink('testtest')
|