refactored file mediator to play nice with saas

This commit is contained in:
Rudi Grinberg 2012-11-19 20:13:00 -05:00
parent 8696571b02
commit ebca3fa9f5
3 changed files with 20 additions and 23 deletions

View File

@ -13,9 +13,8 @@ from media.monitor.log import Loggable
from media.monitor.syncdb import AirtimeDB from media.monitor.syncdb import AirtimeDB
from media.monitor.exceptions import DirectoryIsNotListed from media.monitor.exceptions import DirectoryIsNotListed
from media.monitor.bootstrap import Bootstrapper from media.monitor.bootstrap import Bootstrapper
from media.monitor.listeners import FileMediator
from media.saas.thread import apc from media.saas.thread import apc, user
class AirtimeNotifier(Loggable): class AirtimeNotifier(Loggable):
""" """
@ -192,7 +191,7 @@ class AirtimeMessageReceiver(Loggable):
# request that we'd normally get form pyinotify. But right # request that we'd normally get form pyinotify. But right
# now event contractor would take care of this sort of # now event contractor would take care of this sort of
# thing anyway so this might not be necessary after all # thing anyway so this might not be necessary after all
FileMediator.ignore(msg['filepath']) user().file_mediator.ignore(msg['filepath'])
os.unlink(msg['filepath']) os.unlink(msg['filepath'])
# Verify deletion: # Verify deletion:
if not os.path.exists(msg['filepath']): if not os.path.exists(msg['filepath']):

View File

@ -6,38 +6,31 @@ from functools import wraps
import media.monitor.pure as mmp import media.monitor.pure as mmp
from media.monitor.pure import IncludeOnly from media.monitor.pure import IncludeOnly
from media.monitor.events import OrganizeFile, NewFile, MoveFile, DeleteFile, \ from media.monitor.events import OrganizeFile, NewFile, MoveFile, DeleteFile, \
DeleteDir, EventRegistry, MoveDir,\ DeleteDir, MoveDir,\
DeleteDirWatch DeleteDirWatch
from media.monitor.log import Loggable, get_logger from media.monitor.log import Loggable
from media.saas.thread import getsig from media.saas.thread import getsig, user
# Note: Because of the way classes that inherit from pyinotify.ProcessEvent # Note: Because of the way classes that inherit from pyinotify.ProcessEvent
# interact with constructors. you should only instantiate objects from them # interact with constructors. you should only instantiate objects from them
# using keyword arguments. For example: # using keyword arguments. For example:
# OrganizeListener('watch_signal') <= wrong # OrganizeListener('watch_signal') <= wrong
# OrganizeListener(signal='watch_signal') <= right # OrganizeListener(signal='watch_signal') <= right
class FileMediator(object): class FileMediator(Loggable):
""" """ FileMediator is used an intermediate mechanism that filters out
FileMediator is used an intermediate mechanism that filters out certain certain events. """
events. def __init__(self) : self.ignored_set = set([]) # for paths only
""" def is_ignored(self,path) : return path in self.ignored_set
ignored_set = set([]) # for paths only def ignore(self, path) : self.ignored_set.add(path)
logger = get_logger() def unignore(self, path) : self.ignored_set.remove(path)
@staticmethod
def is_ignored(path): return path in FileMediator.ignored_set
@staticmethod
def ignore(path): FileMediator.ignored_set.add(path)
@staticmethod
def unignore(path): FileMediator.ignored_set.remove(path)
def mediate_ignored(fn): def mediate_ignored(fn):
@wraps(fn) @wraps(fn)
def wrapped(self, event, *args,**kwargs): def wrapped(self, event, *args,**kwargs):
event.pathname = unicode(event.pathname, "utf-8") event.pathname = unicode(event.pathname, "utf-8")
if FileMediator.is_ignored(event.pathname): if user().file_mediator.is_ignored(event.pathname):
FileMediator.logger.info("Ignoring: '%s' (once)" % event.pathname) user().file_mediator.logger.info("Ignoring: '%s' (once)" % event.pathname)
FileMediator.unignore(event.pathname) user().file_mediator.unignore(event.pathname)
else: return fn(self, event, *args, **kwargs) else: return fn(self, event, *args, **kwargs)
return wrapped return wrapped

View File

@ -5,6 +5,7 @@ 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 media.monitor.owners import Owner
from media.monitor.listeners import FileMediator
from api_clients.api_client import AirtimeApiClient from api_clients.api_client import AirtimeApiClient
# poor man's phantom types... # poor man's phantom types...
@ -51,3 +52,7 @@ class AirtimeInstance(object):
@LazyProperty @LazyProperty
def owner(self): def owner(self):
return Owner() return Owner()
@LazyProperty
def file_mediator(self):
return FileMediator()