cc-4105: added locale configuration attempt to beginning of mm2

This commit is contained in:
Rudi Grinberg 2012-07-23 11:02:11 -04:00
parent c5bc6a85d2
commit 6fd1dff60a
4 changed files with 39 additions and 13 deletions

View file

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
class BadSongFile(Exception):
def __init__(self, path): self.path = path
def __str__(self): return "Can't read %s" % self.path
@ -15,3 +14,9 @@ class ConfigAccessViolation(Exception):
class FailedToSetLocale(Exception):
def __str__(self): return "Failed to set locale"
class FailedToObtainLocale(Exception):
def __init__(self, path, cause):
self.path = path
self.cause = cause
def __str__(self): return "Failed to obtain locale from '%s'" % self.path

View file

@ -7,7 +7,6 @@ logging.basicConfig(filename='/home/rudi/throwaway/mm2.log', level=logging.DEBUG
class Loggable(object):
__metaclass__ = abc.ABCMeta
# TODO : replace this boilerplate with LazyProperty
@LazyProperty
def logger(self):
# TODO : Clean this up

View file

@ -236,17 +236,27 @@ def encode_to(obj, encoding='utf-8'):
def convert_dict_value_to_utf8(md):
return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()])
def configure_locale():
def get_system_locale(locale_path='/etc/default/locale'):
"""
Returns the configuration object for the system's default locale. Normally
requires root access.
"""
if os.path.exists(locale_path):
try:
config = ConfigObj(locale_path)
return config
except Exception as e:
raise FailedToSetLocale(locale_path,cause=e)
else: raise ValueError("locale path '%s' does not exist. permissions issue?" % locale_path)
def configure_locale(config):
""" sets the locale according to the system's locale."""
current_locale = locale.getlocale()
if current_locale[1] is None:
default_locale = locale.getdefaultlocale()
if default_locale[1] is None:
if os.path.exists("/etc/default/locale"):
config = ConfigObj('/etc/default/locale')
lang = config.get('LANG')
new_locale = lang
else:
raise FailedToSetLocale()
lang = config.get('LANG')
new_locale = lang
else:
new_locale = default_locale
locale.setlocale(locale.LC_ALL, new_locale)
@ -256,7 +266,6 @@ def configure_locale():
if current_locale_encoding not in ['utf-8', 'utf8']:
raise FailedToSetLocale()
if __name__ == '__main__':
import doctest
doctest.testmod()