Package common :: Module ged
[hide private]
[frames] | no frames]

Source Code for Module common.ged

 1  # -*- coding: utf-8 -*- 
 2   
 3  ## This file is part of Gajim. 
 4  ## 
 5  ## Gajim is free software; you can redistribute it and/or modify 
 6  ## it under the terms of the GNU General Public License as published 
 7  ## by the Free Software Foundation; version 3 only. 
 8  ## 
 9  ## Gajim is distributed in the hope that it will be useful, 
10  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12  ## GNU General Public License for more details. 
13  ## 
14  ## You should have received a copy of the GNU General Public License 
15  ## along with Gajim.  If not, see <http://www.gnu.org/licenses/>. 
16  ## 
17   
18  ''' 
19  Global Events Dispatcher module. 
20   
21  :author: Mateusz Biliński <mateusz@bilinski.it> 
22  :since: 8th August 2008 
23  :copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it> 
24  :license: GPL 
25  ''' 
26   
27  import logging 
28  log = logging.getLogger('gajim.common.ged') 
29   
30  PRECORE = 30 
31  CORE = 40 
32  POSTCORE = 50 
33  GUI1 = 60 
34  GUI2 = 70 
35  POSTGUI = 80 
36   
37 -class GlobalEventsDispatcher(object):
38
39 - def __init__(self):
40 self.handlers = {}
41
42 - def register_event_handler(self, event_name, priority, handler):
43 if event_name in self.handlers: 44 handlers_list = self.handlers[event_name] 45 i = 0 46 for i, h in enumerate(handlers_list): 47 if priority < h[0]: 48 break 49 50 handlers_list.insert(i, (priority, handler)) 51 else: 52 self.handlers[event_name] = [(priority, handler)]
53
54 - def remove_event_handler(self, event_name, priority, handler):
55 if event_name in self.handlers: 56 try: 57 self.handlers[event_name].remove((priority, handler)) 58 except ValueError, error: 59 log.warn('''Function (%s) with priority "%s" never registered 60 as handler of event "%s". Couldn\'t remove. Error: %s''' 61 %(handler, priority, event_name, error))
62
63 - def raise_event(self, event_name, *args, **kwargs):
64 log.debug('%s\nArgs: %s'%(event_name, str(args))) 65 if event_name in self.handlers: 66 for priority, handler in self.handlers[event_name]: 67 if handler(*args, **kwargs): 68 return
69