Package ClusterShell :: Module MsgTree
[hide private]
[frames] | no frames]

Source Code for Module ClusterShell.MsgTree

  1  # 
  2  # Copyright CEA/DAM/DIF (2007, 2008, 2009) 
  3  #  Contributor: Stephane THIELL <stephane.thiell@cea.fr> 
  4  # 
  5  # This file is part of the ClusterShell library. 
  6  # 
  7  # This software is governed by the CeCILL-C license under French law and 
  8  # abiding by the rules of distribution of free software.  You can  use, 
  9  # modify and/ or redistribute the software under the terms of the CeCILL-C 
 10  # license as circulated by CEA, CNRS and INRIA at the following URL 
 11  # "http://www.cecill.info". 
 12  # 
 13  # As a counterpart to the access to the source code and  rights to copy, 
 14  # modify and redistribute granted by the license, users are provided only 
 15  # with a limited warranty  and the software's author,  the holder of the 
 16  # economic rights,  and the successive licensors  have only  limited 
 17  # liability. 
 18  # 
 19  # In this respect, the user's attention is drawn to the risks associated 
 20  # with loading,  using,  modifying and/or developing or reproducing the 
 21  # software by the user in light of its specific status of free software, 
 22  # that may mean  that it is complicated to manipulate,  and  that  also 
 23  # therefore means  that it is reserved for developers  and  experienced 
 24  # professionals having in-depth computer knowledge. Users are therefore 
 25  # encouraged to load and test the software's suitability as regards their 
 26  # requirements in conditions enabling the security of their systems and/or 
 27  # data to be ensured and,  more generally, to use and operate it in the 
 28  # same conditions as regards security. 
 29  # 
 30  # The fact that you are presently reading this means that you have had 
 31  # knowledge of the CeCILL-C license and that you accept its terms. 
 32  # 
 33  # $Id: MsgTree.py 126 2009-07-13 21:35:07Z st-cea $ 
 34   
 35  """ 
 36  MsgTree 
 37   
 38  ClusterShell message tree classes. 
 39  """ 
 40   
 41  from sets import Set 
 42   
43 -class MsgTreeElem:
44 """ 45 Helper class used to build a messages tree. Advantages are: 46 (1) low memory consumption especially on a cluster when all nodes 47 return similar messages, 48 (2) gathering of messages is done (almost) automatically. 49 """
50 - def __init__(self, msg=None, parent=None):
51 """ 52 Initialize message tree element. 53 """ 54 # structure 55 self.parent = parent 56 self.children = {} 57 # content 58 self.msg = msg 59 self.sources = None
60
61 - def __iter__(self):
62 """ 63 Iterate over tree key'd elements. 64 """ 65 estack = [ self ] 66 67 while len(estack) > 0: 68 elem = estack.pop() 69 if len(elem.children) > 0: 70 estack += elem.children.values() 71 if elem.sources and len(elem.sources) > 0: 72 yield elem
73
74 - def _add_source(self, source):
75 """ 76 Add source tuple (worker, key) to this element. 77 """ 78 if not self.sources: 79 self.sources = source.copy() 80 else: 81 self.sources.union_update(source)
82
83 - def _remove_source(self, source):
84 """ 85 Remove a source tuple (worker, key) from this element. 86 It's used when moving it to a child. 87 """ 88 if self.sources: 89 self.sources.difference_update(source)
90
91 - def add_msg(self, source, msg):
92 """ 93 A new message line is coming, add it to the tree. 94 source is a tuple identifying the message source 95 """ 96 if self.sources and len(self.sources) == 1: 97 # do it quick when only one source is attached 98 src = self.sources 99 self.sources = None 100 else: 101 # remove source from parent (self) 102 src = Set([ source ]) 103 self._remove_source(src) 104 105 # add msg elem to child 106 elem = self.children.setdefault(msg, self.__class__(msg, self)) 107 # add source to elem 108 elem._add_source(src) 109 return elem
110
111 - def message(self):
112 """ 113 Get the whole message buffer from this tree element. 114 """ 115 msg = "" 116 117 # no msg in root element 118 if self.msg is None: 119 return msg 120 121 # build list of msg (reversed by design) 122 rmsgs = [self.msg] 123 parent = self.parent 124 while parent and parent.msg is not None: 125 rmsgs.append(parent.msg) 126 parent = parent.parent 127 128 # reverse the list 129 rmsgs.reverse() 130 131 # concat buffers 132 return '\n'.join(rmsgs)
133