From cf1e9e76153dc4ff973c834650cbf74d62f869ec Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Fri, 27 Apr 2012 17:25:12 -0400 Subject: [PATCH] configure locale for pypo --- .../airtimemediamonitorbootstrap.py | 7 ++- python_apps/media-monitor/media_monitor.py | 4 +- python_apps/pypo/pypocli.py | 44 +++++++++++++++++-- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py index acd9873ce..97d135ac6 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py @@ -88,10 +88,10 @@ class AirtimeMediaMonitorBootstrap(): for file in files['files']: db_known_files_set.add(file) - new_files = self.mmc.scan_dir_for_new_files(dir) + all_files = self.mmc.scan_dir_for_new_files(dir) all_files_set = set() - for file_path in new_files: + for file_path in all_files: if len(file_path.strip(" \n")) > 0: all_files_set.add(file_path[len(dir):]) @@ -106,8 +106,8 @@ class AirtimeMediaMonitorBootstrap(): else: command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable" % dir + self.logger.debug(command) stdout = self.mmc.exec_command(command) - #self.logger.info(stdout) new_files = stdout.splitlines() @@ -150,7 +150,6 @@ class AirtimeMediaMonitorBootstrap(): if os.path.exists(full_file_path): self.pe.handle_created_file(False, full_file_path, os.path.basename(full_file_path)) - for file_path in modified_files_set: self.logger.debug("modified file") full_file_path = "%s%s" % (dir, file_path) diff --git a/python_apps/media-monitor/media_monitor.py b/python_apps/media-monitor/media_monitor.py index 0573b8e41..d9dcf518c 100644 --- a/python_apps/media-monitor/media_monitor.py +++ b/python_apps/media-monitor/media_monitor.py @@ -25,7 +25,7 @@ from airtimefilemonitor.mediaconfig import AirtimeMediaConfig from airtimefilemonitor.workerprocess import MediaMonitorWorkerProcess from airtimefilemonitor.airtimemediamonitorbootstrap import AirtimeMediaMonitorBootstrap -def get_locale(): +def configure_locale(): current_locale = locale.getlocale() if current_locale[1] is None: @@ -70,7 +70,7 @@ logger.info("\n\n*** Media Monitor bootup ***\n\n") try: - get_locale() + configure_locale() config = AirtimeMediaConfig(logger) api_client = apc.api_client_factory(config.cfg) diff --git a/python_apps/pypo/pypocli.py b/python_apps/pypo/pypocli.py index 06bb71380..0ef7e1927 100644 --- a/python_apps/pypo/pypocli.py +++ b/python_apps/pypo/pypocli.py @@ -9,6 +9,8 @@ import signal import logging import logging.config import logging.handlers +import locale +import os from Queue import Queue from threading import Lock @@ -44,14 +46,48 @@ parser.add_option("-c", "--check", help="Check the cached schedule and exit", de # parse options (options, args) = parser.parse_args() -# configure logging -logging.config.fileConfig("logging.cfg") -logger = logging.getLogger() -LogWriter.override_std_err(logger) #need to wait for Python 2.7 for this.. #logging.captureWarnings(True) +def configure_locale(): + current_locale = locale.getlocale() + + if current_locale[1] is None: + logger.debug("No locale currently set. Attempting to get default locale.") + default_locale = locale.getdefaultlocale() + + if default_locale[1] is None: + logger.debug("No default locale exists. Let's try loading from /etc/default/locale") + if os.path.exists("/etc/default/locale"): + config = ConfigObj('/etc/default/locale') + lang = config.get('LANG') + new_locale = lang + else: + logger.error("/etc/default/locale could not be found! Please run 'sudo update-locale' from command-line.") + sys.exit(1) + else: + new_locale = default_locale + + logger.debug("New locale set to: " + locale.setlocale(locale.LC_ALL, new_locale)) + + + current_locale_encoding = locale.getlocale()[1].lower() + + if current_locale_encoding not in ['utf-8', 'utf8']: + logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % current_locale_encoding) + +# configure logging +try: + logging.config.fileConfig("logging.cfg") + logger = logging.getLogger() + LogWriter.override_std_err(logger) +except Exception, e: + print "Couldn't configure logging" + sys.exit() + +configure_locale() + # loading config file try: config = ConfigObj('/etc/airtime/pypo.cfg')