cc-4105: reformatted events.py

This commit is contained in:
Rudi Grinberg 2012-08-09 16:05:12 -04:00
parent c748b2ebd6
commit 57a8a6a7f7
2 changed files with 49 additions and 31 deletions

View File

@ -35,12 +35,11 @@ class Bootstrapper(Loggable):
added = modded = deleted = 0 added = modded = deleted = 0
for f in mmp.walk_supported(directory, clean_empties=False): for f in mmp.walk_supported(directory, clean_empties=False):
songs.add(f) songs.add(f)
# We decide whether to update a file's metadata by checking # We decide whether to update a file's metadata by checking its
# its system modification date. If it's above the value # system modification date. If it's above the value self.last_ran
# self.last_ran which is passed to us that means media monitor # which is passed to us that means media monitor wasn't aware when
# wasn't aware when this changes occured in the filesystem # this changes occured in the filesystem hence it will send the
# hence it will send the correct events to sync the database # correct events to sync the database with the filesystem
# with the filesystem
if os.path.getmtime(f) > last_ran: if os.path.getmtime(f) > last_ran:
modded += 1 modded += 1
dispatcher.send(signal=self.watch_signal, sender=self, dispatcher.send(signal=self.watch_signal, sender=self,
@ -62,5 +61,3 @@ class Bootstrapper(Loggable):
self.logger.info( "Flushed watch directory (%s). \ self.logger.info( "Flushed watch directory (%s). \
(added, modified, deleted) = (%d, %d, %d)" (added, modified, deleted) = (%d, %d, %d)"
% (directory, added, modded, deleted) ) % (directory, added, modded, deleted) )

View File

@ -8,33 +8,40 @@ from media.monitor.log import Loggable
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
class PathChannel(object): class PathChannel(object):
"""a dumb struct; python has no record types"""
def __init__(self, signal, path): def __init__(self, signal, path):
self.signal = signal self.signal = signal
self.path = path self.path = path
class EventRegistry(object): class EventRegistry(object):
"""
This class's main use is to keep track all events with a cookie attribute.
This is done mainly because some events must be 'morphed' into other events
because we later detect that they are move events instead of delete events.
"""
registry = {} registry = {}
@staticmethod @staticmethod
def register(evt): def register(evt): EventRegistry.registry[evt.cookie] = evt
EventRegistry.registry[evt.cookie] = evt
@staticmethod @staticmethod
def unregister(evt): def unregister(evt): del EventRegistry.registry[evt.cookie]
del EventRegistry.registry[evt.cookie]
@staticmethod @staticmethod
def registered(evt): def registered(evt): return evt.cookie in EventRegistry.registry
return evt.cookie in EventRegistry.registry
@staticmethod @staticmethod
def matching(evt): def matching(evt):
event = EventRegistry.registry[evt.cookie] event = EventRegistry.registry[evt.cookie]
# Want to disallow accessing the same event twice # Want to disallow accessing the same event twice
EventRegistry.unregister(event) EventRegistry.unregister(event)
return event return event
def __init__(self,*args,**kwargs):
raise Exception("You can instantiate this class. Must only use class \
methods")
# It would be good if we could parameterize this class by the attribute
# that would contain the path to obtain the meta data. But it would be too much
# work
class HasMetaData(object): class HasMetaData(object):
"""
Any class that inherits from this class gains the metadata attribute that
loads metadata from the class's 'path' attribute. This is done lazily so
there is no performance penalty to inheriting from this and subsequent
calls to metadata are cached
"""
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@LazyProperty @LazyProperty
def metadata(self): return Metadata(self.path) def metadata(self): return Metadata(self.path)
@ -83,16 +90,23 @@ class BaseEvent(Loggable):
return self return self
class FakePyinotify(object): class FakePyinotify(object):
"""
sometimes we must create our own pyinotify like objects to instantiate
objects from the classes below whenever we want to turn a single event into
multiple events
"""
def __init__(self, path): def __init__(self, path):
self.pathname = path self.pathname = path
class OrganizeFile(BaseEvent, HasMetaData): class OrganizeFile(BaseEvent, HasMetaData):
def __init__(self, *args, **kwargs): super(OrganizeFile, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(OrganizeFile, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
raise AttributeError("What the hell are you doing? You can't send organize events to airtime!!!") raise AttributeError("You can't send organize events to airtime!!!")
class NewFile(BaseEvent, HasMetaData): class NewFile(BaseEvent, HasMetaData):
def __init__(self, *args, **kwargs): super(NewFile, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(NewFile, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
""" """
packs turns an event into a media monitor request packs turns an event into a media monitor request
@ -103,7 +117,8 @@ class NewFile(BaseEvent, HasMetaData):
return [req_dict] return [req_dict]
class DeleteFile(BaseEvent): class DeleteFile(BaseEvent):
def __init__(self, *args, **kwargs): super(DeleteFile, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(DeleteFile, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
req_dict = {} req_dict = {}
req_dict['mode'] = u'delete' req_dict['mode'] = u'delete'
@ -111,8 +126,11 @@ class DeleteFile(BaseEvent):
return [req_dict] return [req_dict]
class MoveFile(BaseEvent, HasMetaData): class MoveFile(BaseEvent, HasMetaData):
"""Path argument should be the new path of the file that was moved""" """
def __init__(self, *args, **kwargs): super(MoveFile, self).__init__(*args, **kwargs) Path argument should be the new path of the file that was moved
"""
def __init__(self, *args, **kwargs):
super(MoveFile, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
req_dict = {} req_dict = {}
req_dict['mode'] = u'moved' req_dict['mode'] = u'moved'
@ -121,26 +139,28 @@ class MoveFile(BaseEvent, HasMetaData):
return [req_dict] return [req_dict]
def map_events(directory, constructor): def map_events(directory, constructor):
# This hack is actually not necessary: # -unknown-path should not appear in the path here but more testing might
#for f in mmp.walk_supported(directory.replace("-unknown-path",""), # be necessary
#clean_empties=False)
for f in mmp.walk_supported(directory, clean_empties=False): for f in mmp.walk_supported(directory, clean_empties=False):
try: try:
for e in constructor( FakePyinotify(f) ).pack(): yield e for e in constructor( FakePyinotify(f) ).pack(): yield e
except BadSongFile as e: yield e except BadSongFile as e: yield e
class DeleteDir(BaseEvent): class DeleteDir(BaseEvent):
def __init__(self, *args, **kwargs): super(DeleteDir, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(DeleteDir, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
return map_events( self.path, DeleteFile ) return map_events( self.path, DeleteFile )
class MoveDir(BaseEvent): class MoveDir(BaseEvent):
def __init__(self, *args, **kwargs): super(MoveDir, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(MoveDir, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
return map_events( self.path, MoveFile ) return map_events( self.path, MoveFile )
class DeleteDirWatch(BaseEvent): class DeleteDirWatch(BaseEvent):
def __init__(self, *args, **kwargs): super(DeleteDirWatch, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(DeleteDirWatch, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
req_dict = {} req_dict = {}
req_dict['mode'] = u'delete_dir' req_dict['mode'] = u'delete_dir'
@ -148,7 +168,8 @@ class DeleteDirWatch(BaseEvent):
return [req_dict] return [req_dict]
class ModifyFile(BaseEvent, HasMetaData): class ModifyFile(BaseEvent, HasMetaData):
def __init__(self, *args, **kwargs): super(ModifyFile, self).__init__(*args, **kwargs) def __init__(self, *args, **kwargs):
super(ModifyFile, self).__init__(*args, **kwargs)
def pack(self): def pack(self):
req_dict = self.metadata.extract() req_dict = self.metadata.extract()
req_dict['mode'] = u'modify' req_dict['mode'] = u'modify'