def get_summary_yaml(self, params): """Return the node summary YAML formatted.""" node = self.get_object(params) # Produce a "clean" composite details document. details_template = dict.fromkeys(script_output_nsmap.values()) for script_result in ( ScriptResult.objects.filter( script_name__in=script_output_nsmap.keys(), status=SCRIPT_STATUS.PASSED, script_set__node=node, ) .only( "status", "script_name", "updated", "stdout", "script__id", "script_set__node", ) .order_by("script_name", "-updated") .distinct("script_name") ): namespace = script_output_nsmap[script_result.name] details_template[namespace] = script_result.stdout probed_details = merge_details_cleanly(details_template) # We check here if there's something to show instead of after # the call to get_single_probed_details() because here the # details will be guaranteed well-formed. if len(probed_details.xpath("/*/*")) == 0: return "" else: return XMLToYAML( etree.tostring(probed_details, encoding=str, pretty_print=True) ).convert()
def test_xml_to_yaml_converts_xml(self): # This test is similar to the test above but this one # checks that tags with colons works as expected. xml = """ <list xmlns:lldp="lldp" xmlns:lshw="lshw"> <lldp:lldp label="LLDP neighbors"/> <lshw:list>Some Content</lshw:list> </list> """ expected_result = dedent("""\ - list: - lldp:lldp: label: LLDP neighbors - lshw:list: Some Content """) yml = XMLToYAML(xml) self.assertEqual(yml.convert(), expected_result)
def dehydrate_summary_output(self, obj, data): """Dehydrate the machine summary output.""" # Produce a "clean" composite details document. probed_details = merge_details_cleanly(get_single_probed_details(obj)) # We check here if there's something to show instead of after # the call to get_single_probed_details() because here the # details will be guaranteed well-formed. if len(probed_details.xpath('/*/*')) == 0: data['summary_xml'] = None data['summary_yaml'] = None else: data['summary_xml'] = etree.tostring(probed_details, encoding=str, pretty_print=True) data['summary_yaml'] = XMLToYAML( etree.tostring(probed_details, encoding=str, pretty_print=True)).convert() return data
def get_summary_yaml(self, params): """Return the node summary YAML formatted.""" node = self.get_object(params) # Produce a "clean" composite details document. details_template = dict.fromkeys(script_output_nsmap.values()) for hw_type in self._script_results.get(node.id, {}).values(): for script_result in hw_type: if (script_result.name in script_output_nsmap and script_result.status == SCRIPT_STATUS.PASSED): namespace = script_output_nsmap[script_result.name] details_template[namespace] = script_result.stdout probed_details = merge_details_cleanly(details_template) # We check here if there's something to show instead of after # the call to get_single_probed_details() because here the # details will be guaranteed well-formed. if len(probed_details.xpath('/*/*')) == 0: return '' else: return XMLToYAML( etree.tostring(probed_details, encoding=str, pretty_print=True)).convert()