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 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<owner>\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:

View File

@ -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))

View File

@ -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

View File

@ -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()