From 6fd1dff60abd2df393e2b2bbdc818ca85eaf6b76 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 23 Jul 2012 11:02:11 -0400 Subject: [PATCH] cc-4105: added locale configuration attempt to beginning of mm2 --- .../media/monitor/exceptions.py | 7 +++++- .../media-monitor2/media/monitor/log.py | 1 - .../media-monitor2/media/monitor/pure.py | 25 +++++++++++++------ python_apps/media-monitor2/mm2.py | 19 +++++++++++--- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/exceptions.py b/python_apps/media-monitor2/media/monitor/exceptions.py index aaaf8b7d8..499f9c7f9 100644 --- a/python_apps/media-monitor2/media/monitor/exceptions.py +++ b/python_apps/media-monitor2/media/monitor/exceptions.py @@ -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 diff --git a/python_apps/media-monitor2/media/monitor/log.py b/python_apps/media-monitor2/media/monitor/log.py index 3330f809b..104630124 100644 --- a/python_apps/media-monitor2/media/monitor/log.py +++ b/python_apps/media-monitor2/media/monitor/log.py @@ -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 diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index aecf0223b..c0113dde4 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -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() diff --git a/python_apps/media-monitor2/mm2.py b/python_apps/media-monitor2/mm2.py index fc2a020fe..e9858f2fe 100644 --- a/python_apps/media-monitor2/mm2.py +++ b/python_apps/media-monitor2/mm2.py @@ -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.