cc-4105: major refactorings. shit is barely running
This commit is contained in:
parent
add322d515
commit
be00cc6990
14 changed files with 185 additions and 290 deletions
|
@ -3,11 +3,17 @@ from kombu.messaging import Exchange, Queue, Consumer
|
|||
from kombu.connection import BrokerConnection
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import copy
|
||||
|
||||
from media.monitor.exceptions import BadSongFile
|
||||
from media.monitor.metadata import Metadata
|
||||
from media.monitor.log import Loggable
|
||||
from media.monitor.syncdb import SyncDB
|
||||
from media.monitor.exceptions import DirectoryIsNotListed
|
||||
from media.monitor.bootstrap import Bootstrapper
|
||||
|
||||
from api_clients import api_client as apc
|
||||
|
||||
# Do not confuse with media monitor 1's AirtimeNotifier class that more related
|
||||
# to pyinotify's Notifier class. AirtimeNotifier just notifies when events come
|
||||
|
@ -54,7 +60,7 @@ class AirtimeNotifier(Loggable):
|
|||
|
||||
|
||||
class AirtimeMessageReceiver(Loggable):
|
||||
def __init__(self, cfg):
|
||||
def __init__(self, cfg, manager):
|
||||
self.dispatch_table = {
|
||||
'md_update' : self.md_update,
|
||||
'new_watch' : self.new_watch,
|
||||
|
@ -64,6 +70,7 @@ class AirtimeMessageReceiver(Loggable):
|
|||
'file_delete' : self.file_delete,
|
||||
}
|
||||
self.cfg = cfg
|
||||
self.manager = manager
|
||||
def message(self, msg):
|
||||
"""
|
||||
This method is called by an AirtimeNotifier instance that consumes the Rabbit MQ events
|
||||
|
@ -84,13 +91,22 @@ class AirtimeMessageReceiver(Loggable):
|
|||
def _execute_message(self,evt,message):
|
||||
self.dispatch_table[evt](message)
|
||||
|
||||
|
||||
def __request_now_bootstrap(self, directory_id=None, directory=None):
|
||||
sdb = SyncDB(apc.AirtimeApiClient.create_right_config())
|
||||
if directory_id == None: directory_id = sdb.directories[directory]
|
||||
if directory_id in sdb.id_lookup:
|
||||
d = sdb.id_lookup[directory_id]
|
||||
bs = Bootstrapper(sdb, self.manager.watch_signal())
|
||||
bs.flush_watch( directory=d, last_ran=time.time() )
|
||||
else:
|
||||
raise DirectoryIsNotListed(directory_id)
|
||||
|
||||
def supported_messages(self):
|
||||
return self.dispatch_table.keys()
|
||||
|
||||
# TODO : Handler methods - Should either fire the events directly with
|
||||
# pydispatcher or do the necessary changes on the filesystem that will fire
|
||||
# the events
|
||||
def md_update(self, msg):
|
||||
self.logger.info("Updating metadata for: '%s'" % msg['MDATA_KEY_FILEPATH'])
|
||||
md_path = msg['MDATA_KEY_FILEPATH']
|
||||
try:
|
||||
Metadata.write_unsafe(path=md_path, md=msg)
|
||||
|
@ -101,21 +117,65 @@ class AirtimeMessageReceiver(Loggable):
|
|||
self.logger.info("Unknown error when writing metadata to: '%s'" % md_path)
|
||||
|
||||
def new_watch(self, msg):
|
||||
# TODO: "walk" the newly watched directory
|
||||
self.manager.add_watch_directory(msg['directory'])
|
||||
self.logger.info("Creating watch for directory: '%s'" % msg['directory'])
|
||||
if not os.path.exists(msg['directory']):
|
||||
try: os.makedirs(msg['directory'])
|
||||
except Exception as e:
|
||||
self.logger.info("Failed to create watched dir '%s'" % msg['directory'])
|
||||
self.logger.info(str(e))
|
||||
# Is this clever or stupid?
|
||||
else: self.new_watch(msg)
|
||||
else:
|
||||
# TODO : Refactor this; breaks encapsulation.
|
||||
self.manager.watch_listener.flush_events(msg['directory'])
|
||||
self.manager.add_watch_directory(msg['directory'])
|
||||
|
||||
def remove_watch(self, msg):
|
||||
self.logger.info("Removing watch from directory: '%s'" % msg['directory'])
|
||||
self.manager.remove_watch_directory(msg['directory'])
|
||||
|
||||
def rescan_watch(self, msg):
|
||||
# TODO : basically a bootstrap on the directory
|
||||
pass
|
||||
self.logger.info("Trying to rescan watched directory: '%s'" % msg['directory'])
|
||||
try:
|
||||
self.__request_now_bootstrap(msg['id'])
|
||||
except DirectoryIsNotListed as e:
|
||||
self.logger.info("Bad rescan request")
|
||||
self.logger.info( str(e) )
|
||||
except Exception as e:
|
||||
self.logger.info("Bad rescan request. Unknown error.")
|
||||
self.logger.info( str(e) )
|
||||
else:
|
||||
self.logger.info("Successfully re-scanned: '%s'" % msg['directory'])
|
||||
|
||||
def change_storage(self, msg):
|
||||
new_storage_directory = msg['directory']
|
||||
new_storage_directory_id = str(msg['dir_id'])
|
||||
new_import = os.path.join(new_storage_directory, 'imported')
|
||||
new_organize = os.path.join(new_storage_directory, 'organize')
|
||||
for d in [new_import, new_organize]:
|
||||
if os.path.exists(d):
|
||||
self.logger.info("Changing storage to existing dir: '%s'" % d)
|
||||
else:
|
||||
try: os.makedirs(d)
|
||||
except Exception as e:
|
||||
self.logger.info("Could not create dir for storage '%s'" % d)
|
||||
self.logger.info(str(e))
|
||||
|
||||
if all([ os.path.exists(d) for d in [new_import, new_organize] ]):
|
||||
self.manager.set_store_path(new_import)
|
||||
try:
|
||||
self.__request_now_bootstrap( directory=new_import )
|
||||
except Exception as e:
|
||||
self.logger.info("Did not bootstrap off directory '%s'. Probably not in airtime db" % new_import)
|
||||
self.logger.info(str(e))
|
||||
# set_organize_path should automatically flush new_organize
|
||||
self.manager.set_organize_path(new_organize)
|
||||
else:
|
||||
self.logger.info("Change storage procedure failed, could not create directories")
|
||||
|
||||
def file_delete(self, msg):
|
||||
# deletes should be requested only from imported folder but we don't
|
||||
# verify that.
|
||||
# clippy confirmation: "are you really sure?"
|
||||
self.logger.info("Attempting to delete(maybe) '%s'" % msg['filepath'])
|
||||
if msg['delete']:
|
||||
self.logger.info("Clippy confirmation was received, actually deleting file...")
|
||||
if os.path.exists(msg['filepath']):
|
||||
|
@ -126,7 +186,8 @@ class AirtimeMessageReceiver(Loggable):
|
|||
self.logger.info("Failed to delete '%s'" % msg['filepath'])
|
||||
self.logger.info("Error: " % str(e))
|
||||
else:
|
||||
self.logger.info("Attempting to delete file '%s' that does not exist. Full request coming:" % msg['filepath'])
|
||||
self.logger.info("Attempting to delete file '%s' that does not exist. Full request coming:"
|
||||
% msg['filepath'])
|
||||
self.logger.info(msg)
|
||||
else:
|
||||
self.logger.info("No clippy confirmation, ignoring event. Out of curiousity we will print some details.")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue