def config_parse(filename,log=None): xpath_map = {} db_inserts = {} if log: print('\n\n----------\nConfig file parsing.\n----------',file=log) with open(filename) as f: for line in f: line = line.strip().expandtabs() # (Not a comment or empty line) if line=="" or line[0] == "#": continue lineparts = shlex.split(line,comments=True,posix=False) lineparts = (lineparts[0],' '.join(lineparts[1:])) identifier = lineparts[0] if identifier == 'db-insert': key_value = [x.strip() for x in lineparts[1].strip().split(' ', 1)] #key_value[0] is the key, key_value[1] is the value if len(key_value) < 2: if log: print(line + " <----- Is not a proper db insertion! Insufficient tokens.",file=log) continue if log: print('db insertion: ' + str((key_value[0],key_value[1])) + '.',file=log) db_inserts[key_value[0]]=key_value[1] else: path = lineparts[1].strip() if log: print(identifier + ' is mapped to the "xpath" expression: ' + path,file=log) path = xml_cleanup.clean_xpath(path) if log: print('cleaned xpath expression: ' + path,file=log) xpath_map[identifier] = path return (xpath_map,db_inserts)
def get_serial(profile): xpath_expr=xml_cleanup.clean_xpath('//Hardware/Hardware Overview/Serial Number (system)/@val') name_to_val=xpath_lookup(profile,{'serial':xpath_expr}) return name_to_val['serial']