Back to index

python-cliapp  1.20120630
hookmgr.py
Go to the documentation of this file.
00001 # Copyright (C) 2009-2012  Lars Wirzenius
00002 # 
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License along
00014 # with this program; if not, write to the Free Software Foundation, Inc.,
00015 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 
00017 
00018 from cliapp import Hook, FilterHook
00019 
00020 
00021 class HookManager(object):
00022 
00023     '''Manage the set of hooks the application defines.'''
00024     
00025     def __init__(self):
00026         self.hooks = {}
00027         
00028     def new(self, name, hook):
00029         '''Add a new hook to the manager.
00030         
00031         If a hook with that name already exists, nothing happens.
00032         
00033         '''
00034 
00035         if name not in self.hooks:
00036             self.hooks[name] = hook
00037 
00038     def add_callback(self, name, callback):
00039         '''Add a callback to a named hook.'''
00040         return self.hooks[name].add_callback(callback)
00041         
00042     def remove_callback(self, name, callback_id):
00043         '''Remove a specific callback from a named hook.'''
00044         self.hooks[name].remove_callback(callback_id)
00045         
00046     def call(self, name, *args, **kwargs):
00047         '''Call callbacks for a named hook, using given arguments.'''
00048         return self.hooks[name].call_callbacks(*args, **kwargs)
00049