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 -*- # -*- coding: utf-8 -*-
class BadSongFile(Exception): class BadSongFile(Exception):
def __init__(self, path): self.path = path def __init__(self, path): self.path = path
def __str__(self): return "Can't read %s" % self.path def __str__(self): return "Can't read %s" % self.path
@ -15,3 +14,9 @@ class ConfigAccessViolation(Exception):
class FailedToSetLocale(Exception): class FailedToSetLocale(Exception):
def __str__(self): return "Failed to set locale" 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): class Loggable(object):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
# TODO : replace this boilerplate with LazyProperty
@LazyProperty @LazyProperty
def logger(self): def logger(self):
# TODO : Clean this up # TODO : Clean this up

View File

@ -236,17 +236,27 @@ def encode_to(obj, encoding='utf-8'):
def convert_dict_value_to_utf8(md): def convert_dict_value_to_utf8(md):
return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()]) 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() current_locale = locale.getlocale()
if current_locale[1] is None: if current_locale[1] is None:
default_locale = locale.getdefaultlocale() default_locale = locale.getdefaultlocale()
if default_locale[1] is None: if default_locale[1] is None:
if os.path.exists("/etc/default/locale"): lang = config.get('LANG')
config = ConfigObj('/etc/default/locale') new_locale = lang
lang = config.get('LANG')
new_locale = lang
else:
raise FailedToSetLocale()
else: else:
new_locale = default_locale new_locale = default_locale
locale.setlocale(locale.LC_ALL, new_locale) locale.setlocale(locale.LC_ALL, new_locale)
@ -256,7 +266,6 @@ def configure_locale():
if current_locale_encoding not in ['utf-8', 'utf8']: if current_locale_encoding not in ['utf-8', 'utf8']:
raise FailedToSetLocale() raise FailedToSetLocale()
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -2,6 +2,7 @@
# testing ground for the script # testing ground for the script
import pyinotify import pyinotify
import time import time
import sys
import os import os
from media.monitor.listeners import OrganizeListener, StoreWatchListener from media.monitor.listeners import OrganizeListener, StoreWatchListener
from media.monitor.organizer import Organizer from media.monitor.organizer import Organizer
@ -11,9 +12,22 @@ from media.monitor.handler import ProblemFileHandler
from media.monitor.bootstrap import Bootstrapper from media.monitor.bootstrap import Bootstrapper
from media.monitor.log import get_logger from media.monitor.log import get_logger
from media.monitor.syncdb import SyncDB from media.monitor.syncdb import SyncDB
from media.monitor.exceptions import FailedToObtainLocale, FailedToSetLocale
import media.monitor.pure as mmp
from api_clients import api_client as apc from api_clients import api_client as apc
log = get_logger()
# TODO : we should configure locale before doing anything here log.info("Attempting to set the locale...")
try:
mmp.configure_locale(mmp.get_system_locale())
except FailedToSetLocale as e:
log.info("Failed to set the locale...")
sys.exit(1)
except FailedToObtainLocale as e:
log.info("Failed to obtain the locale form the default path: '/etc/default/locale'")
sys.exit(1)
except Exception as e:
log.info("Failed to set the locale for unknown reason. Logging exception.")
log.info(str(e))
channels = { channels = {
# note that org channel still has a 'watch' path because that is the path # note that org channel still has a 'watch' path because that is the path
@ -24,7 +38,6 @@ channels = {
'badfile' : PathChannel('badfile', '/home/rudi/throwaway/fucking_around/problem_dir'), 'badfile' : PathChannel('badfile', '/home/rudi/throwaway/fucking_around/problem_dir'),
} }
log = get_logger()
apiclient = apc.AirtimeApiClient(log) apiclient = apc.AirtimeApiClient(log)
# We initialize sdb before anything because we must know what our watched # We initialize sdb before anything because we must know what our watched
# directories are. # directories are.