From 8facfff750e4e3070a838ed6ea68d14baec4cb84 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 26 May 2014 19:04:27 +0000 Subject: [PATCH] Chain of callback support using simport --- shoebox/handlers.py | 41 +++++++++++++++++++++++++++++++++++++++-- tox.ini | 1 + 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/shoebox/handlers.py b/shoebox/handlers.py index 1fb63db..a6b4cca 100644 --- a/shoebox/handlers.py +++ b/shoebox/handlers.py @@ -13,11 +13,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import os.path import shutil +import simport + class ArchiveCallback(object): + def __init__(self, **kwargs): + pass + def on_open(self, filename): """Called when an Archive is opened.""" pass @@ -27,9 +33,40 @@ class ArchiveCallback(object): pass +class CallbackList(ArchiveCallback): + def __init__(self, **kwargs): + super(CallbackList, self).__init__(**kwargs) + self.callbacks = [] + self.config = kwargs.get('config', {}) + callback_list_str = self.config.get('callback_list', "") + callback_list = [x.strip() for x in callback_list_str.split(",")] + self.callback_list = [simport.load(c) for c in callback_list] + + # TODO(Sandy): Need some exception handling around these. + # The failure of one shouldn't stop processing. + def on_open(self, filename): + for c in self.callbacks: + c.on_open(filename) + + def on_close(self, filename): + for c in self.callbacks: + c.on_close(filename) + + +class ChangeExtensionCallback(object): + """filename.dat becomes filename.dat.done""" + def __init__(self, **kwargs): + super(ChangeExtensionCallback, self).__init__(**kwargs) + self.new_extension = kwargs.get('new_extension', '.done') + + def on_close(self, filename): + os.rename(filename, "%s.%s" % (filename, self.new_extension)) + + class MoveFileCallback(object): - def __init__(self, destination_folder): - self.destination_folder = destination_folder + def __init__(self, **kwargs): + super(MoveFileCallback, self).__init__(**kwargs) + self.destination_folder = kwargs.get('destination_folder', '.') def on_close(self, filename): """Move this file to destination folder.""" diff --git a/tox.ini b/tox.ini index fa46b76..93a052e 100644 --- a/tox.ini +++ b/tox.ini @@ -9,5 +9,6 @@ deps = notigen notification_utils python-dateutil + simport commands = nosetests -d -v --with-coverage --cover-inclusive --cover-package shoebox []