def _from_xml(self: etree.Element, type_: type): attrs: Dict[str, str] = getattr(type_, "_attributes", None) elems: Dict[str, str] = getattr(type_, "_elements", None) _fields = {} for field in fields(type_): attr_value = None if not attrs is None and field.name in attrs: attr_value = self.get(attrs.get(field.name), None) elif not elems is None and field.name in elems: attr_value = self.find(elems.get(field.name)) else: attr_value = self.find(field.name) if not attr_value is None: if field.type in TO_STR and etree.iselement(attr_value): attr_value = field.type(attr_value.text) elif etree.iselement(attr_value): attr_value = _from_xml(attr_value, field.type) if attr_value is None: _fields[field.name] = (field.default_factory() if not field.default_factory is None else field.default) else: _fields[field.name] = attr_value return type_(None, **_fields)
def reset_attributes(xml_attributes, xml_element=None): r""" replaces all @xml_attributes dict() items by new RADXMLAttribute attributes in a new separate dict() object; /!\ keeps @xml_attributes dict() UNTOUCHED /!\ supports automagic ET.Element attributes handling if an XML element is passed along @xml_attributes param; raises TypeError if @xml_attributes is definitely *NOT* of 'dict' type, even after support handling; @xml_element param is the official XML parent element of attrs; returns dict() of new RADXMLAttribute attributes on success; returns None on failure; """ # param controls - XML element attributes support if ET.iselement(xml_attributes): # should supply an XML parent element? if not ET.iselement(xml_element): xml_element = xml_attributes # end if # reset to attributes dict() xml_attributes = xml_attributes.attrib # end if # got a dict() object? if isinstance(xml_attributes, dict): _attrs = xml_attributes.copy() for (_name, _value) in _attrs.items(): if not isinstance(_value, RADXMLAttribute): _attrs[_name] = RADXMLAttribute(xml_element, _name, _value) # end if # end for return _attrs else: raise TypeError("parameter @xml_attributes must be of 'dict' type.") return None
def Element(tag, attrib={}, *children, **extra): """ Element(tag (,attrdict)? (,subelement|string)* ) -> Element """ if isinstance(attrib, dict): attrib = attrib.copy() else: children = (attrib, ) + children attrib = {} attrib.update(extra) element = basefactory(tag, attrib) prevelem = None for arg in children: if ElementTree.iselement(arg): element.append(arg) prevelem = arg else: if isinstance(arg, basestring): if prevelem is None: element.text = (element.text or '') + arg else: prevelem.tail = (prevelem.tail or '') + arg else: try: it = iter(arg) except: raise TypeError, "argument type to Element" for item in it: if not ElementTree.iselement(item): raise TypeError, "invalid argument type to Element" element.append(item) return element
def Element(tag, attrib={}, *children, **extra): """ Element(tag (,attrdict)? (,subelement|string)* ) -> Element """ if isinstance(attrib, dict): attrib = attrib.copy() else: children = (attrib,) + children attrib = {} attrib.update(extra) element = basefactory(tag, attrib) prevelem = None for arg in children: if ElementTree.iselement(arg): element.append(arg) prevelem = arg else: if isinstance(arg, basestring): if prevelem is None: element.text = (element.text or '') + arg else: prevelem.tail = (prevelem.tail or '') + arg else: try: it = iter(arg) except: raise TypeError, "argument type to Element" for item in it: if not ElementTree.iselement(item): raise TypeError, "invalid argument type to Element" element.append(item) return element
def check_start_table_row_ref(self, idx): if idx <= self.t_ref_table_checked_idx: return False new_entry_ok = True if self.t_ref_table_extra_or_col >= 0 and self.cur_table_row is not None: tr = self.cur_table_row l = VG.get_content(tr[self.t_ref_table_extra_or_col]).split('\n') if len(l) > 1 and 'or' == l[-2].strip(): # advance past this line, will never return true new_entry_ok = False new_entry = False while True: token, stuff, pos = self.t_data[idx] ty = MAP_TY[token] if ty in (TY_EOL, TY_EOF): break; if new_entry_ok: if (ET.iselement(stuff) and stuff.tag == 'anchor' and stuff.get('t') == 'star'): new_entry = True elif (pos == self.globalish_cpos[0] and self.globalish_after_blank_line and (ET.iselement(stuff) or len(stuff) > 0 and not stuff[0].isspace())): new_entry = True idx += 1 self.t_ref_table_checked_idx = idx if not new_entry: return False return True
def check_personal_awards(): """ Check for personal awards """ for person in campaign.find("personnel"): person_id = person.find("id").text person_name = get_pilot_name(person_id) if len(person_name) == 0: continue if ETree.iselement(person.find("prisonerStatus")): if debug: print(person_name + " is a prisoner") continue if person.find("status").text != "ACTIVE": if debug: print(person_name + " is " + person.find("status").text) continue if debug: print(person_name + " is a full member") if not ETree.iselement(person.find("personnelLog")): print("No personnelLog found for " + person_name) continue check_personnel_log(person)
def get_network_info(self, network, newNetwork, _ElementTree, ssid): for bssid in network.findall('BSSID'): bssid = bssid.text.strip() newNetwork.add_BSSID(bssid) if ssid != UNKNOWN_SSID_NAME: self.BSSIDToSSIDMap[bssid] = ssid else: self.BSSIDToSSIDMap[bssid] = bssid self.OrphanedBSSIDs.append(bssid) eaptypes = network.find('SSID').find('eap-types') if ElementTree.iselement(eaptypes): for eaptype in eaptypes.text.strip().split(','): if eaptype.isdigit(): newNetwork.addEapType(int(eaptype)) expandedVendorIDs = network.find('SSID').find('expanded-vendor-ids') if ElementTree.iselement(expandedVendorIDs): for vendorid in expandedVendorIDs.text.strip().split(','): if vendorid.isdigit(): newNetwork.add_expanded_vendor_id(int(vendorid)) wpsXMLData = network.find('wps-data') if ElementTree.iselement(wpsXMLData): wpsData = wpsDataHolder() for elem in wpsXMLData: key = elem.tag.replace('-', ' ') value = elem.text.strip() encoding = elem.get('encoding') if encoding == 'hex': wpsData[key] = binascii.a2b_hex(value) elif encoding == 'base64': wpsData[key] = base64.standard_b64decode(value) else: wpsData[key] = value if len(wpsData): newNetwork.wpsData = wpsData
def get_client_info(self, client, newClient, _ElementTree): eaptypes = client.find('eap-types') if ElementTree.iselement(eaptypes): eaptypes = eaptypes.text if eaptypes != None: for eaptype in eaptypes.strip().split(','): if eaptype.isdigit(): newClient.addEapType(int(eaptype)) identities = client.findall('identity') or [] for identity in identities: tmp = identity.get('eap-type') if tmp.isdigit(): newClient.add_identity(int(tmp), identity.text.strip()) mschaps = client.findall('mschap') or [] for mschap in mschaps: newClient.add_ms_chap_info( int(mschap.get('eap-type')), binascii.a2b_hex(mschap.find('challenge').text.strip().replace(':', '')), binascii.a2b_hex(mschap.find('response').text.strip().replace(':', '')), mschap.get('identity') ) wpsXMLData = client.find('wps-data') if ElementTree.iselement(wpsXMLData): wpsData = wpsDataHolder() for elem in wpsXMLData: key = elem.tag.replace('-', ' ') value = elem.text.strip() if elem.get('encoding') == 'hex': wpsData[key] = binascii.a2b_hex(value) elif elem.get('encoding') == 'base64': wpsData[key] = base64.standard_b64decode(value) else: wpsData[key] = value if len(wpsData): newClient.wpsData = wpsData
def parse_replicas_metalink(root): """ Transforms the metalink tree into a list of dictionaries where each dictionary describes a file with its replicas. Will be called by parse_replicas_from_file and parse_replicas_from_string. :param root: root node of the metalink tree :returns: a list with a dictionary for each file """ files = [] # metalink namespace ns = '{urn:ietf:params:xml:ns:metalink}' str_to_bool = {'true': True, 'True': True, 'false': False, 'False': False} # loop over all <file> tags of the metalink string for file_tag_obj in root.findall(ns + 'file'): # search for identity-tag identity_tag_obj = file_tag_obj.find(ns + 'identity') if not ElementTree.iselement(identity_tag_obj): raise InputValidationError( 'Failed to locate identity-tag inside %s' % ElementTree.tostring(file_tag_obj)) cur_file = { 'did': identity_tag_obj.text, 'adler32': None, 'md5': None, 'sources': [] } parent_dids = set() parent_dids_tag_obj = file_tag_obj.find(ns + 'parents') if ElementTree.iselement(parent_dids_tag_obj): for did_tag_obj in parent_dids_tag_obj.findall(ns + 'did'): parent_dids.add(did_tag_obj.text) cur_file['parent_dids'] = parent_dids size_tag_obj = file_tag_obj.find(ns + 'size') cur_file['bytes'] = int( size_tag_obj.text) if ElementTree.iselement(size_tag_obj) else None for hash_tag_obj in file_tag_obj.findall(ns + 'hash'): hash_type = hash_tag_obj.get('type') if hash_type: cur_file[hash_type] = hash_tag_obj.text for url_tag_obj in file_tag_obj.findall(ns + 'url'): key_rename_map = {'location': 'rse'} src = {} for k, v in url_tag_obj.items(): k = key_rename_map.get(k, k) src[k] = str_to_bool.get(v, v) src['pfn'] = url_tag_obj.text cur_file['sources'].append(src) files.append(cur_file) return files
def parse_xml(self): #this exception catches file and input anomalies try: tree = ET.parse(self.csv.path) root = tree.getroot() cp = root.find('.//CedentePrestatore') den = cp.find('.//Denominazione') if ET.iselement( den) and den.text == 'Associazione Professionale Perilli': active = True cc = root.find('.//CessionarioCommittente') if ET.iselement(cc.find('.//Denominazione')): client = cc.find('.//Denominazione').text else: client = (cc.find('.//Nome').text + ' ' + cc.find('.//Cognome').text) else: active = False if ET.iselement(den): client = den.text else: client = (cp.find('.//Nome').text + ' ' + cp.find('.//Cognome').text) category = self.guess_passive_category(client) date = root.find('.//Data').text number = root.find('.//Numero').text security = 0 for sec in root.findall('.//ImportoContributoCassa'): security += float(sec.text) descr = '' for dsc in root.findall('.//Descrizione'): descr += dsc.text + '\n' if active: category = self.guess_active_category(descr) amount = 0 for amnt in root.findall('.//PrezzoTotale'): amount += float(amnt.text) vat = root.find('.//Imposta').text obj, created = Invoice.objects.update_or_create( number=number, date=datetime.strptime(date, '%Y-%m-%d'), defaults={ 'client': client, 'active': active, 'descr': descr, 'amount': amount, 'security': security, 'vat': float(vat), 'category': category, 'paid': False }) if created: self.created += 1 else: self.modified += 1 except: self.failed += 1 super(CSVInvoice, self).save(update_fields=['created', 'modified', 'failed'])
def GET(self, resource_id, metadata_name): web.header('Content-Type', 'text/xml'); print('get_resource_id_metadata_by_name'); print "getting resourceID="+str(resource_id)+" metadata="+str(metadata_name)+'content_type='+str(http_content_type); for child in root: if child.attrib['resourceid'] == str(resource_id): resource = child; common_metadata = resource.find('CommonMetadata'); specific_metadata = resource.find('SpecificMetadata'); resource_type = str(common_metadata.find('Type').text).strip(); # Resource specific metadata to restrict search in case of redundant # tag/element names resource_specific_metadata = specific_metadata.find(resource_type); print('resource_specific_metadata='+str(ET.tostring(resource_specific_metadata, 'utf-8', method='xml'))); if resource_type == resource_hypermodel: print("switch case\n") ; elif resource_type == resource_hypermodel: print("switch case\n"+resource_hypermodel) ; elif resource_type == resource_dataset: print("switch case\n"+resource_dataset) ; print('resource_type='+resource_type); try: xml_element_specific = resource_specific_metadata.find(metadata_name); xml_element_common = common_metadata.find(metadata_name); if ET.iselement(xml_element_common): output = ET.tostring(xml_element_common, 'utf-8', method='xml') xml_element = xml_element_common elif ET.iselement(xml_element_specific): output = ET.tostring(xml_element_specific, 'utf-8', method='xml') xml_element =xml_element_specific; print 'output='+str(output); if http_content_type != 'xml': output == xml_element.text; #description = common_meta_data.find('Description'); #output = ET.tostring(description, 'utf-8', method='xml') #output = description.text; return output except: print('exception raised') app.notfound(); return; app.notfound();
def addContent(parent, content): if ET.iselement(parent): if ET.iselement(content): parent.append(content) else: parent.text = content else: if ET.iselement(content): parent.text = ET.tostring(content) else: parent.text = content
def findsbj(vp, root): sbj = vp.find(query_s) if ET.iselement(sbj): head = findhead(sbj) if ET.iselement(head): return head index = sbj.get('index') if index: coindices = root.findall('.//node[@index="' + index + '"]') for node in coindices: result = findhead(node) if ET.iselement(result): return result return None
def update_xml_element (self, value=None): r""" updates inner XML element's attr name along @value param; returns True on success, False otherwise; """ # param inits if value is None: value = self.value # end if if ET.iselement(self.xml_element): # update XML element self.xml_element.set(self.name, value) # succeeded return True else: raise TypeError( "XML element must be of " "type '{obj_type}', not '{cur_type}'." .format( obj_type=repr(ET.Element), cur_type=repr(self.xml_element), ) ) # end if # failed return False
def test_parse_file_object(): html = "<html><body></body></html>" fileobj = io.StringIO(html) root = htmlement.parse(fileobj, encoding="utf8") assert Etree.iselement(root) assert root.tag == "html" assert root[0].tag == "body"
def test_fromstringlist(): # Check that I can parse a simple tree sequence = ["<html><body>", "</body></html>"] root = htmlement.fromstringlist(sequence) assert Etree.iselement(root) assert root.tag == "html" assert root[0].tag == "body"
def test_fromstring(): # Check that I can parse a simple tree html = "<html><body></body></html>" root = htmlement.fromstring(html) assert Etree.iselement(root) assert root.tag == "html" assert root[0].tag == "body"
def main(): print "Before Post Parse ETree" for root, dirs, files in os.walk(POSTS_PATH): for one_file in files: populatePostDictionary(one_file, root) print "Before User Parse ETree" userTree = ET.parse('/mnt/stackoverflow/data/users/stackoverflow.com-Users.xml') print "Before User Processing Dictionary" processUserFile(userTree) f2 = open ('/mnt/stackoverflow/data/output/targetDocUserDataDone.txt', 'a') f2.write("UserDataDictionaryDone") f2.close() #done all processing of parents for root, dirs, files in os.walk(POSTS_PATH): for one_file in files: postTree = ET.parse(os.path.join(root,one_file)) postDocRoot= postTree.getroot() for child in postDocRoot: if ET.iselement(child): if PARENT_ID in child.attrib: processChild(child)
def makeLink(href, content, xargs={}, hreftag="href"): """Make <a href=...>contents</a> link tag""" xargs[hreftag] = href a = ET.Element('a', xargs) if ET.iselement(content): a.append(content) else: a.text = str(content) return a
def _tostring(self, elem): if not etree.iselement(elem): elem = elem.getroot() self._fixup_xmlns(elem) return etree.tostring(elem)
def to_string(obj): """Get string representation of an object, special-case for XML Element. Args: obj (object): Object to represent as a string. Returns: str: string representation Examples: :: >>> to_string("Hello") 'Hello' >>> to_string(27.5) '27.5' >>> e = ET.Element('hello', attrib={'key': 'value'}) >>> print(e) # doctest: +ELLIPSIS <Element ...hello... at ...> >>> print(to_string(e)) <hello key="value" /> """ if ET.iselement(obj): if sys.version_info[0] >= 3: return ET.tostring(obj, encoding='unicode') else: return ET.tostring(obj) else: return str(obj)
def findsbj(vp, root): """Find the subject of a VP, by looking through the entire tree.""" sbj = vp.find(query_s) #identify the verb's external argument if ET.iselement(sbj): head = findhead(sbj) #check if the subject is overt if ET.iselement(head): return head index = sbj.get( 'index') #if not, find an overt phrase with the same index if index: coindices = root.findall('.//node[@index="' + index + '"]') for node in coindices: result = findhead(node) if ET.iselement(result): return result return None
def __call__(self, tag, *children, **attrib): get = self._typemap.get if self._namespace is not None and tag[0] != '{': tag = self._namespace + tag if self._nsmap: elem = self._makeelement(tag, nsmap=self._nsmap) else: elem = self._makeelement(tag) if attrib: get(dict)(elem, attrib) for item in children: if callable(item): item = item() t = get(type(item)) if t is None: if ET.iselement(item): elem.append(item) continue raise TypeError("bad argument type: %r" % item) else: v = t(elem, item) if v: get(type(v))(elem, v) return elem
def read_isce_xml(fname, standardize=True): """Read ISCE .xml file into a python dict structure.""" root = ET.parse(fname).getroot() xmlDict = {} # imageFile, e.g. filt_fine.unw.xml if root.tag.startswith('image'): for child in root.findall('property'): key = child.get('name') value = child.find('value').text xmlDict[key] = value # Read lat/lon info for geocoded file # in form: root/component coordinate*/property name/value for coord_name, prefix in zip(['coordinate1', 'coordinate2'], ['X', 'Y']): child = root.find("./component[@name='{}']".format(coord_name)) if ET.iselement(child): v_step = float(child.find("./property[@name='delta']").find('value').text) v_first = float(child.find("./property[@name='startingvalue']").find('value').text) if abs(v_step) < 1. and abs(v_step) > 1e-7: xmlDict['{}_STEP'.format(prefix)] = v_step xmlDict['{}_FIRST'.format(prefix)] = v_first - v_step / 2. # PAMDataset, e.g. hgt.rdr.aux.xml elif root.tag == 'PAMDataset': meta = root.find("./Metadata[@domain='ENVI']") for child in meta.findall("MDI"): key = child.get('key') value = child.text xmlDict[key] = value xmlDict['data_type'] = ENVI2NUMPY_DATATYPE[xmlDict['data_type']] if standardize: xmlDict = standardize_metadata(xmlDict) return xmlDict
def post(self): blobXML = self.request.get('file') node = ET.fromstring(str(blobXML)) #Load the XML to test it. #Remove the namespace. for elem in node.getiterator(): if elem.tag[0] == '{': elem.tag = elem.tag.split('}', 1)[1] if ET.iselement(node) and node.findtext('.//PersonName/FormattedName'): m = resumeXML() #Test was ok. Create the Datastore object. m.blobXML = blobXML m.name = node.findtext('.//PersonName/FormattedName') m.id = node.findtext('.//ResumeId/IdValue') timestring = node.findtext('.//RevisionDate') time_format = "%Y-%m-%d" m.resumeDate = datetime.datetime.fromtimestamp( time.mktime(time.strptime(timestring, time_format))) photo = self.request.POST['photo'] if hasattr(photo, 'type'): #Test to see if a photo was uploaded. m.blobImage = photo.value m.blobImageContentType = photo.type m.put() #Put the object in the Datastore self.redirect('/resume2/%s' % m.key()) #Show the uploaded resume. else: #If something was wrong the person should get an error msg, #but I'll just redirect for now. self.redirect('/resume2')
def getDeliveryAddress(e): if ET.iselement(e) and e.tag == 'DeliveryAddress': DeliveryAddress = [] testStr = e.text #There has to be an easier way to test. if testStr and len(testStr.strip()): DeliveryAddress.append(testStr) for AddressLine in e.findall('AddressLine'): testStr = AddressLine.text if testStr and len(testStr.strip()): DeliveryAddress.append(testStr) if e.findtext('BuildingNumber') and e.findtext('StreetName'): AddressLine = [] AddressLine.append(e.findtext('BuildingNumber')) AddressLine.append(e.findtext('StreetName')) testStr = ' '.join(AddressLine) if testStr and len(testStr.strip()): DeliveryAddress.append(testStr) testStr = e.findtext('Unit') if testStr and len(testStr.strip()): DeliveryAddress.append('Unit ' + testStr) testStr = e.findtext('PostOfficeBox') if testStr: DeliveryAddress.append('P.O. Box ' + testStr) return DeliveryAddress return None
def handle_entity_prefix(tree, prefix): # Collect name of fields which are defined in this form. This is # used as we only want to handle prefixes on fieldname and # expression for fields which are defined in the form. fieldnames = set([f.get("name") for f in tree.findall(".//entity")]) def replace_fieldnames(m): # TODO: Handle % and @ variables to? (ti) <2015-12-17 09:30> v = m.group(1) if v in fieldnames: return "$" + prefix + v return m.group(0) for n in tree.iter(): if not ET.iselement(n): continue if n.tag == "entity": # Handle fields n.attrib["name"] = prefix + n.attrib["name"] elif n.tag in ("rule", "if"): # Handle rules and conditionals n.attrib["expr"] = _var_re.sub(replace_fieldnames, n.attrib["expr"]) return tree
def __format_attributes(elt_data): """ Private method which converts a single XML tag to a python dict. It also checks that the elt_data given as argument is of type xml.etree.ElementTree.Element :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element """ rval = {} if not ET.iselement(elt_data): raise NmapParserException("Error while trying to parse supplied " "data attributes: format is not XML or " "XML tag is empty") try: for dkey in elt_data.keys(): rval[dkey] = elt_data.get(dkey) if rval[dkey] is None: raise NmapParserException("Error while trying to build-up " "element attributes: empty " "attribute {0}".format(dkey)) except: raise return rval
def read_isce_xml(xmlfile): """Read ISCE XML file into dict. Add from PySAR/pysar/utils/readfile.py by Zhang Yunjun """ meta = {} root = ET.parse(xmlfile).getroot() if root.tag.startswith('image'): for child in root.findall('property'): key = child.get('name') value = child.find('value').text meta[key] = value # Read lat/lon info for geocoded file # in form: root/component coordinate*/property name/value for coord_name, prefix in zip(['coordinate1', 'coordinate2'], ['X', 'Y']): child = root.find("./component[@name='{}']".format(coord_name)) if ET.iselement(child): v_step = float(child.find("./property[@name='delta']").find('value').text) v_first = float(child.find("./property[@name='startingvalue']").find('value').text) if abs(v_step) < 1. and abs(v_step) > 1e-7: xmlDict['{}_STEP'.format(prefix)] = v_step xmlDict['{}_FIRST'.format(prefix)] = v_first - v_step / 2. # convert key name from isce to roipac isce2roipacKeyDict = { 'width':'WIDTH', 'length':'FILE_LENGTH', } for key,value in isce2roipacKeyDict.items(): meta[value] = meta[key] return meta
def open(self, string_or_file): if isinstance(string_or_file, basestring): string_or_file = ElementTree.fromstring(string_or_file) elif not ElementTree.iselement(string_or_file): string_or_file = ElementTree.parse(string_or_file) if string_or_file.getroot().tag == self._get_qname('gpx'): self._root = string_or_file.getroot()
def __call__(self, tag, *children, **attrib): get = self._typemap.get if self._namespace is not None and tag[0] != '{': tag = self._namespace + tag elem = self._makeelement(tag) if attrib: get(dict)(elem, attrib) for item in children: if callable(item): item = item() t = get(type(item)) if t is None: if ET.iselement(item): elem.append(item) continue for basetype in type(item).__mro__: # See if the typemap knows of any of this type's bases. t = get(basetype) if t is not None: break else: raise TypeError("bad argument type: %s(%r)" % (type(item).__name__, item)) v = t(elem, item) if v: get(type(v))(elem, v) return elem
def slot_write_description(self, index): for row in range(self.pagesModel.rowCount()): index = self.pagesModel.index(row, 1) indexUrl = self.pagesModel.index(row, 0) absoluteUrl = os.path.join( self.projecturl, str(self.pagesModel.data(indexUrl, role=CPE.URL))) page = zipfile.ZipFile(absoluteUrl, "a") xmlDoc = ET.ElementTree() ET.register_namespace("", "http://www.calligra.org/DTD/document-info") location = os.path.join(self.projecturl, "documentinfo.xml") xmlDoc.parse(location) xmlroot = ET.fromstring(page.read("documentinfo.xml")) calligra = "{http://www.calligra.org/DTD/document-info}" aboutelem = xmlroot.find(calligra + 'about') if ET.iselement(aboutelem.find(calligra + 'subject')): desc = aboutelem.find(calligra + 'subject') desc.text = self.pagesModel.data(index, role=Qt.EditRole) xmlstring = ET.tostring(xmlroot, encoding='unicode', method='xml', short_empty_elements=False) page.writestr(zinfo_or_arcname="documentinfo.xml", data=xmlstring) for document in Application.documents(): if str(document.fileName()) == str(absoluteUrl): document.setDocumentInfo(xmlstring) page.close()
def __format_element(elt_data): """ Private method which ensures that a XML portion to be parsed is of type xml.etree.ElementTree.Element. If elt_data is a string, then it is converted to an XML Element type. :param elt_data: XML Element to be parsed or string to be converted to a XML Element :return: Element """ if isinstance(elt_data, str): try: xelement = ET.fromstring(elt_data) except: raise NmapParserException("Error while trying " "to instanciate XML Element from " "string {0}".format(elt_data)) elif ET.iselement(elt_data): xelement = elt_data else: raise NmapParserException("Error while trying to parse supplied " "data: unsupported format") return xelement
def extractAttrByXpath(xpath, nameSpace, ele, tagAttr): nodes = ele.xpath(xpath, nameSpace=None) result = [] for node in nodes: if ET.iselement(node): result.append(node.attrib[tagAttr]) return result[0] if len(result) == 1 else result
def glsrequest(uri, method, data=None): ''' Returns xml node tree as Element instance. 'uri' may be absolute or relative to _BASEURI. 'method' in ('GET', 'POST', 'PUT') 'data' can be a string or Element instance ''' if method not in {'GET', 'POST', 'PUT'}: raise GlslibException(MSGUNSUPPORTEDMETHOD % method) if not uri.startswith(_BASEURI): uri = _BASEURI.rstrip('/') + '/' + uri.lstrip('/') request = urllib.request.Request(uri) request.add_header("Authorization", "Basic %s" % _AUTHSTR) if etree.iselement(data): # tostring generates bytestring (as required for data) data = etree.tostring(data) request.add_header('Content-Type', 'application/xml') request.add_data(data) request.get_method = lambda: method msg = '%s %s\n%s\n%s' % (request.get_method(), request.get_full_url(), request.headers, data.decode('utf-8') if data else '') logger.debug(msg) try: r = urllib.request.urlopen(request) return etree.XML(r.read()) except urllib.error.HTTPError as httperr: logger.error(httperr.read()) raise except urllib.error.URLError as urlerr: logger.error(request.get_full_url()) raise
def process(self): with open('data/links.txt', 'w', encoding='utf-8') as linkfile: linkfile.write('') link_queue = [] page_tag = to_tag('page') with open('data/enwiki-20200220-pages-articles-multistream.xml', "rb") as data: for event, elem in etree.iterparse(data, events=('end', )): if etree.iselement(elem) and elem.tag == page_tag: properties = self.parse_page(elem) with open('data/links.txt', 'a', encoding='utf-8') as linkfile: linkfile.write((properties['title'] + ',' + ','.join(properties['links']) + '\n')) yield self.default_transaction(properties, uuid=properties['uuid']) print('Writing links!', flush=True) with open('data/links.txt', 'r', encoding='utf-8') as linkfile: for line in linkfile: page_title, *links = line.split(',') for link in links: yield self.custom_transaction(uuid=link, from_uuid=page_title, connect_labels=('mentions', 'mentions'))
def parse(log_file = None): tree = ET.parse(log_file) root = tree.getroot() if root.tag != "log": print("Not Informatica log xml") exit() log_detail = ET.Element("logDetails") events = root.findall("logEvent") idx = 0 for idx in range(idx, len(events)): evt_thread = events[idx].get("threadName") evt_severity = events[idx].get("severity") response = process_thread_severity.get( (evt_thread, evt_severity), process_none)(events, idx) # response = process_severity.get(evt_severity, process_none)(event, idx, events) # if evt_severity == 1: # response = process_severity.get(evt_severity, process_none)(event, idx, events) # else: # response = None #response = None if ET.iselement(response): log_detail.append(response) return log_detail
def extractValueByXpath(xpath, nameSpace, ele): nodes = ele.xpath(xpath, nameSpace=None) result = '' for node in nodes: result = node.text if ET.iselement(node) else node break; return result
def makeLink(href, content, xargs = {}): xargs["href"] = href a = ET.Element('a', xargs) if ET.iselement(content): a.append(content) else: a.text = str(content) return a
def parse_element(root: Et): assert Et.iselement(root) if not is_container(root): return create_ui_element(root, None) children = [] for child in root: children.append(parse_element(child)) return create_ui_element(root, children)
def mergecontents(e, contents, prevel = None): """Recursively merge mixed text/tag contents into e""" if ET.iselement(contents): e.append(contents) prevel = contents elif isinstance(contents, tuple) or isinstance(contents, list): for c in contents: if ET.iselement(c): e.append(c) prevel = c elif c is not None: mergecontents(e, c, prevel) elif contents is not None: cs = str(contents) if prevel is not None: prevel.tail = prevel.tail + cs if prevel.tail else cs else: e.text = e.text + cs if e.text else cs
def compare_element(source, target): """ Utility method to recursively compare two etree elements :param source: The source element to compare against the target :type source: xml.etree.ElementTree.Element :param target: The target element to compare against the source :type target: xml.etree.ElementTree.Element :raise AssertionError: if the elements do not match """ if not ElementTree.iselement(source): raise AssertionError("Source is not an element") if not ElementTree.iselement(target): raise AssertionError("Target is not an element") if source.tag != target.tag: raise AssertionError("elements do not match. Tags are different %s != %s" % (source.tag, target.tag)) #test keys source_keys = set(source.keys()) target_keys = set(target.keys()) if source_keys != target_keys: raise AssertionError("elements do not match. Keys are different") for key in source_keys: if source.get(key) != target.get(key): raise AssertionError("Key values do not match. Value mismatch for key %s: %s != %s" % (key, source.get(key), target.get(key))) if source.text != target.text: raise AssertionError("elements do not match. Text is different\n%s\n%s" % (source.text, target.text)) #Use the deprecated getchildren method for python 2.6 support source_children = list(source.getchildren()) target_children = list(target.getchildren()) if len(source_children) != len(target_children): raise AssertionError("elements do not match. Unequal number of child elements") for source_child, target_child in zip(source_children, target_children): compare_element(source_child, target_child)
def TransferToWindow(self): textCtrl = self.GetWindow() if textCtrl.GetName() == "informationName": textCtrl.SetValue(self.xml.attrib["name"]) if textCtrl.GetName() == "informationDescription": xmlDesc = self.xml.find("{http://www.bitplant.de/template}parameter/{http://www.bitplant.de/template}description") if ET.iselement(xmlDesc): if xmlDesc.text: textCtrl.SetValue(xmlDesc.text) return True
def makeTree(struct): if struct == list(struct): name = struct[0] elem = etree.Element(name) for x in struct[1:]: elem.append(makeTree(x)) return elem elif struct == strcons_fix(struct): return etree.Element(struct) elif etree.iselement(struct): return struct
def isloadable(toload): """Check if toload is can be laoded into an xml node""" # Direct loading or file loading if toload is None or ET.iselement(toload) or isinstance(toload, file): return True if isinstance(toload, (str, unicode)): # *ml path file if os.path.exists(toload) and toload.endswith('ml'): return True # xml like string toload = toload.lower().strip(' \t\n') if toload.startswith('<') and toload.endswith('>'): return True # Not loadable return False
def processUserFile(userTree): userDocRoot = userTree.getroot() print "Inside User Processing Dictionary" for child in userDocRoot: if ET.iselement(child): GlobalUserDict[child.attrib["Id"]]["Rep"] = int(child.attrib["Reputation"]) GlobalUserDict[child.attrib["Id"]]["Views"] = int(child.attrib["Views"]) GlobalUserDict[child.attrib["Id"]]["CreationDate"] = child.attrib["CreationDate"][0:10] if (float(child.attrib["DownVotes"]) == 0): GlobalUserDict[child.attrib["Id"]]["UDRatio"] = int(child.attrib["UpVotes"]) else: GlobalUserDict[child.attrib["Id"]]["UDRatio"] = int(float(child.attrib["UpVotes"]) / float(child.attrib["DownVotes"]) )
def test__element2edge(): element2edge = cq._element2edge # Mapping with open('cocotools/tests/sample_map.xml') as xml: prim_e = etree.parse(xml).find('%sPrimaryRelation' % cq.P) nt.assert_true(etree.iselement(prim_e)) edge = ('B05-19', 'PP99-19', {'RC': 'X', 'PDC': 'X'}) nt.assert_equal(element2edge(prim_e, 'Mapping'), edge) # Connectivity with open('cocotools/tests/sample_con.xml') as xml: prim_e = etree.parse(xml).find('%sIntegratedPrimaryProjection' % cq.P) nt.assert_true(etree.iselement(prim_e)) edge = ('B05-2', 'PP99-9/46D', {'EC_Target': 'X', 'Degree': 'X', 'PDC_Site_Target': 'X', 'EC_Source': 'X', 'PDC_Density': 'X', 'PDC_EC_Target': 'X', 'PDC_Site_Source': 'X', 'PDC_EC_Source': 'X', 'Connection': 'Present'}) nt.assert_equal(element2edge(prim_e, 'Connectivity'), edge)
def description(self, xml): if self.tempItemData.has_key("informationDescription"): xmlDesc = xml.find( "{http://www.bitplant.de/template}parameter/{http://www.bitplant.de/template}description" ) if ET.iselement(xmlDesc): xmlDesc.text = self.tempItemData["informationDescription"] else: xmlDesc = ET.SubElement( xml.find("{http://www.bitplant.de/template}parameter"), "{http://www.bitplant.de/template}description", ) xmlDesc.text = self.tempItemData["informationDescription"]
def build_index(self): index = {} for node in self._tree.iter(): if not ET.iselement(node): continue elems = index.get(node.tag) if elems is None: elems = [] index[node.tag] = elems elems.append(node) self.index = index
def __init__(self, source=None): self.view_id = None self.options = {} # self.arguments will contain (name, value) tuples, in order # to preserve order. self.arguments = [] if ET.iselement(source): self.parse_element(source) elif hasattr(source, "read"): # File-like object self.parse_file(source) else: print "Do not know what to do with ", source