Chain of callback support using simport
This commit is contained in:
parent
3b65cab8c2
commit
8facfff750
@ -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."""
|
||||
|
1
tox.ini
1
tox.ini
@ -9,5 +9,6 @@ deps =
|
||||
notigen
|
||||
notification_utils
|
||||
python-dateutil
|
||||
simport
|
||||
|
||||
commands = nosetests -d -v --with-coverage --cover-inclusive --cover-package shoebox []
|
||||
|
Loading…
x
Reference in New Issue
Block a user