Lichen

lib/locale.py

543:b7334adb7dec
2017-02-04 Paul Boddie Handle situations where a global accidentally refers to a built-in module.
     1 #!/usr/bin/env python     2      3 """     4 Locale functions.     5      6 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.types import check_int, check_string    23 from native import getlocale as _getlocale, setlocale as _setlocale    24     25 LC_CTYPE = 0    26 LC_NUMERIC = 1    27 LC_TIME = 2    28 LC_COLLATE = 3    29 LC_MONETARY = 4    30 LC_MESSAGES = 5    31 LC_ALL = 6    32     33 def getlocale(category=LC_CTYPE):    34     35     "Return the locale value for 'category'."    36     37     check_int(category)    38     return _getlocale(category)    39     40 def getpreferredencoding():    41     42     "Return the encoding from the environment's locale."    43     44     s = getlocale()    45     46     try:    47         dot = s.index(".")    48         return s[dot+1:]    49     except ValueError:    50         return None    51     52 def initlocale(category=LC_CTYPE):    53     54     "Initialise the locale for 'category' from the environment."    55     56     check_int(category)    57     return _setlocale(category, "")    58     59 def setlocale(category, value):    60     61     "Set the locale for 'category' to 'value'."    62     63     check_int(category)    64     check_string(value)    65     return _setlocale(category, value)    66     67 # vim: tabstop=4 expandtab shiftwidth=4