Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python2....
File: locale.py
"""Locale support module.
[0] Fix | Delete
[1] Fix | Delete
The module provides low-level access to the C lib's locale APIs and adds high
[2] Fix | Delete
level number formatting APIs as well as a locale aliasing engine to complement
[3] Fix | Delete
these.
[4] Fix | Delete
[5] Fix | Delete
The aliasing engine includes support for many commonly used locale names and
[6] Fix | Delete
maps them to values suitable for passing to the C lib's setlocale() function. It
[7] Fix | Delete
also includes default encodings for all supported locale names.
[8] Fix | Delete
"""
[9] Fix | Delete
[10] Fix | Delete
import sys
[11] Fix | Delete
import encodings
[12] Fix | Delete
import encodings.aliases
[13] Fix | Delete
import re
[14] Fix | Delete
import operator
[15] Fix | Delete
import functools
[16] Fix | Delete
[17] Fix | Delete
# keep a copy of the builtin str type, because 'str' name is overridden
[18] Fix | Delete
# in globals by a function below
[19] Fix | Delete
_str = str
[20] Fix | Delete
[21] Fix | Delete
try:
[22] Fix | Delete
_unicode = unicode
[23] Fix | Delete
except NameError:
[24] Fix | Delete
# If Python is built without Unicode support, the unicode type
[25] Fix | Delete
# will not exist. Fake one.
[26] Fix | Delete
class _unicode(object):
[27] Fix | Delete
pass
[28] Fix | Delete
[29] Fix | Delete
# Try importing the _locale module.
[30] Fix | Delete
#
[31] Fix | Delete
# If this fails, fall back on a basic 'C' locale emulation.
[32] Fix | Delete
[33] Fix | Delete
# Yuck: LC_MESSAGES is non-standard: can't tell whether it exists before
[34] Fix | Delete
# trying the import. So __all__ is also fiddled at the end of the file.
[35] Fix | Delete
__all__ = ["getlocale", "getdefaultlocale", "getpreferredencoding", "Error",
[36] Fix | Delete
"setlocale", "resetlocale", "localeconv", "strcoll", "strxfrm",
[37] Fix | Delete
"str", "atof", "atoi", "format", "format_string", "currency",
[38] Fix | Delete
"normalize", "LC_CTYPE", "LC_COLLATE", "LC_TIME", "LC_MONETARY",
[39] Fix | Delete
"LC_NUMERIC", "LC_ALL", "CHAR_MAX"]
[40] Fix | Delete
[41] Fix | Delete
try:
[42] Fix | Delete
[43] Fix | Delete
from _locale import *
[44] Fix | Delete
[45] Fix | Delete
except ImportError:
[46] Fix | Delete
[47] Fix | Delete
# Locale emulation
[48] Fix | Delete
[49] Fix | Delete
CHAR_MAX = 127
[50] Fix | Delete
LC_ALL = 6
[51] Fix | Delete
LC_COLLATE = 3
[52] Fix | Delete
LC_CTYPE = 0
[53] Fix | Delete
LC_MESSAGES = 5
[54] Fix | Delete
LC_MONETARY = 4
[55] Fix | Delete
LC_NUMERIC = 1
[56] Fix | Delete
LC_TIME = 2
[57] Fix | Delete
Error = ValueError
[58] Fix | Delete
[59] Fix | Delete
def localeconv():
[60] Fix | Delete
""" localeconv() -> dict.
[61] Fix | Delete
Returns numeric and monetary locale-specific parameters.
[62] Fix | Delete
"""
[63] Fix | Delete
# 'C' locale default values
[64] Fix | Delete
return {'grouping': [127],
[65] Fix | Delete
'currency_symbol': '',
[66] Fix | Delete
'n_sign_posn': 127,
[67] Fix | Delete
'p_cs_precedes': 127,
[68] Fix | Delete
'n_cs_precedes': 127,
[69] Fix | Delete
'mon_grouping': [],
[70] Fix | Delete
'n_sep_by_space': 127,
[71] Fix | Delete
'decimal_point': '.',
[72] Fix | Delete
'negative_sign': '',
[73] Fix | Delete
'positive_sign': '',
[74] Fix | Delete
'p_sep_by_space': 127,
[75] Fix | Delete
'int_curr_symbol': '',
[76] Fix | Delete
'p_sign_posn': 127,
[77] Fix | Delete
'thousands_sep': '',
[78] Fix | Delete
'mon_thousands_sep': '',
[79] Fix | Delete
'frac_digits': 127,
[80] Fix | Delete
'mon_decimal_point': '',
[81] Fix | Delete
'int_frac_digits': 127}
[82] Fix | Delete
[83] Fix | Delete
def setlocale(category, value=None):
[84] Fix | Delete
""" setlocale(integer,string=None) -> string.
[85] Fix | Delete
Activates/queries locale processing.
[86] Fix | Delete
"""
[87] Fix | Delete
if value not in (None, '', 'C'):
[88] Fix | Delete
raise Error, '_locale emulation only supports "C" locale'
[89] Fix | Delete
return 'C'
[90] Fix | Delete
[91] Fix | Delete
def strcoll(a,b):
[92] Fix | Delete
""" strcoll(string,string) -> int.
[93] Fix | Delete
Compares two strings according to the locale.
[94] Fix | Delete
"""
[95] Fix | Delete
return cmp(a,b)
[96] Fix | Delete
[97] Fix | Delete
def strxfrm(s):
[98] Fix | Delete
""" strxfrm(string) -> string.
[99] Fix | Delete
Returns a string that behaves for cmp locale-aware.
[100] Fix | Delete
"""
[101] Fix | Delete
return s
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
_localeconv = localeconv
[105] Fix | Delete
[106] Fix | Delete
# With this dict, you can override some items of localeconv's return value.
[107] Fix | Delete
# This is useful for testing purposes.
[108] Fix | Delete
_override_localeconv = {}
[109] Fix | Delete
[110] Fix | Delete
@functools.wraps(_localeconv)
[111] Fix | Delete
def localeconv():
[112] Fix | Delete
d = _localeconv()
[113] Fix | Delete
if _override_localeconv:
[114] Fix | Delete
d.update(_override_localeconv)
[115] Fix | Delete
return d
[116] Fix | Delete
[117] Fix | Delete
[118] Fix | Delete
### Number formatting APIs
[119] Fix | Delete
[120] Fix | Delete
# Author: Martin von Loewis
[121] Fix | Delete
# improved by Georg Brandl
[122] Fix | Delete
[123] Fix | Delete
# Iterate over grouping intervals
[124] Fix | Delete
def _grouping_intervals(grouping):
[125] Fix | Delete
last_interval = None
[126] Fix | Delete
for interval in grouping:
[127] Fix | Delete
# if grouping is -1, we are done
[128] Fix | Delete
if interval == CHAR_MAX:
[129] Fix | Delete
return
[130] Fix | Delete
# 0: re-use last group ad infinitum
[131] Fix | Delete
if interval == 0:
[132] Fix | Delete
if last_interval is None:
[133] Fix | Delete
raise ValueError("invalid grouping")
[134] Fix | Delete
while True:
[135] Fix | Delete
yield last_interval
[136] Fix | Delete
yield interval
[137] Fix | Delete
last_interval = interval
[138] Fix | Delete
[139] Fix | Delete
#perform the grouping from right to left
[140] Fix | Delete
def _group(s, monetary=False):
[141] Fix | Delete
conv = localeconv()
[142] Fix | Delete
thousands_sep = conv[monetary and 'mon_thousands_sep' or 'thousands_sep']
[143] Fix | Delete
grouping = conv[monetary and 'mon_grouping' or 'grouping']
[144] Fix | Delete
if not grouping:
[145] Fix | Delete
return (s, 0)
[146] Fix | Delete
if s[-1] == ' ':
[147] Fix | Delete
stripped = s.rstrip()
[148] Fix | Delete
right_spaces = s[len(stripped):]
[149] Fix | Delete
s = stripped
[150] Fix | Delete
else:
[151] Fix | Delete
right_spaces = ''
[152] Fix | Delete
left_spaces = ''
[153] Fix | Delete
groups = []
[154] Fix | Delete
for interval in _grouping_intervals(grouping):
[155] Fix | Delete
if not s or s[-1] not in "0123456789":
[156] Fix | Delete
# only non-digit characters remain (sign, spaces)
[157] Fix | Delete
left_spaces = s
[158] Fix | Delete
s = ''
[159] Fix | Delete
break
[160] Fix | Delete
groups.append(s[-interval:])
[161] Fix | Delete
s = s[:-interval]
[162] Fix | Delete
if s:
[163] Fix | Delete
groups.append(s)
[164] Fix | Delete
groups.reverse()
[165] Fix | Delete
return (
[166] Fix | Delete
left_spaces + thousands_sep.join(groups) + right_spaces,
[167] Fix | Delete
len(thousands_sep) * (len(groups) - 1)
[168] Fix | Delete
)
[169] Fix | Delete
[170] Fix | Delete
# Strip a given amount of excess padding from the given string
[171] Fix | Delete
def _strip_padding(s, amount):
[172] Fix | Delete
lpos = 0
[173] Fix | Delete
while amount and s[lpos] == ' ':
[174] Fix | Delete
lpos += 1
[175] Fix | Delete
amount -= 1
[176] Fix | Delete
rpos = len(s) - 1
[177] Fix | Delete
while amount and s[rpos] == ' ':
[178] Fix | Delete
rpos -= 1
[179] Fix | Delete
amount -= 1
[180] Fix | Delete
return s[lpos:rpos+1]
[181] Fix | Delete
[182] Fix | Delete
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
[183] Fix | Delete
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
[184] Fix | Delete
[185] Fix | Delete
def format(percent, value, grouping=False, monetary=False, *additional):
[186] Fix | Delete
"""Returns the locale-aware substitution of a %? specifier
[187] Fix | Delete
(percent).
[188] Fix | Delete
[189] Fix | Delete
additional is for format strings which contain one or more
[190] Fix | Delete
'*' modifiers."""
[191] Fix | Delete
# this is only for one-percent-specifier strings and this should be checked
[192] Fix | Delete
match = _percent_re.match(percent)
[193] Fix | Delete
if not match or len(match.group())!= len(percent):
[194] Fix | Delete
raise ValueError(("format() must be given exactly one %%char "
[195] Fix | Delete
"format specifier, %s not valid") % repr(percent))
[196] Fix | Delete
return _format(percent, value, grouping, monetary, *additional)
[197] Fix | Delete
[198] Fix | Delete
def _format(percent, value, grouping=False, monetary=False, *additional):
[199] Fix | Delete
if additional:
[200] Fix | Delete
formatted = percent % ((value,) + additional)
[201] Fix | Delete
else:
[202] Fix | Delete
formatted = percent % value
[203] Fix | Delete
# floats and decimal ints need special action!
[204] Fix | Delete
if percent[-1] in 'eEfFgG':
[205] Fix | Delete
seps = 0
[206] Fix | Delete
parts = formatted.split('.')
[207] Fix | Delete
if grouping:
[208] Fix | Delete
parts[0], seps = _group(parts[0], monetary=monetary)
[209] Fix | Delete
decimal_point = localeconv()[monetary and 'mon_decimal_point'
[210] Fix | Delete
or 'decimal_point']
[211] Fix | Delete
formatted = decimal_point.join(parts)
[212] Fix | Delete
if seps:
[213] Fix | Delete
formatted = _strip_padding(formatted, seps)
[214] Fix | Delete
elif percent[-1] in 'diu':
[215] Fix | Delete
seps = 0
[216] Fix | Delete
if grouping:
[217] Fix | Delete
formatted, seps = _group(formatted, monetary=monetary)
[218] Fix | Delete
if seps:
[219] Fix | Delete
formatted = _strip_padding(formatted, seps)
[220] Fix | Delete
return formatted
[221] Fix | Delete
[222] Fix | Delete
def format_string(f, val, grouping=False):
[223] Fix | Delete
"""Formats a string in the same way that the % formatting would use,
[224] Fix | Delete
but takes the current locale into account.
[225] Fix | Delete
Grouping is applied if the third parameter is true."""
[226] Fix | Delete
percents = list(_percent_re.finditer(f))
[227] Fix | Delete
new_f = _percent_re.sub('%s', f)
[228] Fix | Delete
[229] Fix | Delete
if operator.isMappingType(val):
[230] Fix | Delete
new_val = []
[231] Fix | Delete
for perc in percents:
[232] Fix | Delete
if perc.group()[-1]=='%':
[233] Fix | Delete
new_val.append('%')
[234] Fix | Delete
else:
[235] Fix | Delete
new_val.append(format(perc.group(), val, grouping))
[236] Fix | Delete
else:
[237] Fix | Delete
if not isinstance(val, tuple):
[238] Fix | Delete
val = (val,)
[239] Fix | Delete
new_val = []
[240] Fix | Delete
i = 0
[241] Fix | Delete
for perc in percents:
[242] Fix | Delete
if perc.group()[-1]=='%':
[243] Fix | Delete
new_val.append('%')
[244] Fix | Delete
else:
[245] Fix | Delete
starcount = perc.group('modifiers').count('*')
[246] Fix | Delete
new_val.append(_format(perc.group(),
[247] Fix | Delete
val[i],
[248] Fix | Delete
grouping,
[249] Fix | Delete
False,
[250] Fix | Delete
*val[i+1:i+1+starcount]))
[251] Fix | Delete
i += (1 + starcount)
[252] Fix | Delete
val = tuple(new_val)
[253] Fix | Delete
[254] Fix | Delete
return new_f % val
[255] Fix | Delete
[256] Fix | Delete
def currency(val, symbol=True, grouping=False, international=False):
[257] Fix | Delete
"""Formats val according to the currency settings
[258] Fix | Delete
in the current locale."""
[259] Fix | Delete
conv = localeconv()
[260] Fix | Delete
[261] Fix | Delete
# check for illegal values
[262] Fix | Delete
digits = conv[international and 'int_frac_digits' or 'frac_digits']
[263] Fix | Delete
if digits == 127:
[264] Fix | Delete
raise ValueError("Currency formatting is not possible using "
[265] Fix | Delete
"the 'C' locale.")
[266] Fix | Delete
[267] Fix | Delete
s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
[268] Fix | Delete
# '<' and '>' are markers if the sign must be inserted between symbol and value
[269] Fix | Delete
s = '<' + s + '>'
[270] Fix | Delete
[271] Fix | Delete
if symbol:
[272] Fix | Delete
smb = conv[international and 'int_curr_symbol' or 'currency_symbol']
[273] Fix | Delete
precedes = conv[val<0 and 'n_cs_precedes' or 'p_cs_precedes']
[274] Fix | Delete
separated = conv[val<0 and 'n_sep_by_space' or 'p_sep_by_space']
[275] Fix | Delete
[276] Fix | Delete
if precedes:
[277] Fix | Delete
s = smb + (separated and ' ' or '') + s
[278] Fix | Delete
else:
[279] Fix | Delete
s = s + (separated and ' ' or '') + smb
[280] Fix | Delete
[281] Fix | Delete
sign_pos = conv[val<0 and 'n_sign_posn' or 'p_sign_posn']
[282] Fix | Delete
sign = conv[val<0 and 'negative_sign' or 'positive_sign']
[283] Fix | Delete
[284] Fix | Delete
if sign_pos == 0:
[285] Fix | Delete
s = '(' + s + ')'
[286] Fix | Delete
elif sign_pos == 1:
[287] Fix | Delete
s = sign + s
[288] Fix | Delete
elif sign_pos == 2:
[289] Fix | Delete
s = s + sign
[290] Fix | Delete
elif sign_pos == 3:
[291] Fix | Delete
s = s.replace('<', sign)
[292] Fix | Delete
elif sign_pos == 4:
[293] Fix | Delete
s = s.replace('>', sign)
[294] Fix | Delete
else:
[295] Fix | Delete
# the default if nothing specified;
[296] Fix | Delete
# this should be the most fitting sign position
[297] Fix | Delete
s = sign + s
[298] Fix | Delete
[299] Fix | Delete
return s.replace('<', '').replace('>', '')
[300] Fix | Delete
[301] Fix | Delete
def str(val):
[302] Fix | Delete
"""Convert float to string, taking the locale into account."""
[303] Fix | Delete
return format("%.12g", val)
[304] Fix | Delete
[305] Fix | Delete
def atof(string, func=float):
[306] Fix | Delete
"Parses a string as a float according to the locale settings."
[307] Fix | Delete
#First, get rid of the grouping
[308] Fix | Delete
ts = localeconv()['thousands_sep']
[309] Fix | Delete
if ts:
[310] Fix | Delete
string = string.replace(ts, '')
[311] Fix | Delete
#next, replace the decimal point with a dot
[312] Fix | Delete
dd = localeconv()['decimal_point']
[313] Fix | Delete
if dd:
[314] Fix | Delete
string = string.replace(dd, '.')
[315] Fix | Delete
#finally, parse the string
[316] Fix | Delete
return func(string)
[317] Fix | Delete
[318] Fix | Delete
def atoi(str):
[319] Fix | Delete
"Converts a string to an integer according to the locale settings."
[320] Fix | Delete
return atof(str, int)
[321] Fix | Delete
[322] Fix | Delete
def _test():
[323] Fix | Delete
setlocale(LC_ALL, "")
[324] Fix | Delete
#do grouping
[325] Fix | Delete
s1 = format("%d", 123456789,1)
[326] Fix | Delete
print s1, "is", atoi(s1)
[327] Fix | Delete
#standard formatting
[328] Fix | Delete
s1 = str(3.14)
[329] Fix | Delete
print s1, "is", atof(s1)
[330] Fix | Delete
[331] Fix | Delete
### Locale name aliasing engine
[332] Fix | Delete
[333] Fix | Delete
# Author: Marc-Andre Lemburg, mal@lemburg.com
[334] Fix | Delete
# Various tweaks by Fredrik Lundh <fredrik@pythonware.com>
[335] Fix | Delete
[336] Fix | Delete
# store away the low-level version of setlocale (it's
[337] Fix | Delete
# overridden below)
[338] Fix | Delete
_setlocale = setlocale
[339] Fix | Delete
[340] Fix | Delete
# Avoid relying on the locale-dependent .lower() method
[341] Fix | Delete
# (see issue #1813).
[342] Fix | Delete
_ascii_lower_map = ''.join(
[343] Fix | Delete
chr(x + 32 if x >= ord('A') and x <= ord('Z') else x)
[344] Fix | Delete
for x in range(256)
[345] Fix | Delete
)
[346] Fix | Delete
[347] Fix | Delete
def _replace_encoding(code, encoding):
[348] Fix | Delete
if '.' in code:
[349] Fix | Delete
langname = code[:code.index('.')]
[350] Fix | Delete
else:
[351] Fix | Delete
langname = code
[352] Fix | Delete
# Convert the encoding to a C lib compatible encoding string
[353] Fix | Delete
norm_encoding = encodings.normalize_encoding(encoding)
[354] Fix | Delete
#print('norm encoding: %r' % norm_encoding)
[355] Fix | Delete
norm_encoding = encodings.aliases.aliases.get(norm_encoding,
[356] Fix | Delete
norm_encoding)
[357] Fix | Delete
#print('aliased encoding: %r' % norm_encoding)
[358] Fix | Delete
encoding = locale_encoding_alias.get(norm_encoding,
[359] Fix | Delete
norm_encoding)
[360] Fix | Delete
#print('found encoding %r' % encoding)
[361] Fix | Delete
return langname + '.' + encoding
[362] Fix | Delete
[363] Fix | Delete
def normalize(localename):
[364] Fix | Delete
[365] Fix | Delete
""" Returns a normalized locale code for the given locale
[366] Fix | Delete
name.
[367] Fix | Delete
[368] Fix | Delete
The returned locale code is formatted for use with
[369] Fix | Delete
setlocale().
[370] Fix | Delete
[371] Fix | Delete
If normalization fails, the original name is returned
[372] Fix | Delete
unchanged.
[373] Fix | Delete
[374] Fix | Delete
If the given encoding is not known, the function defaults to
[375] Fix | Delete
the default encoding for the locale code just like setlocale()
[376] Fix | Delete
does.
[377] Fix | Delete
[378] Fix | Delete
"""
[379] Fix | Delete
# Normalize the locale name and extract the encoding and modifier
[380] Fix | Delete
if isinstance(localename, _unicode):
[381] Fix | Delete
localename = localename.encode('ascii')
[382] Fix | Delete
code = localename.translate(_ascii_lower_map)
[383] Fix | Delete
if ':' in code:
[384] Fix | Delete
# ':' is sometimes used as encoding delimiter.
[385] Fix | Delete
code = code.replace(':', '.')
[386] Fix | Delete
if '@' in code:
[387] Fix | Delete
code, modifier = code.split('@', 1)
[388] Fix | Delete
else:
[389] Fix | Delete
modifier = ''
[390] Fix | Delete
if '.' in code:
[391] Fix | Delete
langname, encoding = code.split('.')[:2]
[392] Fix | Delete
else:
[393] Fix | Delete
langname = code
[394] Fix | Delete
encoding = ''
[395] Fix | Delete
[396] Fix | Delete
# First lookup: fullname (possibly with encoding and modifier)
[397] Fix | Delete
lang_enc = langname
[398] Fix | Delete
if encoding:
[399] Fix | Delete
norm_encoding = encoding.replace('-', '')
[400] Fix | Delete
norm_encoding = norm_encoding.replace('_', '')
[401] Fix | Delete
lang_enc += '.' + norm_encoding
[402] Fix | Delete
lookup_name = lang_enc
[403] Fix | Delete
if modifier:
[404] Fix | Delete
lookup_name += '@' + modifier
[405] Fix | Delete
code = locale_alias.get(lookup_name, None)
[406] Fix | Delete
if code is not None:
[407] Fix | Delete
return code
[408] Fix | Delete
#print('first lookup failed')
[409] Fix | Delete
[410] Fix | Delete
if modifier:
[411] Fix | Delete
# Second try: fullname without modifier (possibly with encoding)
[412] Fix | Delete
code = locale_alias.get(lang_enc, None)
[413] Fix | Delete
if code is not None:
[414] Fix | Delete
#print('lookup without modifier succeeded')
[415] Fix | Delete
if '@' not in code:
[416] Fix | Delete
return code + '@' + modifier
[417] Fix | Delete
if code.split('@', 1)[1].translate(_ascii_lower_map) == modifier:
[418] Fix | Delete
return code
[419] Fix | Delete
#print('second lookup failed')
[420] Fix | Delete
[421] Fix | Delete
if encoding:
[422] Fix | Delete
# Third try: langname (without encoding, possibly with modifier)
[423] Fix | Delete
lookup_name = langname
[424] Fix | Delete
if modifier:
[425] Fix | Delete
lookup_name += '@' + modifier
[426] Fix | Delete
code = locale_alias.get(lookup_name, None)
[427] Fix | Delete
if code is not None:
[428] Fix | Delete
#print('lookup without encoding succeeded')
[429] Fix | Delete
if '@' not in code:
[430] Fix | Delete
return _replace_encoding(code, encoding)
[431] Fix | Delete
code, modifier = code.split('@', 1)
[432] Fix | Delete
return _replace_encoding(code, encoding) + '@' + modifier
[433] Fix | Delete
[434] Fix | Delete
if modifier:
[435] Fix | Delete
# Fourth try: langname (without encoding and modifier)
[436] Fix | Delete
code = locale_alias.get(langname, None)
[437] Fix | Delete
if code is not None:
[438] Fix | Delete
#print('lookup without modifier and encoding succeeded')
[439] Fix | Delete
if '@' not in code:
[440] Fix | Delete
return _replace_encoding(code, encoding) + '@' + modifier
[441] Fix | Delete
code, defmod = code.split('@', 1)
[442] Fix | Delete
if defmod.translate(_ascii_lower_map) == modifier:
[443] Fix | Delete
return _replace_encoding(code, encoding) + '@' + defmod
[444] Fix | Delete
[445] Fix | Delete
return localename
[446] Fix | Delete
[447] Fix | Delete
def _parse_localename(localename):
[448] Fix | Delete
[449] Fix | Delete
""" Parses the locale code for localename and returns the
[450] Fix | Delete
result as tuple (language code, encoding).
[451] Fix | Delete
[452] Fix | Delete
The localename is normalized and passed through the locale
[453] Fix | Delete
alias engine. A ValueError is raised in case the locale name
[454] Fix | Delete
cannot be parsed.
[455] Fix | Delete
[456] Fix | Delete
The language code corresponds to RFC 1766. code and encoding
[457] Fix | Delete
can be None in case the values cannot be determined or are
[458] Fix | Delete
unknown to this implementation.
[459] Fix | Delete
[460] Fix | Delete
"""
[461] Fix | Delete
code = normalize(localename)
[462] Fix | Delete
if '@' in code:
[463] Fix | Delete
# Deal with locale modifiers
[464] Fix | Delete
code, modifier = code.split('@', 1)
[465] Fix | Delete
if modifier == 'euro' and '.' not in code:
[466] Fix | Delete
# Assume Latin-9 for @euro locales. This is bogus,
[467] Fix | Delete
# since some systems may use other encodings for these
[468] Fix | Delete
# locales. Also, we ignore other modifiers.
[469] Fix | Delete
return code, 'iso-8859-15'
[470] Fix | Delete
[471] Fix | Delete
if '.' in code:
[472] Fix | Delete
return tuple(code.split('.')[:2])
[473] Fix | Delete
elif code == 'C':
[474] Fix | Delete
return None, None
[475] Fix | Delete
raise ValueError, 'unknown locale: %s' % localename
[476] Fix | Delete
[477] Fix | Delete
def _build_localename(localetuple):
[478] Fix | Delete
[479] Fix | Delete
""" Builds a locale code from the given tuple (language code,
[480] Fix | Delete
encoding).
[481] Fix | Delete
[482] Fix | Delete
No aliasing or normalizing takes place.
[483] Fix | Delete
[484] Fix | Delete
"""
[485] Fix | Delete
language, encoding = localetuple
[486] Fix | Delete
if language is None:
[487] Fix | Delete
language = 'C'
[488] Fix | Delete
if encoding is None:
[489] Fix | Delete
return language
[490] Fix | Delete
else:
[491] Fix | Delete
return language + '.' + encoding
[492] Fix | Delete
[493] Fix | Delete
def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
[494] Fix | Delete
[495] Fix | Delete
""" Tries to determine the default locale settings and returns
[496] Fix | Delete
them as tuple (language code, encoding).
[497] Fix | Delete
[498] Fix | Delete
According to POSIX, a program which has not called
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function