def _get_hp_xml_data(ip_address, timeout=10): try: url = urlopen( "https://{}/xmldata?item=all".format(ip_address), timeout=timeout, ) try: data = url.read() finally: url.close() except (URLError, httplib.InvalidURL, httplib.BadStatusLine) as e: raise IncompatibleAnswerError('Incompatible answer (%s).' % unicode(e)) else: if not url.info().get('Content-Type', '').startswith('text/xml'): raise IncompatibleAnswerError( 'Incompatible answer (improper content type).', ) data = data.decode('utf-8', 'replace').encode('utf-8') rimp = ET.fromstring(data) if rimp.tag.upper() != 'RIMP': raise IncompatibleAnswerError( 'Incompatible answer (unknown content).', ) return nullify( etree_to_dict( rimp, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz ], ))[1]
def parse_lshw(as_string): parser = ET.ETCompatXMLParser(recover=True) response = ET.fromstring(as_string, parser=parser) if response.tag.upper() != 'NODE': return None, as_string for element in response.findall('.//'): for k in element.attrib.keys(): try: v = element.attrib[k] except UnicodeDecodeError: continue # value has bytes not possible to decode with UTF-8 if (element.tag, k) in _tag_translation_pairs: try: element.tag = v except ValueError: pass continue if (element.tag, k) in _text_translation_pairs: element.text = v continue if k == 'units': value = ET.Element(b'value') value.text = element.text element.text = '' element.append(value) child = ET.Element(k) child.text = v element.append(child) return nullify(etree_to_dict(response, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz, ]))[1]
def _get_hp_xml_data(ip_address, timeout=10): try: url = urlopen( "https://{}/xmldata?item=all".format(ip_address), timeout=timeout, ) try: data = url.read() finally: url.close() except (URLError, httplib.InvalidURL, httplib.BadStatusLine) as e: raise IncompatibleAnswerError('Incompatible answer (%s).' % unicode(e)) else: if not url.info().get('Content-Type', '').startswith('text/xml'): raise IncompatibleAnswerError( 'Incompatible answer (improper content type).', ) data = data.decode('utf-8', 'replace').encode('utf-8') rimp = ET.fromstring(data) if rimp.tag.upper() != 'RIMP': raise IncompatibleAnswerError( 'Incompatible answer (unknown content).', ) return nullify( etree_to_dict( rimp, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz ], ) )[1]
def hp_xmldata(hostname, timeout=10): try: url = urlopen("https://{}/xmldata?item=all".format(hostname), timeout=timeout) try: data = url.read() finally: url.close() except (URLError, httplib.InvalidURL, httplib.BadStatusLine): return else: if not url.info().get('Content-Type', '').startswith('text/xml'): return data = data.decode('utf-8', 'replace').encode('utf-8') rimp = ET.fromstring(data) if rimp.tag.upper() != 'RIMP': return return nullify( etree_to_dict( rimp, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz ], ) )[1]
def hp_xmldata(hostname, timeout=10): try: url = urlopen("https://{}/xmldata?item=all".format(hostname), timeout=timeout) try: data = url.read() finally: url.close() except (URLError, httplib.InvalidURL, httplib.BadStatusLine): return None, "" else: if not url.info().get("Content-Type", "").startswith("text/xml"): return None, "" data = data.decode("utf-8", "replace").encode("utf-8") rimp = ET.fromstring(data) if rimp.tag.upper() != "RIMP": return None, data return ( nullify( etree_to_dict( rimp, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz, ], ) )[1], data, )
def parse_lshw(raw_data): parser = ET.ETCompatXMLParser(recover=True) response = ET.fromstring(raw_data, parser=parser) if response.tag and response.tag.upper() == 'LIST': response = response[0] elif response.tag and response.tag.upper() == 'NODE': pass else: raise Error('Lshw parse error.') for element in response.findall('.//'): for k in element.attrib.keys(): try: v = element.attrib[k] except UnicodeDecodeError: continue # value has bytes not possible to decode with UTF-8 if (element.tag, k) in TAG_TRANSLATION_PAIRS: try: element.tag = v except ValueError: pass continue if (element.tag, k) in TEXT_TRANSLATION_PAIRS: element.text = v continue if k == 'units': value = ET.Element(b'value') value.text = element.text element.text = '' element.append(value) child = ET.Element(k) child.text = v element.append(child) return nullify( etree_to_dict( response, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz, ], ), )[1]
def parse_lshw(as_string): parser = ET.ETCompatXMLParser(recover=True) response = ET.fromstring(as_string, parser=parser) if response.tag.upper() != 'NODE': return None, as_string for element in response.findall('.//'): for k in element.attrib.keys(): try: v = element.attrib[k] except UnicodeDecodeError: continue # value has bytes not possible to decode with UTF-8 if (element.tag, k) in _tag_translation_pairs: try: element.tag = v except ValueError: pass continue if (element.tag, k) in _text_translation_pairs: element.text = v continue if k == 'units': value = ET.Element(b'value') value.text = element.text element.text = '' element.append(value) child = ET.Element(k) child.text = v element.append(child) return nullify( etree_to_dict(response, _converters=[ _nullify, int, float, lck.xml.converters._datetime, lck.xml.converters._datetime_strip_tz, ]))[1]