Package AccessEngine :: Package AEMonitor :: Module MonitorCollection
[hide private]
[frames] | no frames]

Source Code for Module AccessEngine.AEMonitor.MonitorCollection

  1  ''' 
  2  Defines a convenience collection that can be used to notify all L{AEMonitor}s 
  3  of a certain event. 
  4   
  5  @author: Peter Parente 
  6  @organization: IBM Corporation 
  7  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
  8  @license: The BSD License 
  9   
 10  All rights reserved. This program and the accompanying materials are made  
 11  available under the terms of the BSD license which accompanies 
 12  this distribution, and is available at 
 13  U{http://www.opensource.org/licenses/bsd-license.php} 
 14  ''' 
 15  import logging 
 16  import Base 
 17  from AccessEngine.AEAccInterfaces import implements 
 18   
 19  log = logging.getLogger('Monitor') 
 20   
21 -class MonitorCollection(object):
22 ''' 23 Collection of known monitors. Defines methods for informing all elements about 24 about information to be buffered. 25 26 @ivar monitors: list of L{AEMonitor}s that will be notified of events 27 @type monitors: list of L{AEMonitor}s 28 '''
29 - def __init__(self):
30 '''Initializes the monitor list.''' 31 self.monitors = []
32
33 - def __iter__(self):
34 ''' 35 @return: Iterator over the list of L{AEMonitor}s 36 @rtype: iterator 37 ''' 38 return iter(self.monitors)
39
40 - def __len__(self):
41 ''' 42 @return: Number of monitors in the collection 43 @rtype: integer 44 ''' 45 return len(self.monitors)
46
47 - def __contains__(self, val):
48 ''' 49 Checks if the given monitor class name or instance is in the monitor 50 collection. 51 52 @param val: Instance of a monitor or a string representing its class name 53 @type val: string or L{AEMonitor} 54 @return: Is the monitor in the collection? 55 @rtype: boolean 56 ''' 57 if isinstance(val, str): 58 for mon in self.monitors: 59 if mon.getClassName() == val: 60 return True 61 return False 62 else: 63 return val in self.monitors
64
65 - def clear(self):
66 '''Closes all monitors in L{monitors}.''' 67 for m in self.monitors: 68 try: 69 m.close() 70 except Exception, e: 71 pass
72
73 - def removeByClassName(self, name):
74 ''' 75 Removes one L{AEMonitor} instance given its class name from the collection. 76 77 @param name: Class name of the monitor to remove 78 @type name: string 79 ''' 80 for m in self.monitors: 81 if m.getClassName() == name: 82 m.close() 83 # only one possible based on UIE naming convention, so this is safe 84 self.monitors.remove(m) 85 return
86
87 - def remove(self, *mons):
88 ''' 89 Removes one or more L{AEMonitor} instances from the collection. 90 91 @param mons: Monitors to remove 92 @type mons: list of L{AEMonitor} 93 ''' 94 for mon in mons: 95 self.monitors.remove(mon) 96 try: 97 mon.close() 98 except Exception: 99 pass
100
101 - def add(self, kind, monitors):
102 ''' 103 Adds one or more L{AEMonitor}s to the list of monitors to be notified. 104 Filters the list of monitors based on the kind of events in which they are 105 interested. 106 107 @param kind: Kind of event of interest to the monitor 108 @type kind: class 109 @param monitors: L{AEMonitor}s to notify 110 @type monitors: tuple of L{AEMonitor}s 111 ''' 112 for m in monitors: 113 if implements(m, Base.AEMonitor) and (implements(m.getEventType(), kind) 114 or implements(kind, m.getEventType())): 115 # store only the desired kind of monitors 116 self.monitors.append(m) 117 # make sure the monitor isn't already initialized 118 if not m.isInitialized(): 119 # initialize each monitor so it is displayed 120 m.init()
121
122 - def show(self, *args, **kwargs):
123 ''' 124 Informs L{AEMonitor}s added via L{add} of information to be buffered. Calls 125 L{Base.AEMonitor.show} on all monitors. Monitors raising IOError in show 126 are removed from the L{monitors} list and are no longer notified of events. 127 128 @param args: Information to buffer 129 @type args: list 130 @param kwargs: Information to buffer 131 @type kwargs: dictionary 132 ''' 133 i = 0 134 # can't use a for loop here because we're removing elements along the way 135 while i < len(self.monitors): 136 m = self.monitors[i] 137 try: 138 # buffer the given information 139 m.show(*args, **kwargs) 140 except IOError: 141 # remove any monitors that have indicated that want no more events 142 del self.monitors[i] 143 continue 144 except Exception: 145 # log all exceptions other than IO 146 log.exception('Monitor exception') 147 i += 1
148