Package up2date_client :: Module haltree
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.haltree

  1   
  2  # HalTree Purpose: 
  3  # 
  4  # HalTree is a way to organize the mess of data you get from hal.  In general, 
  5  # if you want to get all the information about every device in the system, you 
  6  # end up with a list of dicts, where each dict contains the property name/values 
  7  # for a device.  This list isn't very useful as the hal data is actually 
  8  # organized into a tree.  For example, you have the computer as the head, then 
  9  # there may be a scsi card plugged in.  That in turn will have scsi channels 
 10  # and luns, which scsi devices may be connected to.  So this module will help 
 11  # you reorganize your hal data back to the way they were intended. 
 12  # 
 13  # HalTree Usage: 
 14  # 
 15  # The tree gets built one device at a time.  Once you've created a HalTree 
 16  # object, devices get added to the tree with HalTree.add(hw_dev_dict).  The 
 17  # devices can be added in any particular order, and the tree gets properly 
 18  # structured as the devices get added.  But the tree structure isn't likely 
 19  # to be ready until all the devices have been added.  Those devices without a 
 20  # parent get stuck in the no_parent_yet list. 
 21  # 
 22  # When a device gets added, it is no longer a plain dict.  It is stored in a 
 23  # HalDevice.  The original dict can be found in HalDevice.properties. 
 24   
 25  try: # python2 
 26      from types import StringType, IntType 
 27  except ImportError: # python3 
 28      StringType = bytes 
 29      IntType = int 
30 31 32 -class HalDevice:
33 "An object containing its udi, properties and children"
34 - def __init__ (self, properties):
35 self.udi = properties['info.udi'] 36 37 self.properties = properties 38 self.children = [] 39 self.classification = None 40 41 if 'info.parent' in properties: 42 self.parent_udi = properties['info.parent'] 43 else: 44 self.parent_udi = None 45 46 self.parent = None
47
48 - def print_properties(self):
49 print(self.udi, ":") 50 for property, value in self.properties.items(): 51 print(" ", property," ==> ", value)
52
53 54 55 56 -class HalTree:
57 - def __init__ (self):
58 self.head = None 59 self.no_parent_yet = []
60 61
62 - def add(self, hal_device):
63 if hal_device.parent_udi: 64 parent = self.__find_node(hal_device.parent_udi) 65 if parent: 66 parent.children.append(hal_device) 67 hal_device.parent = parent 68 else: #parent isn't in the main tree yet, stick it in waiting 69 self.no_parent_yet.append(hal_device) 70 else: #if it doesn't have a parent, it must be the head 'computer' 71 self.head = hal_device 72 73 #check to see if there are any children waiting for this dev 74 self.__get_lost_children(hal_device)
75 76
77 - def __get_lost_children(self, hal_device):
78 found_list = [] 79 indexes = [] 80 no_parent_yet_copy = self.no_parent_yet[:] 81 for dev in no_parent_yet_copy: 82 if dev.parent_udi == hal_device.udi: 83 dev.parent = hal_device 84 hal_device.children.append(dev) 85 self.no_parent_yet.remove(dev)
86
87 - def __find_node(self, udi):
88 """ 89 This takes a node in the HalDevice tree and returns the HalDevice with 90 the given udi. 91 """ 92 if self.head: 93 node = HalTree.__find_node_worker(self.head, udi) 94 if node: 95 return node 96 97 for node in self.no_parent_yet: 98 found_node = HalTree.__find_node_worker(node, udi) 99 if found_node: 100 return found_node 101 return None
102 103 @staticmethod
104 - def __find_node_worker(node, udi):
105 if node.udi == udi: 106 return node 107 for device in node.children: 108 res = HalTree.__find_node_worker(device, udi) 109 if res: 110 return res 111 return None
112
113 - def print_tree(self):
114 self.__print_dev_tree(self.head, "")
115
116 - def __print_dev_tree(self, node, indent):
117 print(indent, node.udi) 118 print(indent, "CLASS:", node.classification) 119 for name, property in node.properties.items(): 120 if (type(property) == StringType): 121 if property.isdigit(): 122 print(indent + " ", "%-20s ==> %s" % (name, hex(int(property)))) 123 else: 124 print(indent + " ", "%-20s ==> %s" % (name, property)) 125 elif (type(property) == IntType): 126 print(indent + " ", "%-20s ==> %s" % (name, hex(int(property)))) 127 else: 128 print(indent + " ", "%-20s ==> %s" % (name, property)) 129 print 130 for child in node.children: 131 self.__print_dev_tree(child, indent + " ")
132