Package libxyz :: Package core :: Module hookmanager
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.hookmanager

 1  #-*- coding: utf8 -* 
 2  # 
 3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008 
 4  # 
 5   
 6  # This file is part of XYZCommander. 
 7  # XYZCommander is free software: you can redistribute it and/or modify 
 8  # it under the terms of the GNU Lesser Public License as published by 
 9  # the Free Software Foundation, either version 3 of the License, or 
10  # (at your option) any later version. 
11  # XYZCommander is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
14  # GNU Lesser Public License for more details. 
15  # You should have received a copy of the GNU Lesser Public License 
16  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
17   
18  from libxyz.core.utils import ustring 
19   
20 -class HookManager(object):
21 """ 22 Hooks dispatcher 23 """ 24
25 - def __init__(self):
26 self._data = {}
27 28 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 29
30 - def register(self, event, proc):
31 """ 32 Register proc to be run upon event occured 33 """ 34 35 if event not in self._data: 36 self._data[event] = [] 37 38 if not callable(proc): 39 xyzlog.error(_(u"HookManager: Callable argument expected")) 40 return False 41 42 self._data[event].append(proc) 43 44 return True
45 46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 47
48 - def clear(self, event):
49 """ 50 Clear all data assosiated with an event 51 """ 52 53 self._data[event] = []
54 55 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56
57 - def dispatch(self, event, *args):
58 """ 59 Sequentially run procedures registered with provided event 60 """ 61 62 # No callbacks registered 63 if event not in self._data or not self._data[event]: 64 return False 65 66 for proc in self._data[event]: 67 try: 68 proc(*args) 69 except Exception, e: 70 xyzlog.error( 71 _(u"Error running callback procedure for event %s") % 72 ustring(str(e))) 73 return False 74 75 return True
76