refactor owner to be saas friendly

This commit is contained in:
Rudi Grinberg 2012-11-19 19:57:33 -05:00
parent a28c9d9e27
commit 8696571b02
4 changed files with 44 additions and 45 deletions

View File

@ -3,12 +3,11 @@ import os
import abc import abc
import re import re
import media.monitor.pure as mmp import media.monitor.pure as mmp
import media.monitor.owners as owners
from media.monitor.pure import LazyProperty from media.monitor.pure import LazyProperty
from media.monitor.metadata import Metadata from media.monitor.metadata import Metadata
from media.monitor.log import Loggable from media.monitor.log import Loggable
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
from media.saas.thread import getsig from media.saas.thread import getsig, user
class PathChannel(object): class PathChannel(object):
""" """
@ -102,7 +101,7 @@ class BaseEvent(Loggable):
self._raw_event = raw_event self._raw_event = raw_event
self.path = os.path.normpath(raw_event.pathname) self.path = os.path.normpath(raw_event.pathname)
else: self.path = raw_event 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<owner>\d+)/', self.path) owner_re = re.search('stor/imported/(?P<owner>\d+)/', self.path)
if owner_re: if owner_re:
self.logger.info("matched path: %s" % self.path) self.logger.info("matched path: %s" % self.path)
@ -153,7 +152,7 @@ class BaseEvent(Loggable):
ret = self.pack() ret = self.pack()
# Remove owner of this file only after packing. Otherwise packing # Remove owner of this file only after packing. Otherwise packing
# will not serialize the owner correctly into the airtime request # 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 return ret
except BadSongFile as e: return [e] except BadSongFile as e: return [e]
except Exception as e: except Exception as e:

View File

@ -1,13 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import media.monitor.pure as mmp import media.monitor.pure as mmp
import media.monitor.owners as owners
from media.monitor.handler import ReportHandler from media.monitor.handler import ReportHandler
from media.monitor.log import Loggable from media.monitor.log import Loggable
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
from media.monitor.events import OrganizeFile from media.monitor.events import OrganizeFile
from pydispatch import dispatcher from pydispatch import dispatcher
from os.path import dirname from os.path import dirname
from media.saas.thread import getsig from media.saas.thread import getsig, user
import os.path import os.path
class Organizer(ReportHandler,Loggable): class Organizer(ReportHandler,Loggable):
@ -75,7 +74,7 @@ class Organizer(ReportHandler,Loggable):
# backwards way is bewcause we are unable to encode the owner id # backwards way is bewcause we are unable to encode the owner id
# into the file itself so that the StoreWatchListener listener can # into the file itself so that the StoreWatchListener listener can
# detect it from the file # 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"' % self.logger.info('Organized: "%s" into "%s"' %
(event.path, new_path)) (event.path, new_path))

View File

@ -1,44 +1,40 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from media.monitor.log import get_logger from media.monitor.log import Loggable
log = get_logger()
# hash: 'filepath' => owner_id
owners = {}
def reset_owners(): class Owner(Loggable):
""" Wipes out all file => owner associations """ def __init__(self):
global owners # hash: 'filepath' => owner_id
owners = {} 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): def add_file_owner(self,f,owner):
""" Get the owner id of the file 'f' """ """ Associate file f with owner. If owner is -1 then do we will not record
o = owners[f] if f in owners else -1 it because -1 means there is no owner. Returns True if f is being stored
log.info("Received owner for %s. Owner: %s" % (f, o)) after the function. False otherwise. """
return o if owner == -1: return False
if f in self.owners:
if owner != self.owners[f]: # check for fishiness
def add_file_owner(f,owner): self.logger.info("Warning ownership of file '%s' changed from '%d' to '%d'"
""" Associate file f with owner. If owner is -1 then do we will not record % (f, self.owners[f], owner))
it because -1 means there is no owner. Returns True if f is being stored else: return True
after the function. False otherwise. """ self.owners[f] = owner
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]
return True 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

View File

@ -4,6 +4,7 @@ from os.path import join
from media.monitor.exceptions import NoConfigFile from media.monitor.exceptions import NoConfigFile
from media.monitor.pure import LazyProperty from media.monitor.pure import LazyProperty
from media.monitor.config import MMConfig from media.monitor.config import MMConfig
from media.monitor.owners import Owner
from api_clients.api_client import AirtimeApiClient from api_clients.api_client import AirtimeApiClient
# poor man's phantom types... # poor man's phantom types...
@ -46,3 +47,7 @@ class AirtimeInstance(object):
@LazyProperty @LazyProperty
def mm_config(self): def mm_config(self):
return MMConfig(self.config_paths['media_monitor']) return MMConfig(self.config_paths['media_monitor'])
@LazyProperty
def owner(self):
return Owner()