def run(self, setup): etree = lxml.etree.ElementTree( self.core.BuildConfiguration(setup.hostname)) try: etree.write( setup.filename, encoding='UTF-8', xml_declaration=True, pretty_print=True) except IOError: err = sys.exc_info()[1] print("Failed to write %s: %s" % (setup.filename, err))
def save_original(self, filename): """ Save the original, downloaded source into the filename provided. :param filename: Filename to save the file to. :returns: True or False :rtype: boolean """ if self.raw_data is None: return False etree.write(filename, self.raw_data, encoding='utf-8') return True
def add_thyme_labels(filename, outfile): brain = [] colon = [] xmltree = etree.parse(filename) root = xmltree.getroot() for child in root: idname = child.get('record_id').text.split('_')[0] id = idname[2:] if int(id) in brain: label = 'brain_cancer' elif int(id) in colon: label = 'colon_cancer' else: print('WARNING: id not found:', id) label = 'none' labelnode = etree.SubElement(child, 'diagnosis') labelnode.text = label etree.write(outfile)
if isinstance(catalog["instruction"], dict): if "ja" in catalog["instruction"]: instruction = lxml.etree.Element("instruction") instruction.attrib[ "{http://www.w3.org/XML/1998/namespace}lang"] = "ja" instruction.text = catalog["instruction"]["ja"] root.append(instruction) del catalog["instruction"]["ja"] for lang, text in catalog["instruction"].iteritems(): instruction = lxml.etree.Element("instruction") instruction.attrib[ "{http://www.w3.org/XML/1998/namespace}lang"] = lang instruction.text = text root.append(instruction) else: instruction = lxml.etree.Element("instruction") instruction.text = catalog["instruction"] root.append(instruction) return lxml.etree.ElementTree(root) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("catalog", type=str, help="catalog file") args = parser.parse_args() etree = run(json.load(open(args.catalog))) etree.write(sys.stdout, pretty_print=True, encoding='UTF-8', xml_declaration=True)
def saveSolution(fileName: str, linksModules: Dict[str, Any], demandsFlows: Dict[str, Any]): """ Save computed solution to XML file compatible with SNDlib platform linksModules must be of form: {'LINK_0_1': {'capacity': 4.0, 'count': 2.0}, ...} demandsFlows must be of form: {'Demand_0_1': [(127.0, ['Link_1', 'Link_2', ...])], ...} """ try: from lxml import etree as et except ImportError: print("[-] Failed to save solution - lxml module is not installed !!!") return root = et.Element("solution") root.attrib['xmlns'] = 'http://sndlib.zib.de/solution' root.attrib['version'] = '1.0' linkConfigs = et.Element('linkConfigurations') for linkName in linksModules: linkModules = linksModules[linkName] linkConfig = et.Element('linkConfiguration') linkConfig.attrib['linkId'] = linkName if linkModules['count'] > 0: instModules = et.Element('installedModule') capacity = et.Element('capacity') count = et.Element('installCount') capacity.text = str(linkModules['capacity']) count.text = str(linkModules['count']) instModules.append(capacity) instModules.append(count) linkConfig.append(instModules) linkConfigs.append(linkConfig) root.append(linkConfigs) demandRoutings = et.Element('demandRoutings') demandRoutings.attrib['state'] = 'NOS' for demandName in demandsFlows: flows = demandsFlows[demandName] demandRouting = et.Element('demandRouting', demandId=demandName) for flow in flows: flowPath = et.Element('flowPath') flowPathValue = et.Element('flowPathValue') flowPathValue.text = str(flow[0]) routingPath = et.Element('routingPath') for linkName in flow[1]: link = et.Element('linkId') link.text = linkName routingPath.append(link) flowPath.append(flowPathValue) flowPath.append(routingPath) demandRouting.append(flowPath) demandRoutings.append(demandRouting) root.append(demandRoutings) et = et.ElementTree(root) et.write(fileName, pretty_print=True, xml_declaration=True, encoding='UTF-8')
def etree_to_file(self, filename=None): if filename: etree.write(filename, encoding='utf-8') else: etree.write(self.filename, encoding='utf-8')
root = lxml.etree.Element("virtual-appliance", uuid=catalog["uuid"]) version = lxml.etree.Element("version") version.text = catalog["version"] root.append(version) if "instruction" in catalog: if isinstance(catalog["instruction"], dict): if "ja" in catalog["instruction"]: instruction = lxml.etree.Element("instruction") instruction.attrib["{http://www.w3.org/XML/1998/namespace}lang"] = "ja" instruction.text = catalog["instruction"]["ja"] root.append(instruction) del catalog["instruction"]["ja"] for lang, text in catalog["instruction"].iteritems(): instruction = lxml.etree.Element("instruction") instruction.attrib["{http://www.w3.org/XML/1998/namespace}lang"] = lang instruction.text = text root.append(instruction) else: instruction = lxml.etree.Element("instruction") instruction.text = catalog["instruction"] root.append(instruction) return lxml.etree.ElementTree(root) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("catalog", type=str, help="catalog file") args = parser.parse_args() etree = run(json.load(open(args.catalog))) etree.write(sys.stdout, pretty_print=True, encoding='UTF-8', xml_declaration=True)