def _write_root_tag_open(self): repomd_attributes = {'xmlns': REPO_XML_NAME_SPACE, 'xmlns:rpm': RPM_XML_NAME_SPACE} repomd_element = ElementTree.Element('repomd', repomd_attributes) revision_element = ElementTree.SubElement(repomd_element, 'revision') revision_element.text = str(int(time.time())) bogus_element = ElementTree.SubElement(repomd_element, '') repomd_element_string = ElementTree.tostring(repomd_element, 'utf-8') bogus_element_string = ElementTree.tostring(bogus_element, 'utf-8') opening_tag, closing_tag = repomd_element_string.split(bogus_element_string, 1) self.metadata_file_handle.write(opening_tag + '\n') # Setup the corresponding _write_root_tag_close as a closure def _write_root_tag_close_closure(*args): self.metadata_file_handle.write(closing_tag + '\n') self._write_root_tag_close = _write_root_tag_close_closure
def mount_iso(self, dev, image): def attach_iso(dev, disk, vol): if disk.get('device') == 'cdrom': for elm in disk: if elm.tag == 'target': if elm.get('dev') == dev: src_media = ElementTree.Element('source') src_media.set('file', vol.path()) disk.insert(2, src_media) return True storages = self.get_storages() for storage in storages: stg = self.get_storage(storage) if stg.info()[0] != 0: for img in stg.listVolumes(): if image == img: vol = stg.storageVolLookupByName(image) tree = ElementTree.fromstring(self._XMLDesc(0)) for disk in tree.findall('devices/disk'): if attach_iso(dev, disk, vol): break if self.get_status() == 1: xml = ElementTree.tostring(disk) self.instance.attachDevice(xml) xmldom = self._XMLDesc(VIR_DOMAIN_XML_SECURE) if self.get_status() == 5: xmldom = ElementTree.tostring(tree) self._defineXML(xmldom)
def test_postXMLEndpoint(self): testURL = "https://api.eveonline.com/server/ServerStatus.xml.aspx" res_cache = api.postXMLEndpoint(testURL) self.assertIsInstance(res_cache, ET.Element) # Test cached response self.assertEqual(ET.tostring(api.postXMLEndpoint(testURL)), ET.tostring(res_cache))
def test_insert_nested_div(self): content_root = ET.fromstring(content_root_text) html = ET.fromstring(html_text) output = ET.fromstring(output_text) make_html.insert(content_root, html) self.assertEqual(ET.tostring(html).split('\n'), ET.tostring(output).split('\n'))
def open_soap_envelope(text): """ :param text: SOAP message :return: dictionary with two keys "body"/"header" """ try: envelope = ElementTree.fromstring(text) except Exception as exc: raise XmlParseError("%s" % exc) assert envelope.tag == '{%s}Envelope' % soapenv.NAMESPACE assert len(envelope) >= 1 content = {"header": [], "body": None} for part in envelope: if part.tag == '{%s}Body' % soapenv.NAMESPACE: assert len(part) == 1 content["body"] = ElementTree.tostring(part[0], encoding="UTF-8") elif part.tag == "{%s}Header" % soapenv.NAMESPACE: for item in part: _str = ElementTree.tostring(item, encoding="UTF-8") content["header"].append(_str) return content
def f(self, extracted, info, *default): try: l = info.findall(path) if len(l) > 1: msg = 'The xpath "%s" matches more than one element in %s API response:\n%s' msg %= (path, self.name, ET.tostring(info)) log(msg) raise ValueError(msg) v = l[0].get(attr) if attr else l[0] except IndexError: if default: return default[0] msg = 'Unable to find xpath "%s" in %s API response:\n%s' msg %= (path, self.name, ET.tostring(info)) log(msg) raise IndexError(msg) except KeyError: if default: return default[0] msg = 'The element has no "%s" attribute in %s API response:\n%s' msg %= (attr, self.name, ET.tostring(info)) log(msg) raise KeyError(msg) if v: v = clean(v) if not v and not default: msg = 'The xpath "%s" points to an empty value in %s API response:\n%s' msg %= (path, self.name, ET.tostring(info)) log(msg) raise ValueError(msg) return v
def appendToExistingFiltersFile(filterFileName, xml_filtersRoot): print("Updating filters of existing file.") existingRoot = Xml.parse(filterFileName).getroot() print(Xml.tostring(xml_filtersRoot)) existingRoot.set("ToolsVersion", ToolsVersion) found = False for itemGroup in existingRoot: for child in itemGroup: if child.tag.endswith("Filter"): print("Removing all existing filters.") found = True itemGroup.clear() toAppendTo = itemGroup break if found: break if not found: print("No existing filters found.") toAppendTo = Xml.SubElement(existingRoot, "ItemGroup") for itemGroup in xml_filtersRoot: for filterElement in itemGroup: toAppendTo.append(filterElement) with open(filterFileName, "w") as filterFile: xmlContent = """<?xml version="1.0" encoding="utf-8"?>\n""" xmlContent += Xml.tostring(existingRoot, encoding="unicode"); filterFile.write(xmlContent)
def pack(exif, photo, label, config=None): """Pack the necessary information into a single string, which should be used in write_db function. """ package = {} package['label'] = label # Get the photo pos if config is None: pos = None package['urls'] = None else: pos = tb.get_photo_pos(photo, config) package['urls'] = config.get_urls() package['pos'] = pos # Get the focal in 135 camera if config is None or exif is None: focal_in35 = None else: focal_in35 = tb.get_focal_in35(exif, config) package['focal_in35'] = focal_in35 # Convert photo to XML string package['photo'] = ET.tostring(photo, encoding='utf8', method='xml') # Convert exif to xml string if exif is None: package['exif'] = None else: package['exif'] = ET.tostring(exif.exif, encoding='utf8', method='xml') # Pack the dict to string package = yaml.dump(package) return package
def test_fuzz_data_xml(self): """Test _fuzz_data_ with an XML element.""" data = ElementTree.Element("a") sub_ele = ElementTree.Element("b") sub_ele.text = "c" sub_ele.attrib = {"name": "val"} sub_ele2 = ElementTree.Element("ACTION_FIELD:d") sub_ele2.text = "e" data.append(sub_ele) data.append(sub_ele2) strings = ["test"] contents = [] expected_contents = [ '<a><b name="val">test</b><ACTION_FIELD:d>e</ACTION_FIELD:d></a>', '<a><b name="test">c</b><ACTION_FIELD:d>e</ACTION_FIELD:d></a>' ] for i, d in enumerate( fuzz_datagen._fuzz_data(strings, data, "ACTION_FIELD:", "unittest"), 1): name, model, stri, param_path = d self.assertEqual("unitteststr1_model{0}".format(i), name) self.assertEqual("test", stri) if six.PY2: contents.append(ElementTree.tostring(model)) else: contents.append(ElementTree.tostring(model).decode("utf-8")) self.assertEqual(expected_contents, contents)
def creating_xml(file1,file2,path_for_reference_xsd, xsd_path,schema_url,outputfile): value_list,template_id,templatestring,last_value1=test(file1,file2,path_for_reference_xsd, xsd_path) root=Element("template") p=templatestring.split("-",1)[1] tree=ElementTree(root) root.set("xsi:schemaLocation","https://appliedbroadband.com/ipdr/template_block template_block.xsd") root.set("xmlns","https://appliedbroadband.com/ipdr/template_block") root.set("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") templateId=Element("templateId") schemaName=Element("schemaName") typeName=Element("typeName") root.append(templateId) root.append(schemaName) root.append(typeName) templateId.text=str(template_id) schemaName.text=str(schema_url)+str(last_value1) typeName.text=str(templatestring)+":"+str(templatestring.split("-",1)[1]) for i in value_list: field=Element("field") root.append(field) typeId=Element("typeId") fieldId=Element("fieldId") fieldName=Element("fieldName") isEnabled=Element("isEnabled") field.append(typeId) field.append(fieldId) field.append(fieldName) field.append(isEnabled) typeId.text=str(i[0]) fieldId.text=str(i[1]) fieldName.text=str(i[2]) isEnabled.text=str(i[3]) print Etree.tostring(root) tree.write(open(r'%s'%outputfile,"w+"))
def to_xml(elem, encoding=None, pretty=False): ''' Export element to XML Parameters ---------- elem : ElementTree.Element or xml-string The element to export encoding : string, optional The output encoding Returns ------- string ''' if isinstance(elem, six.string_types): elem = ET.fromstring(elem) # In-place editing!! if pretty: xml_indent(elem) if encoding is None: return ET.tostring(elem, encoding='utf-8').decode() return ET.tostring(elem, encoding=encoding)
def main(datafile, threshold): filename = "out{}{}.hrc".format(os.sep, os.path.basename(datafile.name)) if not os.path.isfile(filename): header = datafile.readline() collist = [i for i, toggle in enumerate(header.split(",")) if toggle != "0"] datafile.seek(0) data = pd.read_csv(datafile, usecols=collist).as_matrix() pipeline = Pipeline([("clf", Hierarchical())]) pipeline.set_params(**{}) pipeline.fit(data) clf = pipeline.get_params()["clf"] hierarchy = clf.hierarchy_ with open(filename, "wb") as fh: fh.write(ET.tostring(hierarchy.to_xml())) else: with open(filename, "rb") as fh: hierarchy = Cluster.from_xml(ET.parse(fh).getroot()) print(ET.tostring(hierarchy.to_xml()).decode("utf-8")) if threshold != None: clusters = hierarchy.cut(threshold) print("\n".join(c.to_str(i) for i, c in enumerate(clusters))) dump_graph(clusters)
def __bodyOfferModify(self, tsn, pc_body_id, description, duration, size, title, subtitle, url): data = { 'bodyId' : 'tsn:' + tsn, 'description' : description, 'duration' : duration, 'encodingType' : 'mpeg2ProgramStream', 'partnerId' : 'tivo:pt.3187', 'pcBodyId' : pc_body_id, 'publishDate' : time.strftime('%Y-%m-%d %H:%M%S', time.gmtime()), 'size' : size, 'source' : 'file:/C%3A%2FDocuments%20and%20Settings%2FStephanie%2FDesktop%2FVideo', 'state' : 'complete', 'subtitle' : subtitle, 'title' : title, 'url' : url, } r = urllib2.Request( 'https://mind.tivo.com:8181/mind/mind7?type=bodyOfferModify&bodyId=tsn:' + tsn, dictcode(data), {'Content-Type' : 'x-tivo/dict-binary'} ) result = self.__opener.open(r) xml = ElementTree.parse(result).find('.') self.__log('__bodyOfferModify\n%s\n\n%sg' % (data, ElementTree.tostring(xml))) if xml.findtext('state') != 'complete': raise Exception(ElementTree.tostring(xml)) offer_id = xml.findtext('offerId') content_id = offer_id.replace('of','ct') return offer_id, content_id
def pprint(self): if self.xml is not None: mxml = minidom.parseString(ElementTree.tostring(self.xml)) mxml = minidom.parseString( ElementTree.tostring(self.xml.find('./cputune'))) print mxml.toprettyxml() self.res.pprint()
def feedback(items): feedback = ET.Element("items") def processItem(item): itemToAdd = ET.SubElement(feedback, "item") data = item.get() for (k, v) in data["attrib"].iteritems(): itemToAdd.set(k, v) for (k, v) in data["content"].iteritems(): if k != "fileIcon" and k != "fileType": child = ET.SubElement(itemToAdd, k) child.text = v if k == "icon": if "fileIcon" in data["content"].keys(): if data["content"]["fileIcon"] == True: child.set("type", "fileicon") if "fileType" in data["content"].keys(): if data["content"]["fileType"] == True: child.set("type", "filetype") if isinstance(items, list): for anItem in items: processItem(anItem) else: processItem(items) print ET.tostring(feedback, encoding="utf-8")
def mount_iso(self, dev, image): def attach_iso(dev, disk, vol): if disk.get("device") == "cdrom": for elm in disk: if elm.tag == "target": if elm.get("dev") == dev: existing_media = disk.find("source") src_media = existing_media if src_media is None: src_media = ElementTree.Element("source") src_media.set("file", vol.path()) if existing_media is None: disk.insert(2, src_media) return True storages = self.get_storages() for storage in storages: stg = self.get_storage(storage) if stg.info()[0] != 0: for img in stg.listVolumes(): if image == img: vol = stg.storageVolLookupByName(image) tree = ElementTree.fromstring(self._XMLDesc(0)) for disk in tree.findall("devices/disk"): if attach_iso(dev, disk, vol): break if self.get_status() == 1: xml = ElementTree.tostring(disk) self.instance.updateDeviceFlags(xml, VIR_DOMAIN_DEVICE_MODIFY_FORCE) xmldom = self._XMLDesc(VIR_DOMAIN_XML_SECURE) if self.get_status() == 5: xmldom = ElementTree.tostring(tree) self._defineXML(xmldom)
def test_icon(): # not attribute root = etree.Element('root') e = Icon('icon_text') e.build(root) eq_('<root><icon>icon_text</icon></root>', etree.tostring(root)) # fileicon attribute root = etree.Element('root') e = Icon('icon_text', type='fileicon') e.build(root) eq_('<root><icon type="fileicon">icon_text</icon></root>', etree.tostring(root)) # filetype attribute root = etree.Element('root') e = Icon('icon_text', type='filetype') e.build(root) eq_('<root><icon type="filetype">icon_text</icon></root>', etree.tostring(root)) # error with assert_raises(ValueError) as ve: e = Icon('root') e.type = 'dummy' eq_("type must be ['fileicon', 'filetype', None]", str(ve.exception))
def test_generate_etree(self, xml_params): """Test xml generation of parameter lists""" params = [{'a': a, 'b': b} for a in range(10) for b in range(10)] etree = generate_etree(params, 'root') assert len(etree.getchildren()) == len(xml_params.getchildren()) assert ET.tostring(etree) == ET.tostring(xml_params)
def unassign_volume(self, device_to_unassign): not_active = self.get_status() != 1 # if not_active: xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) tree = ElementTree.fromstring(xml) file_to_unassign = None # for live detach only devices = tree.findall("devices")[-1] for device in devices: print device if device.tag == "disk": target = device.findall("target")[-1] if target.get("dev") == device_to_unassign: if not_active: devices.remove(device) else: source = device.findall("source")[-1] file_to_unassign = source.get("file") if not_active: new_xml = ElementTree.tostring(tree) self._defineXML(new_xml) else: disk = ElementTree.Element("disk", {"type": "file", "device": "disk"}) disk.append(ElementTree.Element("driver", {"name": "qemu", "type": "qcow2"})) disk.append(ElementTree.Element("source", {"file": file_to_unassign})) disk.append(ElementTree.Element("target", {"dev": device_to_unassign, "bus": "virtio"})) xml = ElementTree.tostring(disk) self.instance.detachDeviceFlags( xml, VIR_DOMAIN_AFFECT_CURRENT | VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG )
def __str__(self): if PY3: # Return unicode return ET.tostring(self.get_printable_tree(), encoding="unicode") else: # Return bytes return ET.tostring(self.get_printable_tree())
def build_DAR(urls): """ urls consist of a list of tuples (dl_dir, url), where dl_dir is the download directory for each url""" if use_register: try: ET.register_namespace("ngeo", NGEO_URI) ET.register_namespace("xsi", XSI_URI) except Exception: pass root = ET.Element(NGEO_NS+DA_RESP, {XSI_NS+"schemaLocation":SCHEMA_LOCATION}) ET.SubElement(root, NGEO_NS+MONITORINGSTATUS).text = "IN_PROGRESS" pa_list = ET.SubElement(root, NGEO_NS+PRODACCESSLIST) for url in urls: pa = ET.SubElement(pa_list, NGEO_NS+PRODACCESS) ET.SubElement(pa, NGEO_NS+PRODACCESSURL).text = url[1] ET.SubElement(pa, NGEO_NS+PRODACCESSSTATUS).text = "READY" ET.SubElement(pa, NGEO_NS+PRODDOWNLOADDIRECTORY).text = url[0] if LOCAL_DEBUG > 0: print "dAR:\n"+ DAR_PREAMBLE + ET.tostring(root) return DAR_PREAMBLE + ET.tostring(root)
def check_block(case_block): case_block.set('xmlns', XMLNS) case_block = ElementTree.fromstring(ElementTree.tostring(case_block)) case_id = get_case_id(case_block) n = 0 def extra_info(): return "\n%s\n%s" % (ElementTree.tostring(case_block), map(ElementTree.tostring, blocks)) match = None for block in blocks: if get_case_id(block) == case_id: if should_have: if line_by_line: check_xml_line_by_line( testcase, ElementTree.tostring(case_block), ElementTree.tostring(block) ) match = block n += 1 if n == 2: testcase.fail( "Block for case_id '%s' appears twice" " in ota restore for user '%s':%s" % (case_id, user.username, extra_info()) ) else: testcase.fail( "User '%s' gets case '%s' " "but shouldn't:%s" % (user.username, case_id, extra_info()) ) if not n and should_have: testcase.fail("Block for case_id '%s' doesn't appear in ota restore for user '%s':%s" % (case_id, user.username, extra_info())) return match
def assertXMLEqual(self, first, second, msg=None, ignore_white_space=True): if first is None and second is None: return True if ignore_white_space: def cleanup(elem): for e in elem.iter(): if e.text is not None: e.text = e.text.strip() if e.tail is not None: e.tail = e.tail.strip() cleanup(first) cleanup(second) if (first is None and second is not None) or (second is None and first is not None): self.fail("unequal elements: first %r, second %r" % (first, second)) first_string = ET.tostring(prettify_tree(first, level=2)) second_string = ET.tostring(prettify_tree(second, level=2)) if not msg: msg = 'xml is not equal' msg += "\nfirst_string:\n" + first_string + \ "\nsecond_string:\n" + second_string if first_string != second_string: # Could be xml ordering - try reparsing to ensure consistent ordering. first_string = _serializePrefToString(_deserializePrefString(first_string)) second_string = _serializePrefToString(_deserializePrefString(second_string)) self.assertEqual(first_string, second_string, msg=msg)
def _xml_to_cdltransforms(xml): """Given some XML as a string, returns a list of CDLTransform objects for each ColorCorrection (returns a one-item list for a .cc file) """ tree = ET.fromstring(xml) # Strip away xmlns for elem in tree.getiterator(): if elem.tag.startswith("{"): elem.tag = elem.tag.partition("}")[2] filetype = tree.tag if filetype == "ColorCorrection": ccxml = ET.tostring(tree) cdl = OCIO.CDLTransform() cdl.setXML(ccxml) return [cdl] elif filetype == "ColorCorrectionCollection": allcdl = [] for cc in tree.getchildren(): ccxml = ET.tostring(cc) cdl = OCIO.CDLTransform() cdl.setXML(ccxml) allcdl.append(cdl) return allcdl else: raise RuntimeError( "The supplied file did not have the correct root element, expected" " 'ColorCorrection' or 'ColorCorrectionCollection', got %r" % (filetype))
def test_edit_config_2(self, mock_request): session = ncclient.transport.SSHSession(self.device_handler) session._server_capabilities = [":rollback-on-error", ":validate"] obj = EditConfig( session, self.device_handler, raise_mode=RaiseMode.ALL) root = """ system { host-name foo-bar; } """ obj.request(copy.deepcopy(root), format="text", target="running", error_option="rollback-on-error", default_operation="default", test_option="test") node = new_ele("edit-config") node.append(util.datastore_or_url("target", "running")) sub_ele(node, "error-option").text = "rollback-on-error" sub_ele(node, "test-option").text = "test" sub_ele(node, "default-operation").text = "default" config_text = sub_ele(node, "config-text") sub_ele(config_text, "configuration-text").text = root xml = ElementTree.tostring(node) call = mock_request.call_args_list[0][0][0] call = ElementTree.tostring(call) self.assertEqual(call, xml)
def hdd(root, test): nodes = [] nodes.append(root) fail_nodes = [] while len(nodes) > 0: temp = [] for node in nodes: for child in node: if test(string_to_list(ET.tostring(child))) == FAIL: if len(child) > 0: temp.append(child) else: fail_nodes.append(child) nodes = temp print len(fail_nodes) for node in fail_nodes: data = ET.tostring(node) #replace location of fail to correct fail string if FAIL_TAG in data: data = data.replace(FAIL_TAG, FAIL_STRING, 1) print data circumstances = string_to_list(data) test(ddmin(circumstances, test)) showResult(TEMP_FILE) return fail_nodes
def test_xml_output(lldpd1, lldpd, lldpcli, namespaces, uname, command, expected): with namespaces(2): lldpd() with namespaces(1): result = lldpcli( *shlex.split("-f xml show {} details".format(command))) assert result.returncode == 0 out = result.stdout.decode('ascii') xml = ET.fromstring(out) age = xml.findall('./interface[1]')[0].attrib['age'] router = xml.findall("./interface[1]/chassis/" "capability[@type='Router']")[0].attrib['enabled'] station = xml.findall("./interface[1]/chassis/" "capability[@type='Station']")[0].attrib['enabled'] if 'Dot3' in pytest.config.lldpd.features: dot3 = """ <auto-negotiation enabled="no" label="PMD autoneg" supported="no"> <current label="MAU oper type">10GigBaseCX4 - X copper over 8 pair 100-Ohm balanced cable</current> </auto-negotiation>""" else: dot3 = "" expected = ET.fromstring(expected.format(age=age, router=router, station=station, uname=uname, dot3=dot3)) assert ET.tostring(xml) == ET.tostring(expected)
def get_info_from_xml(fpath): keywords = [] abstract = "" title = "" body = "" categories = set() tree = ET.parse(fpath) root = tree.getroot() for kwd in root.iter("kwd"): out_kw = ET.tostring(kwd,'utf-8') keywords.append(re.sub(r'<[^<>]*>','',out_kw).strip()) for abst in root.iter('abstract'): for p in abst.iter('p'): out_abst = ET.tostring(p,'utf-8') abstract += re.sub(r'<[^<>]*>','',out_abst).strip() + "\n" for titlegroup in root.iter('title-group'): for titlestring in titlegroup.iter('article-title'): out_title = ET.tostring(titlestring,'utf-8') title += re.sub(r'<[^<>]*>','',out_title).strip() + "\n" for bdy in root.iter('body'): for p in bdy.iter('p'): out_body = ET.tostring(p,'utf-8') body += re.sub(r'<[^<>]*>','',out_body).strip() + "\n" for cat in root.iter("article-categories"): for subj in cat.iter('subject'): out_subj = ET.tostring(subj,'utf-8') categories.add(re.sub(r'<[^<>]*>','',out_subj).strip()) if not categories: categories.add("none") return title,keywords,abstract,body,categories
def create_main_html(tables_tape,tables_disk, sites_tape, sites_disk, output_dir): html = open("main_page.tmpl").read() column_tape = "" for site in sites_tape: column_tape += '<a href="tape_storage_overview_'+site.name+'.html">' + site.name + '</a> ' column_disk = "" for site in sites_disk: column_disk += '<a href="disk_storage_overview_'+site.name+'.html">' + site.name + '</a> ' fp = open(os.path.join(output_dir, "main.html"), "w") fp.write(html.format( title='Storage Overview', title_tape='Tape Storage Overview', title_disk='Disk Storage Overview', date=date.today(), summary_tape=ET.tostring(tables_tape.summary()), delta_summary_tape=ET.tostring(tables_tape.summary(delta=True)), summary_disk=ET.tostring(tables_disk.summary()), delta_summary_disk=ET.tostring(tables_disk.summary(delta=True)), column_tape=column_tape, column_disk=column_disk, fulldate=date.today() )) fp.close()
def get_fixtures(request, domain, user_id, fixture_id=None): try: user = CommCareUser.get_by_user_id(user_id) except CouchUser.AccountTypeError: err = ("You can't use case sharing or fixtures as a %s. " "Login as a mobile worker and try again.") % settings.WEB_USER_TERM, return HttpResponse(err, status=412, content_type="text/plain") if not user: raise Http404 assert user.is_member_of(domain) restore_user = user.to_ota_restore_user() if not fixture_id: ret = ElementTree.Element("fixtures") for fixture in generator.get_fixtures(restore_user, version=V2): ret.append(fixture) return HttpResponse(ElementTree.tostring(ret), content_type="text/xml") else: fixture = generator.get_fixture_by_id(fixture_id, restore_user, version=V2) if not fixture: raise Http404 assert len(fixture.getchildren()) == 1, 'fixture {} expected 1 child but found {}'.format( fixture_id, len(fixture.getchildren()) ) return HttpResponse(ElementTree.tostring(fixture.getchildren()[0]), content_type="text/xml")
def test_product_fixture(self): user = bootstrap_user(self, phone_number="1234567890") xml = self.generate_product_fixture_xml(user) fixture = product_fixture_generator(user, V1) self.assertXmlEqual(xml, ElementTree.tostring(fixture[0]))
<nodeProperties/> </slave> ''' # Load the node's config into an xml tree for ease of manipulation. tree_node_config = ElementTree.fromstring(str_xml_node_config) # Set all the original data again. set_element_text(tree_node_config, "description", args.description) set_element_text(tree_node_config, "remoteFS", args.path) set_element_text(tree_node_config, "numExecutors", args.num_executors) set_element_text(tree_node_config, "mode", args.mode) set_element_text(tree_node_config, "label", args.labels) # Double check that the changes were made. str_xml_node_config_mod = ElementTree.tostring(tree_node_config) if args.verbose: print "AFTER MOD" print "----------" print str_xml_node_config_mod print # Reconfig the node. server.reconfig_node(args.name, str_xml_node_config_mod) # Run a script to get the node's secret. # https://support.cloudbees.com/hc/en-us/articles/222520647-How-to-find-slave-secret-key- node_secret = server.run_script( "for (aSlave in hudson.model.Hudson.instance.slaves) " "{ if (aSlave.name == '" + args.name + "') { println aSlave.getComputer().getJnlpMac() } }")
def make_svg_bargraph(labels, heights, categories=None, palette=None, barheight=100, barwidth=12, show_labels=True, file_header=False, data_url=False): if palette is None: palette = default_bargraph_palette if categories is None: categories = [('', len(labels))] unitheight = float(barheight) / max(max(heights, default=1), 1) textheight = barheight if show_labels else 0 labelsize = float(barwidth) gap = float(barwidth) / 4 # textsize = barwidth + gap textsize = barwidth + gap / 2 rollup = max(heights, default=1) textmargin = float(labelsize) * 2 / 3 leftmargin = 32 rightmargin = 8 svgwidth = len(heights) * (barwidth + gap) + 2 * leftmargin + rightmargin svgheight = barheight + textheight # create an SVG XML element svg = et.Element('svg', width=str(svgwidth), height=str(svgheight), version='1.1', xmlns='http://www.w3.org/2000/svg') # Draw the bar graph basey = svgheight - textheight x = leftmargin # Add units scale on left if len(heights): for h in [1, (max(heights) + 1) // 2, max(heights)]: et.SubElement(svg, 'text', x='0', y='0', style=('font-family:sans-serif;font-size:%dpx;' + 'text-anchor:end;alignment-baseline:hanging;' + 'transform:translate(%dpx, %dpx);') % (textsize, x - gap, basey - h * unitheight)).text = str(h) et.SubElement(svg, 'text', x='0', y='0', style=('font-family:sans-serif;font-size:%dpx;' + 'text-anchor:middle;' + 'transform:translate(%dpx, %dpx) rotate(-90deg)') % (textsize, x - gap - textsize, basey - h * unitheight / 2) ).text = 'units' # Draw big category background rectangles for catindex, (cat, catcount) in enumerate(categories): if not catcount: continue et.SubElement(svg, 'rect', x=str(x), y=str(basey - rollup * unitheight), width=(str((barwidth + gap) * catcount - gap)), height=str(rollup * unitheight), fill=palette[catindex % len(palette)][1]) x += (barwidth + gap) * catcount # Draw small bars as well as 45degree text labels x = leftmargin catindex = -1 catcount = 0 for label, height in zip(labels, heights): while not catcount and catindex <= len(categories): catindex += 1 catcount = categories[catindex][1] color = palette[catindex % len(palette)][0] et.SubElement(svg, 'rect', x=str(x), y=str(basey - (height * unitheight)), width=str(barwidth), height=str(height * unitheight), fill=color) x += barwidth if show_labels: et.SubElement(svg, 'text', x='0', y='0', style=('font-family:sans-serif;font-size:%dpx;text-anchor:end;' + 'transform:translate(%dpx, %dpx) rotate(-45deg);') % (labelsize, x, basey + textmargin)).text = label x += gap catcount -= 1 # Text labels for each category x = leftmargin for cat, catcount in categories: if not catcount: continue et.SubElement(svg, 'text', x='0', y='0', style=('font-family:sans-serif;font-size:%dpx;text-anchor:end;' + 'transform:translate(%dpx, %dpx) rotate(-90deg);') % (textsize, x + (barwidth + gap) * catcount - gap, basey - rollup * unitheight + gap)).text = '%d %s' % ( catcount, cat + ('s' if catcount != 1 else '')) x += (barwidth + gap) * catcount # Output - this is the bare svg. result = et.tostring(svg).decode('utf-8') if file_header or data_url: result = ''.join([ '<?xml version=\"1.0\" standalone=\"no\"?>\n', '<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n', '\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n', result]) if data_url: import base64 result = 'data:image/svg+xml;base64,' + base64.b64encode( result.encode('utf-8')).decode('utf-8') return result
break elif tbody_start: td_search = td_re.search(line) current_item = {} td_search = td_re.split(line) current_item['mcc'] = td_search[1] current_item['mnc'] = td_search[2] current_item['iso'] = td_search[3] current_item['country'] = td_search[4] current_item['country_code'] = td_search[5] current_item['network'] = td_search[6][0:-1] mcc_mnc_list.append(current_item) root_element = element_tree.Element('carriers') for carrier in mcc_mnc_list: sub_element = element_tree.Element('carrier') for key, value in carrier.items(): child_element = element_tree.Element(key) child_element.text = value sub_element.append(child_element) root_element.append(sub_element) print( minidom.parseString( element_tree.tostring(root_element)).toprettyxml(indent=' '))
def dictParseQMark(userId=1, userSecret="", hideSecret=False): strQMarkXml = strRetrieveQMark(userId, userSecret) etQMarkXmlRoot = None try: etQMarkXmlRoot = ET.fromstring(strQMarkXml) etQMarkXmlRoot = etQMarkXmlRoot.find("channel") except Exception as e: # print("Invalid RSS") return {"valid": False, "message": "Invalid RSS"} # ============================================================ # Parse XML into dictQMark dictQMark = {"valid": True} # Include Title etQmarkProcessing = etQMarkXmlRoot.find("title") dictQMark["title"] = etQmarkProcessing.text # Include LastBuildDate etQmarkProcessing = etQMarkXmlRoot.find("lastBuildDate") dictQMark["lastBuildDate"] = etQmarkProcessing.text if (hideSecret): # As of Mar 2021, secret element is the first and only element with attribute "rel" etQmarkProcessing = etQMarkXmlRoot.find('*[@rel="self"]') # print(etQMarkSecret) # debug dictQMark["link"] = etQmarkProcessing.attrib['href'] # Include Mark Items dictQMark["items"] = [] for etQMarkXmlItem in etQMarkXmlRoot.findall("item"): dictQMarkItem = {} # Retrieve Marked Article Title etQmarkProcessing = etQMarkXmlItem.find("title") if (etQmarkProcessing is None): dictQMarkItem["title"] = "" else: dictQMarkItem["title"] = etQmarkProcessing.text # Include Marked Article Link etQmarkProcessing = etQMarkXmlItem.find("link") if (etQmarkProcessing is None): dictQMarkItem["link"] = "" else: dictQMarkItem["link"] = etQmarkProcessing.text etQmarkProcessing = etQMarkXmlItem.find("description") if ((etQmarkProcessing is not None) and (etQmarkProcessing.text is not None)): etQmarkProcessing = ET.fromstring( "<div>" + html.unescape(etQmarkProcessing.text.strip()) + "</div>") else: etQmarkProcessing = ET.Element("div") # print( ET.tostring(etQmarkProcessing, encoding='unicode') ) # Include Quotes (if any) etQMarkItemQuotes = etQmarkProcessing.find("blockquote") dictQMarkItem["quotes"] = [] if etQMarkItemQuotes is not None: for etQuote in etQMarkItemQuotes: dictQMarkItem["quotes"].append( ET.tostring(etQuote, encoding='unicode')) # Include Notes (if any) etQMarkItemNotes = etQmarkProcessing.find("aside") dictQMarkItem["notes"] = [] if etQMarkItemNotes is not None: # print("text: "+etQMarkItemNotes.text+"endtext") # debug dictQMarkItem["notes"].append( ET.tostring(etQMarkItemNotes, encoding='unicode')) # Include Mark Item Link etQmarkProcessing = etQMarkXmlItem.find("guid") dictQMarkItem["guid"] = etQmarkProcessing.text # Include Mark Item PubDate etQmarkProcessing = etQMarkXmlItem.find("pubDate") dictQMarkItem["pubDate"] = etQmarkProcessing.text dictQMark["items"].append(dictQMarkItem) # print(json.dumps(dictQMarkItem)) # debug return dictQMark
def runCmd(self, cmd, engine, cluster, eclfile, report, **kwargs): args = [] args.append(cmd) args.append('-v') args.append('-fpickBestEngine=false') args.append('--target=' + engine) args.append('--cluster=' + cluster) args.append('--port=' + self.config.espSocket) if self.config.useSsl.lower() == 'true': args.append('--ssl') retryCount = int(kwargs.pop('retryCount', 1)) args.append('--wait='+str(retryCount * eclfile.getTimeout() * 1000)) # ms server = kwargs.pop('server', False) if server: args.append('--server=' + server) username = kwargs.pop('username', False) if username: args.append("--username="******"--password="******"--name=" + name) else: args.append('--exception-level=warning') args.append('--noroot') name = kwargs.pop('name', False) if not name: name = eclfile.getJobname() args.append("--name=" + name) args = args + eclfile.getDParameters() args = args + eclfile.getStoredInputParameters() args.append(eclfile.getArchive()) data = "" wuid = "N/A" state = "" results='' try: if eclfile.flushDiskCache(): # At the moment it is not a critical problem if the clearOSCache() # fails, the test case should execute anyway try: clearOSCache(eclfile.getTaskId()) pass finally: # Go on pass #print "runCmd:", args results, stderr = self.__ECLcmd()(*args) logging.debug("%3d. results:'%s'", eclfile.getTaskId(), results) logging.debug("%3d. stderr :'%s'", eclfile.getTaskId(), stderr) data = '\n'.join(line for line in results.split('\n') if line) + "\n" ret = data.split('\n') result = "" cnt = 0 for i in ret: logging.debug("%3d. ret:'%s'", eclfile.getTaskId(), i ) if "wuid:" in i: logging.debug("------ runCmd:" + repr(i) + "------") wuid = i.split()[1] if "state:" in i: state = i.split()[1] if "aborted" in i: state = "aborted" if cnt > 4: if i.startswith('<Warning>') or i.startswith('<Exception>'): # Remove absolute path from filename to # enable to compare it with same part of keyfile xml = ET.fromstring(i) try: path = xml.find('.//Filename').text logging.debug("%3d. path:'%s'", eclfile.getTaskId(), path ) filename = os.path.basename(path) xml.find('.//Filename').text = filename except: logging.debug("%3d. Unexpected error: %s (line: %s) ", eclfile.getTaskId(), str(sys.exc_info()[0]), str(inspect.stack()[0][2])) finally: i = ET.tostring(xml) logging.debug("%3d. ret:'%s'", eclfile.getTaskId(), i ) pass result += i + "\n" cnt += 1 data = '\n'.join(line for line in result.split('\n') if line) + "\n" except Error as err: data = str(err) logging.error("------" + err + "------") raise err except: err = Error("6007") logging.critical(err) logging.critical(traceback.format_exc()) raise err finally: res = queryWuid(eclfile.getJobname(), eclfile.getTaskId()) logging.debug("%3d. in finally -> 'wuid':'%s', 'state':'%s', data':'%s', ", eclfile.getTaskId(), res['wuid'], res['state'], data) if wuid == 'N/A': logging.debug("%3d. in finally queryWuid() -> 'result':'%s', 'wuid':'%s', 'state':'%s'", eclfile.getTaskId(), res['result'], res['wuid'], res['state']) wuid = res['wuid'] if res['result'] != "OK": eclfile.diff=eclfile.getBaseEcl()+'\n\t'+res['state']+'\n' logging.error("%3d. %s in queryWuid(%s)", eclfile.getTaskId(), res['state'], eclfile.getJobname()) try: eclfile.addResults(data, wuid) except: logging.critical("Exception in eclfile.addResults()") logging.critical(traceback.format_exc()) if cmd == 'publish': if state == 'compiled': test = True else: test = False eclfile.diff = 'Error' else: if (res['state'] == 'aborted') or eclfile.isAborted(): eclfile.diff = ("%3d. Test: %s\n") % (eclfile.taskId, eclfile.getBaseEclRealName()) eclfile.diff += '\t'+'Aborted ( reason: '+eclfile.getAbortReason()+' )' test = False elif eclfile.getIgnoreResult(): logging.debug("%3d. Ignore result (ecl:'%s')", eclfile.getTaskId(), eclfile.getBaseEclRealName()) test = True elif eclfile.testFail(): if res['state'] == 'completed': logging.debug("%3d. Completed but Fail is the expected result (ecl:'%s')", eclfile.getTaskId(), eclfile.getBaseEclRealName()) test = False else: logging.debug("%3d. Fail is the expected result (ecl:'%s')", eclfile.getTaskId(), eclfile.getBaseEclRealName()) test = True elif eclfile.testNoKey(): # keyfile comparaison disabled with //nokey tag if eclfile.testNoOutput(): #output generation disabled with //nooutput tag eclfile.diff = '-' else: eclfile.diff = ("%3d. Test: %s\n") % (eclfile.taskId, eclfile.getBaseEclRealName()) eclfile.diff += data test = True elif (res['state'] == 'failed'): resultLines = data.strip().split('\n') resultLineIndex = 0; while not resultLines[resultLineIndex].startswith('<'): resultLineIndex += 1 logging.debug("%3d. State is fail (resultLineIndex:%d, resultLines:'%s' )", eclfile.getTaskId(), resultLineIndex, resultLines) data = '\n'.join(resultLines[resultLineIndex:])+ "\n" eclfile.addResults(data, wuid) logging.debug("%3d. State is fail (resultLineIndex:%d, data:'%s' )", eclfile.getTaskId(), resultLineIndex, data) test = eclfile.testResults() else: test = eclfile.testResults() report.addResult(eclfile) if not test: return False else: return True
gdppc.text = '141100' country.append(gdppc) country.append(ET.Element('neighbor', name="Austria", direction="E")) country.append(ET.Element('neighbor', name="Switzerland", direction="W")) root.append(country) country = ET.Element('country', name="Singapore") root.append(country) ... indent(root) xml_str = ET.tostring(root, encoding="utf-8", method="xml") print(xml_str.decode(encoding="utf-8")) # <data> # <country name="Liechtenstein"> # <rank updated="yes">2</rank> # <year>2008</year> # <gdppc>141100</gdppc> # <neighbor direction="E" name="Austria" /> # <neighbor direction="W" name="Switzerland" /> # </country> # <country name="Singapore" /> # </data> etree = ET.ElementTree(root) f = io.BytesIO() etree.write(f, encoding='utf-8', xml_declaration=True)
def _lxml_prettyprint(self): """ XML pretty printer using lxml """ return lxml.etree.tostring(lxml.etree.fromstring( ET.tostring(self.root, encoding="UTF-8")), pretty_print=True)
class HttpsServerCommunicator(SecureServerCommunicator): def __init__(self, remote, ctrl, communicator=None): (addr, self.ssl_ctx) = remote self.f_ctrl = ctrl self.http = HttpMessage(self) self.http_communicator = None SecureServerCommunicator.__init__(self, addr, communicator=communicator) self.parent_http = None if communicator is not None: self.parent_http = self.communicator.http def handle_read(self): # Read data in buffer SecureServerCommunicator.handle_read(self) # Fetch the Http request from parent communicator if (self.http_communicator is None) and (len( self.communicator.http_history) > 0): self.http_communicator = self.communicator.http_history.pop(0) if self.http_communicator.path in Config.force_buffering: Logger.debug("Force buffering : " + self.http_communicator.path) self.http.force_full_buffering = True if not self.http.is_ready(): # Parse it until ready if self.make_http_message(): # Now it is ready self._buffer = self.process() # Stream it self.http_communicator = None else: # Data is streamed "on-the-fly" self._buffer = self._buffer # Stream it if self.http.is_complete(): self.http = HttpMessage(self) def make_http_message(self): # Do we have headers ? if not self.http.have_headers(): if self.http.put_headers() is None: return False # Now we have valid headers # Push data self.http.put_body() return self.http.is_ready() def process(self): # in any case of redirection with HTTP protocol use if self.http.have_redirection(): location = self.http.get_header("Location") if location is not None and location.startswith("http://"): location = location.replace("http", "https", 1) self.http.set_header("Location", location) # XML rewriting on start request if (self.http_communicator is not None) and (self.http_communicator.path == "/ovd/client/start"): body = self.http.get_body() xml = self.rewrite_xml(body) self.http.set_body(xml) return self.http.show() def rewrite_xml(self, body): try: session = parser.XML(body) if session.tag.lower() != 'session': raise Exception("not a 'session' XML response") except Exception: Logger.exception("Gateway:: parsing XML session failed") return None session.set('mode_gateway', 'on') for server in session.findall('server'): port = Protocol.RDP if server.attrib.has_key("port"): try: port = int(server.attrib["port"]) except ValueError, err: Logger.warn( "Gateway:: Invalid protocol: server port attribute is not a digit (%s)" % (server.attrib["port"])) token = self.f_ctrl.send( ('insert_token', (server.attrib['fqdn'], port))) server.set('token', token) del server.attrib['fqdn'] if server.attrib.has_key("port"): del server.attrib["port"] for server in session.findall('webapp-server'): if server.attrib.has_key("webapps-url"): url = server.attrib["webapps-url"] if url is not None: token = self.f_ctrl.send(('assign_token', url)) host = self.parent_http.get_header("Host") server.attrib[ "webapps-url"] = "https://" + host + "/webapps-" + token return parser.tostring(session)
def _manual_prettyprint(self): """ XML pretty printer using xml_indent """ xml_indent(self.root) return ET.tostring(self.root, encoding="UTF-8")
# # Testing # if (__name__ == "__main__"): import sys from xml.dom import minidom if 1: p1 = halParameters(sys.argv[1]) p2 = halParameters(sys.argv[2]) string = ElementTree.tostring(p2.toXML(), 'utf-8') reparsed = minidom.parseString(string) print reparsed.toprettyxml(indent=" ", encoding="ISO-8859-1") if 0: pm = parameters(sys.argv[1], True) print pm.get("film") print pm.get("film.directory") # string = ElementTree.tostring(pm.toXML(), 'utf-8') # reparsed = minidom.parseString(string) # print reparsed.toprettyxml(indent = " ", encoding = "ISO-8859-1") # # The MIT License #
def main(): #List of Objects to be updated with open('list.txt', 'r') as file: task = file.readlines() depth_main = len(task) if source_env.upper() == 'DEV': s_env_var = 'emsbucdev' elif source_env.upper() == 'TEST': s_env_var = 'emsbuctst' elif source_env.upper() == 'MAUI': s_env_var = 'emsbuce2e' elif source_env.upper() == 'PREPROD': s_env_var = 'emsbuccppe' elif source_env.upper() == 'PROD': s_env_var = 'emsbuccprd' if target_env.upper() == 'DEV': t_env_var = 'emsbucdev' elif target_env.upper() == 'TEST': t_env_var = 'emsbuctst' elif target_env.upper() == 'MAUI': t_env_var = 'emsbuce2e' elif target_env.upper() == 'PREPROD': t_env_var = 'emsbuccppe' elif target_env.upper() == 'PROD': t_env_var = 'emsbuccprd' #loop for every task for t in range(0, depth_main): obj_name = task[t].rstrip("\n") tgt_url = 'https://' + t_env_var + '/opswise/resources/' + obj_type + '?' + obj_type + 'name=' + obj_name if v_action.lower() == 'backup': backup(tgt_url, obj_name) if v_action.lower() == 'deploy': src_url = 'https://' + s_env_var + '/opswise/resources/' + obj_type + '?' + obj_type + 'name=' + obj_name response = requests.get(src_url, auth=('vaverma', passwd)) if response.status_code == 404: print('obj_name not found ' + obj_name) else: file_name = obj_name + '.xml' with open(file_name, 'wb') as file: file.write(response.content) tree = ET.parse(file_name) root = tree.getroot() param = ET.tostring(root, encoding='utf-8', method='xml') deploy(tgt_url, param, header) if v_action.lower() in ('enable', 'disable'): if v_action == 'enable': v_act = 'true' else: v_act = 'false' with open('data.xml', 'wb') as file: file.write('<enable-disable-trigger><trigger enable="' + v_act + '" name="' + obj_name + '"></trigger></enable-disable-trigger>') tree = ET.parse('data.xml') root = tree.getroot() param = ET.tostring(root, encoding='utf-8', method='xml') enable_disable_trg(t_env_var, param) if v_action.lower() == 'delete': delete(tgt_url, header) return
def prettify(elem): rough_string = ElementTree.tostring(elem, 'utf-8') reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent=" ")
def saveToFile(self, filename): rough_string = ElementTree.tostring(self.toXML()) reparsed = minidom.parseString(rough_string) with open(filename, "w") as fp: fp.write(reparsed.toprettyxml(indent=" ", encoding="ISO-8859-1"))
def prettify(elem): """Return a pretty-printed XML string for the Element.""" rough_string = ElementTree.tostring(elem, 'utf-8') reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent=" ")
def to_text(src, width): while True: try: root = ET.fromstring(src) break except Exception as ent: ent = str(ent) ent = re.search("(?<=undefined entity &).*?;(?=:)", ent).group() src = re.sub("&" + ent, "", src.decode("utf-8")).encode("utf-8") # src = re.sub("&" + ent, html5[ent], src.decode("utf-8")).encode("utf-8") body = root.find("XHTML:body", NS) text, imgs = [], [] # for i in body.findall("*", NS): # for i in body.findall(".//XHTML:p", NS): for i in body.findall(".//*"): if re.match("{"+NS["XHTML"]+"}h[0-9]", i.tag) != None: for j in i.itertext(): text.append(unescape(j).rjust(width//2 + len(unescape(j))//2 - RIGHTPADDING)) text.append("") elif re.match("{"+NS["XHTML"]+"}(p|div)", i.tag) != None: par = ET.tostring(i, encoding="utf-8").decode("utf-8") par = unescape(par) par = re.sub("<[^>]*>", "", par) par = re.sub("\t", "", par) par = textwrap.fill(par, width) text += par.splitlines() + [""] elif re.match("{"+NS["XHTML"]+"}dt", i.tag) != None: dt = ET.tostring(i, encoding="utf-8").decode("utf-8") dt = unescape(dt) dt = re.sub("<[^>]*>", "", dt) dt = re.sub("\t", "", dt) dt = textwrap.fill(dt, width - 2) text += [""] + [" " + j for j in dt.splitlines()] # + [""] # elif re.match("{"+NS["XHTML"]+"}dd", i.tag) != None: # dd = ET.tostring(i, encoding="utf-8").decode("utf-8") # dd = unescape(dd) # dd = re.sub("<[^>]*>", "", dd) # dd = re.sub("\t", "", dd) # dd = textwrap.fill(dd, width - 4) # text += [" " + j for j in dd.splitlines()] + [""] elif re.match("{"+NS["XHTML"]+"}(pre|blockquote)", i.tag) != None: block = ET.tostring(i, encoding="utf-8").decode("utf-8") block = unescape(block) block = re.sub("<[^>]*>", "", block) block = re.sub("\t", "", block) block = textwrap.fill(block, width - 2) text += [" " + j for j in block.splitlines()] + [""] elif re.match("{"+NS["XHTML"]+"}li", i.tag) != None: li = ET.tostring(i, encoding="utf-8").decode("utf-8") li = unescape(li) li = re.sub("<[^>]*>", "", li) li = re.sub("\t", "", li) li = textwrap.fill(li, width - 2) text += [" - " + j if n == 0 else " " + j for n, j in enumerate(li.splitlines())] + [""] elif re.match("{"+NS["XHTML"]+"}img", i.tag) != None: text.append("[IMG:{}]".format(len(imgs))) text.append("") imgs.append(unquote(i.attrib["src"])) return text + [""], imgs
def __format_request(self, request=None): body = et.Element('qdbapi') for f, v in six.iteritems(request): self.__add_element(body, f, v) return et.tostring(body)
def _from_caseblock(cls, domain, caseblock): username = const.COMMTRACK_USERNAME casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, const.get_commtrack_user_id(domain), xmlns=const.COMMTRACK_SUPPLY_POINT_XMLNS) return cls.get(caseblock._id)
OSM_FILE = "seattle_washington.osm" # Replace this with your osm file SAMPLE_FILE = "sample.osm" k = 1500 # Parameter: take every k-th top level element def get_element(osm_file, tags=('node', 'way', 'relation')): """Yield element if it is the right type of tag Reference: http://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python """ context = iter(ET.iterparse(osm_file, events=('start', 'end'))) _, root = next(context) for event, elem in context: if event == 'end' and elem.tag in tags: yield elem root.clear() with open(SAMPLE_FILE, 'wb') as output: output.write('<?xml version="1.0" encoding="UTF-8"?>\n') output.write('<osm>\n ') # Write every kth top level element for i, element in enumerate(get_element(OSM_FILE)): if i % k == 0: output.write(ET.tostring(element, encoding='utf-8')) output.write('</osm>')
def register_excel(excel_file, id_system: str, ns): wb = xl.load_workbook(excel_file) sh = wb['Sheet1'] m_row = sh.max_row last_line = 0 data = {'Patient': {}, 'Encounter': {}, 'Observation': []} for i in range(1, m_row + 1): cell = sh.cell(row=i, column=4) if (cell.value): tag = sh.cell(row=i, column=2) if tag.value == 'HO_TEN': data['Patient']['name'] = cell.value elif tag.value == 'NGAY_SINH': data['Patient']['birthDate'] = cell.value elif tag.value == 'GIOI_TINH': data['Patient']['gender'] = cell.value elif tag.value == 'DIA_CHI': data['Patient']['address'] = [{'address': cell.value}] elif tag.value == 'NGAY_VAO': data['Encounter']['period'] = {'start': cell.value} elif tag.value == 'NGAY_RA': if data['Encounter'].get('period'): data['Encounter']['period']['stop'] = cell.value else: data['Encounter']['period'] = {'stop': cell.value} elif tag.value == 'MA_LOAI_KCB': data['Encounter']['class'] = cell.value elif tag.value == 'MA_KHOA': data['Encounter']['location'] = cell.value elif tag.value == 'SO_NGAY_DTRI': data['Encounter']['length'] = cell.value elif not tag.value: tag_2 = sh.cell(row=i, column=3) tag_2_content = tag_2.value.split('.') if tag_2_content[0] == 'Patient': if data['Patient'][tag_2_content[1]]: data['Patient'][tag_2_content[1]].append( {tag_2_content[1]: cell.value}) if len(tag_2_content) > 2: for i in range(2, len(tag_2_content)): data['Patient'][tag_2_content[1]][-1][ tag_2_content[i].split('=') [0]] = tag_2_content[i].split('=')[1] elif tag_2_content[0] == 'Encounter': if data['Encounter'][tag_2_content[1]]: data['Encounter'][tag_2_content[1]].append( {tag_2_content[1]: cell.value}) if len(tag_2_content) > 2: for i in range(2, len(tag_2_content)): data['Encounter'][tag_2_content[1]][-1][ tag_2_content[i].split('=') [0]] = tag_2_content[i].split('=')[1] elif tag_2_content[0] == 'Observation': _observation = {} for i in range(1, len(tag_2_content)): _observation[tag_2_content[i].split('=') [0]] = tag_2_content[i].split('=')[1] _observation['valueQuantity'] = cell.value data['Observation'].append(_observation) data['Patient']['identifier'] = '12345' root = ET.Element('Patient') tree = ET.ElementTree(root) root.set("xmlns", "http://hl7.org/fhir") if data['Patient'].values(): identifier = ET.SubElement(root, 'identifier') dt.identifier_type( identifier, id_system, '12345', 'usual', { 'codes': [{ 'system': 'http://terminology.hl7.org/CodeSystem/v2-0203', 'code': 'MR' }] }) if data['Patient'].get('name'): name = ET.SubElement(root, 'name') dt.name_type(name, data['Patient']['name']) if data['Patient'].get('gender'): gender = ET.SubElement(root, 'gender') if data['Patient']['gender'] == 'Nam': code = 'male' elif data['Patient']['gender'] == 'Nữ': code = 'female' gender.set('value', code) if data['Patient'].get('birthDate'): birthDate = ET.SubElement(root, 'birthDate') birthDate.set('value', data['Patient']['birthDate']) if data['Patient'].get('address'): for value in data['Patient']['address']: address = ET.SubElement(root, 'address') dt.address_type(address, value.get('address'), value.get('postalCode'), value.get('country'), value.get('use'), value.get('type')) if data['Patient'].get('contact'): contact = ET.SubElement(root, 'contact') put_req = None post_req = None encounter_id = None get_req = requests.get( "http://hapi.fhir.org/baseR4/Patient?identifier=urn:trinhcongminh|" + '12345', headers={'Content-type': 'application/xml'}) if get_req.status_code == 200 and 'entry' in get_req.content.decode( 'utf-8'): get_root = ET.fromstring(get_req.content.decode('utf-8')) entry = get_root.find('d:entry', ns) resource = entry.find('d:resource', ns) patient_resource = resource.find('d:Patient', ns) id_resource = patient_resource.find('d:id', ns) patient_id = id_resource.attrib['value'] root.insert(0, ET.Element('id')) res_id = root.find('id') res_id.set('value', patient_id) text = ET.tostring(root, encoding="us-ascii", method="xml", xml_declaration=None, default_namespace=None, short_empty_elements=True) put_req = requests.put("http://hapi.fhir.org/baseR4/Patient/" + patient_id, headers={'Content-type': 'application/xml'}, data=text.decode('utf-8')) print( ET.tostring(root, encoding="us-ascii", method="xml", xml_declaration=None, default_namespace=None, short_empty_elements=True)) else: text = ET.tostring(root, encoding="us-ascii", method="xml", xml_declaration=None, default_namespace=None, short_empty_elements=True) post_req = requests.post("http://hapi.fhir.org/baseR4/Patient/", headers={'Content-type': 'application/xml'}, data=text.decode('utf-8')) if post_req.status_code == 201: get_root = ET.fromstring(get_req.content.decode('utf-8')) ns = {'d': "http://hl7.org/fhir"} id_resource = get_root.find('d:id', ns) patient_id = id_resource.attrib['value'] if (put_req and put_req.status_code == 200) or (post_req and post_req.status_code == 201): if data['Encounter']: root = ET.Element('Encounter') tree = ET.ElementTree(root) root.set("xmlns", "http://hl7.org/fhir") if data['Encounter'].values(): ids = ET.SubElement(root, 'id') ids.set('value', '1') if not data['Encounter'].get('status'): status = ET.SubElement(root, 'status') status.set('value', 'in-progress') else: status = ET.SubElement(root, 'status') status.set('value', data['Encounter']['status']) if data['Encounter'].get('class'): _class = ET.SubElement(root, 'class') dt.coding_type( _class, 'http://terminology.hl7.org/CodeSystem/v3-ActCode', data['Encounter']['class']) if data['Encounter'].get('serviceType'): serviceType = ET.SubElement(root, 'serviceType') subject = ET.SubElement(root, 'subject') dt.reference_type(subject, 'Patient/' + patient_id, 'Patient', display=data['Patient']['name']) if data['Encounter'].get('period'): period = ET.SubElement(root, 'period') dt.period_type(period, data['Encounter']['period'].get('start'), data['Encounter']['period'].get('stop')) if data['Encounter'].get('length'): length = ET.SubElement(root, 'length') dt.duration_type(length, data['Encounter']['length'], 'days', 'http://unitsofmeasure.org', 'd') if data['Encounter'].get('serviceProvider'): serviceProvider = ET.SubElement(root, 'serviceProvider') text = ET.tostring(root, encoding="us-ascii", method="xml", xml_declaration=None, default_namespace=None, short_empty_elements=True) post_req = requests.post( "http://hapi.fhir.org/baseR4/Encounter/", headers={'Content-type': 'application/xml'}, data=text.decode('utf-8')) if post_req.status_code == 201: get_root = ET.fromstring(post_req.content.decode('utf-8')) ns = {'d': "http://hl7.org/fhir"} id_resource = get_root.find('d:id', ns) encounter_id = id_resource.attrib['value'] print(encounter_id) if data['Observation']: for i in range(len(data['Observation'])): root = ET.Element('Observation') tree = ET.ElementTree(root) root.set('xmlns', 'http://hl7.org/fhir') ids = ET.SubElement(root, 'id') ids.set('value', '{}'.format(i)) if data['Observation'][i].get('status'): status = ET.SubElement(root, 'status') status.set('value', data['Observation'][i]['status']) if data['Observation'][i].get('category'): category = ET.SubElement(root, 'category') coding = ET.SubElement(category, 'coding') dt.coding_type( coding, 'http://terminology.hl7.org/CodeSystem/observation-category', data['Observation'][i]['category']) if data['Observation'][i].get('code'): code = ET.SubElement(root, 'code') if patient_id: subject = ET.SubElement(root, 'subject') dt.reference_type(subject, 'Patient/' + patient_id, 'Patient', display=data['Patient']['name']) if encounter_id: encounter = ET.SubElement(root, 'encounter') dt.reference_type(encounter, 'Encounter/' + encounter_id, 'Encounter') if data['Observation'][i]['code'] == '8867-4': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '8867-4', display='Heart rate') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'beats/minute', 'http://unitsofmeasure.org', '{Beats}/min') elif data['Observation'][i]['code'] == '8310-5': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '8867-4', display='Body temperature') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'Cel', 'http://unitsofmeasure.org', 'Cel') elif data['Observation'][i]['code'] == '8480-6': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '8480-6', display='Systolic blood pressure') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'mmHg', 'http://unitsofmeasure.org', 'mm[Hg]') elif data['Observation'][i]['code'] == '8462-4': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '8462-4', display='Diastolic blood pressure') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'mmHg', 'http://unitsofmeasure.org', 'mm[Hg]') elif data['Observation'][i]['code'] == '9279-1': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '9279-1', display='Respiratory rate') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'breaths/minute', 'http://unitsofmeasure.org', '{Breaths}/min') elif data['Observation'][i]['code'] == '29463-7': coding = ET.SubElement(code, 'coding') dt.coding_type(coding, 'http://loinc.org', '29463-7', display='Body weight') valueQuantity = ET.SubElement(root, 'valueQuantity') dt.quantity_type( valueQuantity, data['Observation'][i]['valueQuantity'], 'kg', 'http://unitsofmeasure.org', 'kg') text = ET.tostring(root, encoding="us-ascii", method="xml", xml_declaration=None, default_namespace=None, short_empty_elements=True) post_req = requests.post( "http://hapi.fhir.org/baseR4/Observation/", headers={'Content-type': 'application/xml'}, data=text.decode('utf-8'))
def runCactusBlastOnly(options): with Toil(options) as toil: importSingularityImage(options) #Run the workflow if options.restart: alignmentID = toil.restart() else: options.cactusDir = getTempDirectory() # apply path overrides. this was necessary for wdl which doesn't take kindly to # text files of local paths (ie seqfile). one way to fix would be to add support # for s3 paths and force wdl to use it. a better way would be a more fundamental # interface shift away from files of paths throughout all of cactus if options.pathOverrides: seqFile = SeqFile(options.seqFile) configNode = ET.parse(options.configFile).getroot() config = ConfigWrapper(configNode) tree = MultiCactusTree(seqFile.tree) tree.nameUnlabeledInternalNodes( prefix=config.getDefaultInternalNodePrefix()) for name, override in zip(options.pathOverrideNames, options.pathOverrides): seqFile.pathMap[name] = override override_seq = os.path.join(options.cactusDir, 'seqFile.override') with open(override_seq, 'w') as out_sf: out_sf.write(str(seqFile)) options.seqFile = override_seq #to be consistent with all-in-one cactus, we make sure the project #isn't limiting itself to the subtree (todo: parameterize so root can #be passed through from prepare to blast/align) proj_options = copy.deepcopy(options) proj_options.root = None #Create the progressive cactus project (as we do in runCactusProgressive) projWrapper = ProjectWrapper(proj_options, proj_options.configFile, ignoreSeqPaths=options.root) projWrapper.writeXml() pjPath = os.path.join( options.cactusDir, ProjectWrapper.alignmentDirName, '%s_project.xml' % ProjectWrapper.alignmentDirName) assert os.path.exists(pjPath) project = MultiCactusProject() if not os.path.isdir(options.cactusDir): os.makedirs(options.cactusDir) project.readXML(pjPath) # open up the experiment (as we do in ProgressiveUp.run) # note that we copy the path into the options here experimentFile = project.expMap[options.root] expXml = ET.parse(experimentFile).getroot() logger.info("Experiment {}".format(ET.tostring(expXml))) experiment = ExperimentWrapper(expXml) configPath = experiment.getConfigPath() configXml = ET.parse(configPath).getroot() seqIDMap = dict() tree = MultiCactusTree(experiment.getTree()).extractSubTree( options.root) leaves = tree.getChildNames(tree.getRootName()) outgroups = experiment.getOutgroupGenomes() genome_set = set(leaves + outgroups) logger.info("Genomes in blastonly, {}: {}".format( options.root, list(genome_set))) print(str(project.inputSequenceMap)) #import the sequences (that we need to align for the given event, ie leaves and outgroups) for genome, seq in list(project.inputSequenceMap.items()): if genome in genome_set: if os.path.isdir(seq): tmpSeq = getTempFile() catFiles([ os.path.join(seq, subSeq) for subSeq in os.listdir(seq) ], tmpSeq) seq = tmpSeq seq = makeURL(seq) project.inputSequenceIDMap[genome] = toil.importFile(seq) else: # out-of-scope sequences will only cause trouble later on del project.inputSequenceMap[genome] #import cactus config if options.configFile: cactusConfigID = toil.importFile(makeURL(options.configFile)) else: cactusConfigID = toil.importFile( makeURL(project.getConfigPath())) project.setConfigID(cactusConfigID) project.syncToFileStore(toil) configNode = ET.parse(project.getConfigPath()).getroot() configWrapper = ConfigWrapper(configNode) configWrapper.substituteAllPredefinedConstantsWithLiterals() workFlowArgs = CactusWorkflowArguments( options, experimentFile=experimentFile, configNode=configNode, seqIDMap=project.inputSequenceIDMap) outWorkFlowArgs = toil.start( CactusTrimmingBlastPhase(standAlone=True, cactusWorkflowArguments=workFlowArgs, phaseName="trimBlast")) # export the alignments toil.exportFile(outWorkFlowArgs.alignmentsID, makeURL(options.outputFile)) # optional secondary alignments if outWorkFlowArgs.secondaryAlignmentsID: toil.exportFile(outWorkFlowArgs.secondaryAlignmentsID, makeURL(options.outputFile) + '.secondary') # outgroup fragments and coverage are necessary for cactus-align, as the sequence names got changed in the above alignemnts for i, outgroupFragmentID in enumerate( outWorkFlowArgs.outgroupFragmentIDs): toil.exportFile( outgroupFragmentID, makeURL(options.outputFile) + '.og_fragment_{}'.format(i)) # cactus-align can recompute coverage on the fly, but we save them because we have them for i, ingroupCoverageID in enumerate( outWorkFlowArgs.ingroupCoverageIDs): toil.exportFile( ingroupCoverageID, makeURL(options.outputFile) + '.ig_coverage_{}'.format(i))
def granted_dbs(self): req = {} res = self.__request('GrantedDBs', 'main', req) return xmltodict.parse(et.tostring(res))['qdbapi']
def ReadGroudTruth(filepath, foldername, outLoction): # create the file structure data = ET.Element('annotation') folder = ET.SubElement(data, 'folder') folder.text = 'windows_v1.8.0' filename = ET.SubElement(data, 'filename') path = ET.SubElement(data, 'path') source = ET.SubElement(data, 'source') database = ET.SubElement(source, 'database') database.text = 'Unknown' filelines = [] # parse an xml file by name mydoc = minidom.parse(filepath) # document common info # size size = ET.SubElement(data, 'size') width = ET.SubElement(size, 'width') root = items = mydoc.getElementsByTagName('document')[0] # print(root.attributes['width'].value) width.text = root.attributes['width'].value height = ET.SubElement(size, 'height') height.text = root.attributes['height'].value # fixed deep to 3 depth = ET.SubElement(size, 'depth') depth.text = '3' # fixed segmented to 0 segmented = ET.SubElement(data, 'segmented') segmented.text = '0' items = mydoc.getElementsByTagName('textBlock') # one specific item attribute print('Item #1 attribute:') print(items[0].attributes['height'].value) # all item attributes print('\nAll attributes:') for elem in items: print(elem.attributes['height'].value) pragraphs = elem.getElementsByTagName('paragraph') blockwidth = elem.attributes['width'].value blockheight = elem.attributes['height'].value blockx = int(elem.attributes['x'].value) blocky = int(elem.attributes['y'].value) for pragraph in pragraphs: strings = pragraph.getElementsByTagName('string') for s in strings: chars = s.getElementsByTagName('char') lines = [] # Luu thong tin tat ca cac dong moi dong la tap hop cac diem # tach tat ca cac line line = [] #Thong tin 1 dong prveviosx = -1 for celem in chars: if int(celem.attributes['x'].value) < prveviosx: prveviosx = -1 lines.append(line) line = [celem] else: line.append(celem) prveviosx = int(celem.attributes['x'].value) lines.append(line) for line in lines: words = [] # luu thong tin tat ca cac words word = [] # luu thong tin 1 word for celem in line: if celem.attributes['display'].value is ' ': words.append(word) word = [] else: word.append(celem) words.append(word) for word in words: if len(word)>0: displayvalues = [celem.attributes['display'].value for celem in word] xvalues = [blockx + int(celem.attributes['x'].value) for celem in word] yvalues = [blocky + int(celem.attributes['y'].value) for celem in word] xmaxvalues = [ blockx + int(celem.attributes['x'].value) + int(celem.attributes['width'].value) for celem in word] ymaxvalues = [ blocky + int(celem.attributes['y'].value) + int(celem.attributes['height'].value) for celem in word] # each char add 1 element objectele = ET.SubElement(data, 'object') name = ET.SubElement(objectele, 'name') name.text = convert(displayvalues) pose = ET.SubElement(objectele, 'pose') pose.text = 'Unspecified' truncated = ET.SubElement(objectele, 'truncated') truncated.text = '0' difficult = ET.SubElement(objectele, 'difficult') difficult.text = '0' bndbox = ET.SubElement(objectele, 'bndbox') xmin = ET.SubElement(bndbox, 'xmin') xmin.text = str(min(xvalues)) ymin = ET.SubElement(bndbox, 'ymin') ymin.text = str(min(yvalues)) xmax = ET.SubElement(bndbox, 'xmax') xmax.text = str(max(xmaxvalues)) ymax = ET.SubElement(bndbox, 'ymax') ymax.text = str(max(ymaxvalues)) content = xmin.text + ',' + ymin.text + ',' + xmax.text + "," + ymin.text + "," + xmax.text + "," + ymax.text + "," + xmin.text + "," + ymax.text + "," + name.text filelines.append(content) # item1.set('name','item1') # create a new XML file with the results # for celem in chars: # # character # print(celem.attributes['display'].value) # chardisplay = celem.attributes['display'].value # charx = celem.attributes['x'].value # chary = celem.attributes['y'].value # charwidth = celem.attributes['width'].value # charheight = celem.attributes['height'].value # # each char add 1 element # objectele = ET.SubElement(data, 'object') # name = ET.SubElement(objectele, 'name') # name.text = chardisplay # pose = ET.SubElement(objectele, 'pose') # pose.text = 'Unspecified' # truncated = ET.SubElement(objectele, 'truncated') # truncated.text = '0' # difficult = ET.SubElement(objectele, 'difficult') # difficult.text = '0' # bndbox = ET.SubElement(objectele, 'bndbox') # xmin = ET.SubElement(bndbox, 'xmin') # xmin.text = str(int(blockx) + int(charx)) # ymin = ET.SubElement(bndbox, 'ymin') # ymin.text = str(int(blocky) + int(chary)) # xmax = ET.SubElement(bndbox, 'xmax') # xmax.text = str(int(xmin.text) + int(charwidth)) # ymax = ET.SubElement(bndbox, 'ymax') # ymax.text = str(int(ymin.text) + int(charheight)) # # item1.set('name','item1') # # create a new XML file with the results mydata = ET.tostring(data) myfile = open(outLoction + "/" + foldername + ".xml", "wb") myfile.write(mydata) with open(outLoction + "/" + foldername + '.txt', 'w') as f: for item in filelines: f.write("%s\n" % item) # one specific item's data print('\nItem #1 data:') print(items[0].firstChild.data) print(items[0].childNodes[0].data) # all items data` print('\nAll item data:') for elem in items: print(elem.firstChild.data)
def check_theme(self, view): # Get some settings self.settings = sublime.load_settings( 'Dart - Plugin Settings.sublime-settings') error_color = self.settings.get('dart_linter_underline_color_error') warn_color = self.settings.get('dart_linter_underline_color_warning') info_color = self.settings.get('dart_linter_underline_color_info') error_icon = self.settings.get('dart_linter_gutter_icon_error') warn_icon = self.settings.get('dart_linter_gutter_icon_warning') info_icon = self.settings.get('dart_linter_gutter_icon_info') # Set the icons and colors in the file scope GUTTER_Icon.update({ 'dartlint_ERROR': error_icon, 'dartlint_WARNING': warn_icon, 'dartlint_INFO': info_icon }) ULINE_Color.update({ 'dartlint.mark.error': error_color, 'dartlint.mark.warning': warn_color, 'dartlint.mark.info': info_color, 'dartlint.mark.gutter': 'not used' }) # Get the current theme system_prefs = sublime.load_settings('Preferences.sublime-settings') theme = system_prefs.get('color_scheme') theme_xml = sublime.load_resource(theme) append_xml = False append_scopes = [] # Check for required scopes for scopes in SCOPES_Dartlint: if theme_xml.find(scopes) is -1: # append to xml append_xml = True append_scopes.append(scopes) print('%s not found in theme' % scopes) plist = ElementTree.XML(theme_xml) styles = plist.find('./dict/array') # Add missing elements if append_xml: for s2append in append_scopes: styles.append( ElementTree.fromstring( SCOPES_Dartlint[s2append]['style'].format( ULINE_Color[s2append]))) else: # No need to do anything return # write back to 'Packages/User/<theme> DL.tmTheme' original_name = os.path.splitext(os.path.basename(theme))[0] new_name = original_name + ' DL' theme_path = os.path.join(sublime.packages_path(), 'User', new_name + '.tmTheme') with open(theme_path, 'w', encoding='utf8') as f: f.write(THEME_Head.format('UTF-8')) f.write(ElementTree.tostring(plist, encoding='unicode')) # Set the amended color scheme to the current color scheme path = os.path.join('User', os.path.basename(theme_path)) prep_path = FormRelativePath(path) if prep_path is not False: system_prefs.set('color_scheme', prep_path) sublime.save_settings('Preferences.sublime-settings') print('Created: %s' % prep_path)
def switch_req(self, site_content_url): xml_request = ET.Element('tsRequest') site_element = ET.SubElement(xml_request, 'site') site_element.attrib['contentUrl'] = site_content_url return ET.tostring(xml_request)
root = tree.getroot() for i, p in enumerate(root): url = p.attrib['url'] load_contents(url) time.sleep(load_post_tout / 1000.0) sock = socket.socket() sock.connect(("localhost", port)) send_str( sock, "document.getElementsByClassName('reply_button')[0].click()\n") json_response = recv_str(sock) time.sleep(load_reply_tout / 1000.0) sock.close() response = get_contents() marks = MarksCollector.get_all(response) ind = elem_index(marks, lambda x: is_open_tag(x, 'a', 'mailapp')) if ind == -1: print("WARNING: no E-mail for " + url) continue mail_address = marks[ind + 1].get_data() p.attrib['email'] = mail_address print(mail_address) with open(dest_xml, "w") as f: f.write(minidom.parseString(ET.tostring(root)).toprettyxml())
def clone_instance(self, clone_data): clone_dev_path = [] xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) tree = ElementTree.fromstring(xml) name = tree.find('name') name.text = clone_data['name'] uuid = tree.find('uuid') tree.remove(uuid) for num, net in enumerate(tree.findall('devices/interface')): elm = net.find('mac') mac_address = self.fix_mac(clone_data['clone-net-mac-' + str(num)]) elm.set('address', mac_address) for disk in tree.findall('devices/disk'): if disk.get('device') == 'disk': elm = disk.find('target') device_name = elm.get('dev') if device_name: target_file = clone_data['disk-' + device_name] try: meta_prealloc = clone_data['meta-' + device_name] except: meta_prealloc = False elm.set('dev', device_name) elm = disk.find('source') source_file = elm.get('file') if source_file: clone_dev_path.append(source_file) clone_path = os.path.join(os.path.dirname(source_file), target_file) elm.set('file', clone_path) vol = self.get_volume_by_path(source_file) vol_format = util.get_xml_path( vol.XMLDesc(0), "/volume/target/format/@type") if vol_format == 'qcow2' and meta_prealloc: meta_prealloc = True vol_clone_xml = """ <volume> <name>%s</name> <capacity>0</capacity> <allocation>0</allocation> <target> <format type='%s'/> </target> <permissions> <owner>%s</owner> <group>%s</group> <mode>0644</mode> <label>virt_image_t</label> </permissions> <compat>1.1</compat> <features> <lazy_refcounts/> </features> </volume>""" % ( target_file, vol_format, owner['uid'], owner['guid']) stg = vol.storagePoolLookupByVolume() stg.createXMLFrom(vol_clone_xml, vol, meta_prealloc) source_protocol = elm.get('protocol') if source_protocol == 'rbd': source_name = elm.get('name') clone_name = "%s/%s" % (os.path.dirname(source_name), target_file) elm.set('name', clone_name) vol = self.get_volume_by_path(source_name) vol_format = util.get_xml_path( vol.XMLDesc(0), "/volume/target/format/@type") vol_clone_xml = """ <volume type='network'> <name>%s</name> <capacity>0</capacity> <allocation>0</allocation> <target> <format type='%s'/> </target> </volume>""" % (target_file, vol_format) stg = vol.storagePoolLookupByVolume() stg.createXMLFrom(vol_clone_xml, vol, meta_prealloc) source_dev = elm.get('dev') if source_dev: clone_path = os.path.join(os.path.dirname(source_dev), target_file) elm.set('dev', clone_path) vol = self.get_volume_by_path(source_dev) stg = vol.storagePoolLookupByVolume() vol_name = util.get_xml_path(vol.XMLDesc(0), "/volume/name") pool_name = util.get_xml_path(stg.XMLDesc(0), "/pool/name") storage = self.get_wvmStorage(pool_name) storage.clone_volume(vol_name, target_file) options = { 'title': clone_data.get('clone-title', ''), 'description': clone_data.get('clone-description', ''), } self._set_options(tree, options) self._defineXML(ElementTree.tostring(tree)) return self.get_instance(clone_data['name']).UUIDString()
def add_user_to_alert(self, alert_item, user_id): xml_request = ET.Element('tsRequest') user_element = ET.SubElement(xml_request, 'user') user_element.attrib['id'] = user_id return ET.tostring(xml_request)
def to_str(self): return et.tostring(self._element, encoding="unicode")
def wrapper(self, *args, **kwargs): xml_request = ET.Element('tsRequest') func(self, xml_request, *args, **kwargs) return ET.tostring(xml_request)