summaryrefslogtreecommitdiff
path: root/cliapp/hook.py
diff options
context:
space:
mode:
authorEdward Cragg <edward.cragg@codethink.co.uk>2016-03-27 11:56:15 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2016-03-31 11:43:55 +0100
commite36043737201a645897d366ba0eb44e91748d16e (patch)
tree0b7fb836cf84b8ef8c15fe91907c37ab71aec947 /cliapp/hook.py
parenta7f12476d4e7b2025a60be58027b67b9e551f31b (diff)
downloadmorph-e36043737201a645897d366ba0eb44e91748d16e.tar.gz
Add local cliapp module
Add a copy of the specific version of the cliapp module required by morph, a known version of which can't otherwise be guaranteed to be available on general Linux systems, or via PyPi. Including the module directly allows for continued development in line with morph, until a time when it may become desirable to move away from dependence on this module. With a local copy of cliapp added to morph, it has been shown that morph is capable of building Baserock images without depending on an existing Baserock system. Change-Id: Iab1f7037c7afff054a2404c0552d9b5e4d2d141f
Diffstat (limited to 'cliapp/hook.py')
-rw-r--r--cliapp/hook.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/cliapp/hook.py b/cliapp/hook.py
new file mode 100644
index 00000000..d453f2e6
--- /dev/null
+++ b/cliapp/hook.py
@@ -0,0 +1,76 @@
+# Copyright (C) 2009-2012 Lars Wirzenius
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+'''Hooks with callbacks.
+
+In order to de-couple parts of the application, especially when plugins
+are used, hooks can be used. A hook is a location in the application
+code where plugins may want to do something. Each hook has a name and
+a list of callbacks. The application defines the name and the location
+where the hook will be invoked, and the plugins (or other parts of the
+application) will register callbacks.
+
+'''
+
+
+class Hook(object):
+
+ '''A hook.'''
+
+ def __init__(self):
+ self.callbacks = []
+
+ def add_callback(self, callback):
+ '''Add a callback to this hook.
+
+ Return an identifier that can be used to remove this callback.
+
+ '''
+
+ if callback not in self.callbacks:
+ self.callbacks.append(callback)
+ return callback
+
+ def call_callbacks(self, *args, **kwargs):
+ '''Call all callbacks with the given arguments.'''
+ for callback in self.callbacks:
+ callback(*args, **kwargs)
+
+ def remove_callback(self, callback_id):
+ '''Remove a specific callback.'''
+ if callback_id in self.callbacks:
+ self.callbacks.remove(callback_id)
+
+
+class FilterHook(Hook):
+
+ '''A hook which filters data through callbacks.
+
+ Every hook of this type accepts a piece of data as its first argument
+ Each callback gets the return value of the previous one as its
+ argument. The caller gets the value of the final callback.
+
+ Other arguments (with or without keywords) are passed as-is to
+ each callback.
+
+ '''
+
+ def call_callbacks(self, data, *args, **kwargs):
+ for callback in self.callbacks:
+ data = callback(data, *args, **kwargs)
+ return data
+