Package web :: Package pluginmanager :: Package plugins :: Package hardware_inventory
[hide private]
[frames] | no frames]

Source Code for Package web.pluginmanager.plugins.hardware_inventory

  1  ''' 
  2  Inventory Hardware Module 
  3  ''' 
  4   
  5  from ..pluginbase import PluginBase 
  6  from model import HardwareElement, HardwareProfile 
  7  import web 
  8  import searchbyhardware 
  9  import createhardwareprofile 
 10  #import updatehardwareprofile 
 11  #import deletehardwareprofile 
 12   
13 -class Plugin(PluginBase):
14 ''' 15 '''
16 - def enable(self):
17 self.urls = ('action/searchbyhardware', 18 searchbyhardware.SearchByHardwareView, 19 'action/createhardwareprofile/(\w+)/(\w+)$', 20 createhardwareprofile.CreateHardwareProfileView)
21 #'/action/updatehardwareprofile', 22 #updatehardwareprofile.UpdateHardwareProfileView, 23 #'/action/deletehardwareprofile', 24 #deletehardwareprofile.DeleteHardwareProfileView) 25
26 - def disable(self):
27 pass
28
29 - def install(self):
30 pass
31
32 - def uninstall(self):
33 pass
34
35 - def get_options(self):
36 return None
37
38 - def save_options(self, inputs):
39 pass
40
41 - def get_actions_for_ou(self, ou):
42 return [self.actions["createhardwareprofile"]]
43
44 - def get_actions_for_computer(self, computer):
45 # If the computer has a hardware profile 46 if computer.hardware: 47 return [self.actions["updatehardwareprofile"], 48 self.actions["deletehardwareprofile"]] 49 else: 50 return [self.actions["createhardwareprofile"]]
51
52 - def add_hardware_elements(self, hardware_text):
53 ''' 54 Parses a text and adds hardware elements to the 55 db. Returns the hardware elements parsed. 56 If a hardware element is already in db, it won't be added again. 57 A hardware element is only added once. 58 59 If an error ocurred will be ignored. 60 61 Hardware_text is the text returned by a client that executed 62 ogHardwareList. 63 ''' 64 orm = web.ctx.orm 65 elements = [] 66 67 for line in hardware_text.split('\n'): 68 parts = line.split('=') 69 if (len(parts) != 2): 70 print '%s: Hardware element incorrect: %s' % self.human_name, line 71 continue 72 73 if (len(parts[0]) != 3): 74 print '%s: Type with size incorrect size (must be 3): %s' % self.human_name, line 75 continue 76 77 if (len(parts[1]) > 512): 78 print '%s: Too long description: %s' % self.human_name, line 79 continue 80 81 hardware_element = self._search_specific_hardware_element(parts) 82 83 if hardware_element: 84 # If hardware element doesn't exists in the db, it'll be added 85 hardware_element = HardwareElement() 86 hardware_element.type = parts[0] 87 hardware_element.description = parts[1] 88 orm.add(hardware_element) 89 90 elements.append(hardware_element) 91 92 orm.commit() 93 94 return elements
95
96 - def add_hardware_profile(self, computer, hardware_elements, name=''):
97 ''' 98 Adds a profile hardware with the given computer, 99 hardware_elemengs and name. If a same hardware profile exists 100 then the function only relates computer with that profile, and 101 its name will NOT be changed. 102 103 Returns the profile. 104 ''' 105 106 hardware_profile = self._search_specific_hardware_profile(hardware_elements) 107 108 if hardware_profile: 109 # If hardware_profile doesn't exist in the db, it'll be added 110 hardware_profile = HardwareProfile() 111 hardware_profile.name = name 112 hardware_profile.computer = [] 113 hardware_profile.elements = hardware_elements 114 web.ctx.orm.add(hardware_profile) 115 116 hardware_profile.computers.append(computer) 117 118 return hardware_profile
119 - def _search_specific_hardware_element(self, hardware_type, description):
120 ''' 121 Searchs a element with the given type and description. 122 It is returned if it found. None is returned in other case. 123 ''' 124 orm = web.ctx.orm 125 elements = orm.query(HardwareElement).all() 126 127 for element in elements: 128 if (element.type == hardware_type and 129 element.description == description): 130 return element 131 132 return None
133
134 - def _search_specific_hardware_profile(self, elements):
135 ''' 136 Searchs a profile with the given elements. 137 It is returned if it found. None is returned in other case. 138 ''' 139 orm = web.ctx.orm 140 profiles = orm.query(HardwareProfile).all() 141 142 for profile in profiles: 143 if set(profile.elements) == set(elements): 144 return profile 145 146 return None
147 148
149 - def create_hardware_profile_response(self, job):
150 pass
151