From 8696571b0226baccc28f25f5948b34d4c630870c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 19 Nov 2012 19:57:33 -0500 Subject: [PATCH] refactor owner to be saas friendly --- .../media-monitor2/media/monitor/events.py | 7 +- .../media-monitor2/media/monitor/organizer.py | 5 +- .../media-monitor2/media/monitor/owners.py | 72 +++++++++---------- .../media/saas/airtimeinstance.py | 5 ++ 4 files changed, 44 insertions(+), 45 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/events.py b/python_apps/media-monitor2/media/monitor/events.py index ac16b3746..fe7bc47f6 100644 --- a/python_apps/media-monitor2/media/monitor/events.py +++ b/python_apps/media-monitor2/media/monitor/events.py @@ -3,12 +3,11 @@ import os import abc import re import media.monitor.pure as mmp -import media.monitor.owners as owners from media.monitor.pure import LazyProperty from media.monitor.metadata import Metadata from media.monitor.log import Loggable from media.monitor.exceptions import BadSongFile -from media.saas.thread import getsig +from media.saas.thread import getsig, user class PathChannel(object): """ @@ -102,7 +101,7 @@ class BaseEvent(Loggable): self._raw_event = raw_event self.path = os.path.normpath(raw_event.pathname) else: self.path = raw_event - self.owner = owners.get_owner(self.path) + self.owner = user().owner.get_owner(self.path) owner_re = re.search('stor/imported/(?P\d+)/', self.path) if owner_re: self.logger.info("matched path: %s" % self.path) @@ -153,7 +152,7 @@ class BaseEvent(Loggable): ret = self.pack() # Remove owner of this file only after packing. Otherwise packing # will not serialize the owner correctly into the airtime request - owners.remove_file_owner(self.path) + user().owner.remove_file_owner(self.path) return ret except BadSongFile as e: return [e] except Exception as e: diff --git a/python_apps/media-monitor2/media/monitor/organizer.py b/python_apps/media-monitor2/media/monitor/organizer.py index 6d72bee4b..17f46054c 100644 --- a/python_apps/media-monitor2/media/monitor/organizer.py +++ b/python_apps/media-monitor2/media/monitor/organizer.py @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- import media.monitor.pure as mmp -import media.monitor.owners as owners from media.monitor.handler import ReportHandler from media.monitor.log import Loggable from media.monitor.exceptions import BadSongFile from media.monitor.events import OrganizeFile from pydispatch import dispatcher from os.path import dirname -from media.saas.thread import getsig +from media.saas.thread import getsig, user import os.path class Organizer(ReportHandler,Loggable): @@ -75,7 +74,7 @@ class Organizer(ReportHandler,Loggable): # backwards way is bewcause we are unable to encode the owner id # into the file itself so that the StoreWatchListener listener can # detect it from the file - owners.add_file_owner(new_path, owner_id ) + user().owner.add_file_owner(new_path, owner_id ) self.logger.info('Organized: "%s" into "%s"' % (event.path, new_path)) diff --git a/python_apps/media-monitor2/media/monitor/owners.py b/python_apps/media-monitor2/media/monitor/owners.py index 2d6c4be44..48898c9aa 100644 --- a/python_apps/media-monitor2/media/monitor/owners.py +++ b/python_apps/media-monitor2/media/monitor/owners.py @@ -1,44 +1,40 @@ # -*- coding: utf-8 -*- -from media.monitor.log import get_logger -log = get_logger() -# hash: 'filepath' => owner_id -owners = {} +from media.monitor.log import Loggable -def reset_owners(): - """ Wipes out all file => owner associations """ - global owners - owners = {} +class Owner(Loggable): + def __init__(self): + # hash: 'filepath' => owner_id + self.owners = {} + + def get_owner(self,f): + """ Get the owner id of the file 'f' """ + o = self.owners[f] if f in self.owners else -1 + self.logger.info("Received owner for %s. Owner: %s" % (f, o)) + return o -def get_owner(f): - """ Get the owner id of the file 'f' """ - o = owners[f] if f in owners else -1 - log.info("Received owner for %s. Owner: %s" % (f, o)) - return o - - -def add_file_owner(f,owner): - """ Associate file f with owner. If owner is -1 then do we will not record - it because -1 means there is no owner. Returns True if f is being stored - after the function. False otherwise. """ - if owner == -1: return False - if f in owners: - if owner != owners[f]: # check for fishiness - log.info("Warning ownership of file '%s' changed from '%d' to '%d'" - % (f, owners[f], owner)) - else: return True - owners[f] = owner - return True - -def has_owner(f): - """ True if f is owned by somebody. False otherwise. """ - return f in owners - -def remove_file_owner(f): - """ Try and delete any association made with file f. Returns true if - the the association was actually deleted. False otherwise. """ - if f in owners: - del owners[f] + def add_file_owner(self,f,owner): + """ Associate file f with owner. If owner is -1 then do we will not record + it because -1 means there is no owner. Returns True if f is being stored + after the function. False otherwise. """ + if owner == -1: return False + if f in self.owners: + if owner != self.owners[f]: # check for fishiness + self.logger.info("Warning ownership of file '%s' changed from '%d' to '%d'" + % (f, self.owners[f], owner)) + else: return True + self.owners[f] = owner return True - else: return False + + def has_owner(self,f): + """ True if f is owned by somebody. False otherwise. """ + return f in self.owners + + def remove_file_owner(self,f): + """ Try and delete any association made with file f. Returns true if + the the association was actually deleted. False otherwise. """ + if f in self.owners: + del self.owners[f] + return True + else: return False diff --git a/python_apps/media-monitor2/media/saas/airtimeinstance.py b/python_apps/media-monitor2/media/saas/airtimeinstance.py index fb701b432..eed54101a 100644 --- a/python_apps/media-monitor2/media/saas/airtimeinstance.py +++ b/python_apps/media-monitor2/media/saas/airtimeinstance.py @@ -4,6 +4,7 @@ from os.path import join from media.monitor.exceptions import NoConfigFile from media.monitor.pure import LazyProperty from media.monitor.config import MMConfig +from media.monitor.owners import Owner from api_clients.api_client import AirtimeApiClient # poor man's phantom types... @@ -46,3 +47,7 @@ class AirtimeInstance(object): @LazyProperty def mm_config(self): return MMConfig(self.config_paths['media_monitor']) + + @LazyProperty + def owner(self): + return Owner()