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()

View File

@ -2,6 +2,7 @@
# testing ground for the script
import pyinotify
import time
import sys
import os
from media.monitor.listeners import OrganizeListener, StoreWatchListener
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.log import get_logger
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
# TODO : we should configure locale before doing anything here
log = get_logger()
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 = {
# 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'),
}
log = get_logger()
apiclient = apc.AirtimeApiClient(log)
# We initialize sdb before anything because we must know what our watched
# directories are.