From f40b076b467a5e15230e4ef9d1b30959dbd83952 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 23 Jul 2012 14:12:59 -0400 Subject: [PATCH] cc-4105: locale fixes, more exceptions, routine to touch index file --- .../media-monitor2/media/monitor/config.py | 9 +++++ .../media/monitor/exceptions.py | 8 +++++ .../media-monitor2/media/monitor/pure.py | 11 ++++++ python_apps/media-monitor2/mm2.py | 36 ++++++++++++++----- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/config.py b/python_apps/media-monitor2/media/monitor/config.py index c57870439..a23f02bec 100644 --- a/python_apps/media-monitor2/media/monitor/config.py +++ b/python_apps/media-monitor2/media/monitor/config.py @@ -27,6 +27,15 @@ class MMConfig(Loggable): """ raise ConfigAccessViolation(key) + def last_ran(): + return 123456 + + def update_last_run(): + pass + + def set_last_run_now(): + pass + def save(self): self.cfg.write() # Remove this after debugging... diff --git a/python_apps/media-monitor2/media/monitor/exceptions.py b/python_apps/media-monitor2/media/monitor/exceptions.py index 499f9c7f9..333663948 100644 --- a/python_apps/media-monitor2/media/monitor/exceptions.py +++ b/python_apps/media-monitor2/media/monitor/exceptions.py @@ -20,3 +20,11 @@ class FailedToObtainLocale(Exception): self.path = path self.cause = cause def __str__(self): return "Failed to obtain locale from '%s'" % self.path + +class CouldNotCreateIndexFile(Exception): + """exception whenever index file cannot be created""" + def __init__(self, path, cause): + self.path = path + self.cause = cause + def __str__(self): return "Failed to create touch file '%s'" % self.path + diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index c0113dde4..9cd6dd4f7 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -229,11 +229,13 @@ def file_md5(path,max_length=100): else: raise ValueError("'%s' must exist to find its md5") def encode_to(obj, encoding='utf-8'): + # TODO : add documentation + unit tests for this function if isinstance(obj, unicode): obj = obj.encode(encoding) return obj def convert_dict_value_to_utf8(md): + # TODO : add documentation + unit tests for this function return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()]) def get_system_locale(locale_path='/etc/default/locale'): @@ -266,6 +268,15 @@ def configure_locale(config): if current_locale_encoding not in ['utf-8', 'utf8']: raise FailedToSetLocale() +def fondle(path,times=None): + # TODO : write unit tests for this + """ + touch a file to change the last modified date. Beware of calling this function on the + same file from multiple threads. + """ + with file(path, 'a'): + os.utime(path, times) + if __name__ == '__main__': import doctest doctest.testmod() diff --git a/python_apps/media-monitor2/mm2.py b/python_apps/media-monitor2/mm2.py index e9858f2fe..ae190b0f7 100644 --- a/python_apps/media-monitor2/mm2.py +++ b/python_apps/media-monitor2/mm2.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -# testing ground for the script import pyinotify import time import sys @@ -11,11 +10,29 @@ from media.monitor.watchersyncer import WatchSyncer from media.monitor.handler import ProblemFileHandler from media.monitor.bootstrap import Bootstrapper from media.monitor.log import get_logger +from media.monitor.config import MMConfig from media.monitor.syncdb import SyncDB -from media.monitor.exceptions import FailedToObtainLocale, FailedToSetLocale +from media.monitor.exceptions import FailedToObtainLocale, FailedToSetLocale, NoConfigFile +from media.monitor.airtime import AirtimeNotifier, AirtimeMessageReceiver import media.monitor.pure as mmp from api_clients import api_client as apc + log = get_logger() +global_config = u'/path/to/global/config' +# MMConfig is a proxy around ConfigObj instances. it does not allow itself +# users of MMConfig instances to modify any config options directly through the +# dictionary. Users of this object muse use the correct methods designated for +# modification +config = None +try: config = MMConfig(global_config) +except NoConfigFile as e: + log.info("Cannot run mediamonitor2 without configuration file.") + log.info("Current config path: '%s'" % global_config) + sys.exit(1) +except Exception as e: + log.info("Unknown error reading configuration file: '%s'" % global_config) + log.info(str(e)) + log.info("Attempting to set the locale...") try: mmp.configure_locale(mmp.get_system_locale()) @@ -67,13 +84,6 @@ bs = Bootstrapper(db=sdb, last_run=int(time.time()), org_channels=[channels['org bs.flush_organize() bs.flush_watch() -# do the bootstrapping before any listening is going one -#conn = Connection('localhost', 'more', 'shit', 'here') -#db = DBDumper(conn).dump_block() -#bs = Bootstrapper(db, [channels['org']], [channels['watch']]) -#bs.flush_organize() -#bs.flush_watch() - wm = pyinotify.WatchManager() # Listeners don't care about which directory they're related to. All they care @@ -87,5 +97,13 @@ wdd1 = wm.add_watch(channels['org'].path, pyinotify.ALL_EVENTS, rec=True, auto_a for pc in channels['watch']: wdd2 = wm.add_watch(pc.path, pyinotify.ALL_EVENTS, rec=True, auto_add=True, proc_fun=o2) +# After finishing the bootstrapping + the listeners we should initialize the +# kombu message consumer to respond to messages from airtime. we prefer to +# leave this component of the program for last because without the *Listener +# objects we cannot properly respond to all events from airtime anyway. + +airtime_receiver = AirtimeMessageReceiver(config) +airtime_notifier = AirtimeNotifier(config, airtime_receiver) + notifier.loop()