我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.etree.ElementTree.ElementTree()。
def write(self, filename): xml = ET.Element("phonebooks") for book in self.phonebookList: xml.append(book.getXML()) tree = ET.ElementTree(xml) if False: tree.write(filename, encoding="iso-8859-1", xml_declaration=True) else: rough_string = ET.tostring(tree.getroot(), encoding="iso-8859-1", method="xml") reparsed = parseString(rough_string) pretty = reparsed.toprettyxml(indent=" ", encoding="iso-8859-1").decode("iso-8859-1") with open(filename, 'w', encoding="iso-8859-1") as outfile: outfile.write(pretty) # sid: Login session ID # phonebookid: 0 for main phone book # 1 for next phone book in list, etc...
def writeXML(dataObj, fname, dtdPath): if dataObj.predSev == 0: sev = "ABSENT" elif dataObj.predSev == 1: sev = "MILD" elif dataObj.predSev == 2: sev = "MODERATE" elif dataObj.predSev == 3: sev = "SEVERE" root = ET.Element("RDoC") ET.SubElement(root, "TEXT").text = dataObj.text.content tags = ET.SubElement(root, "TAGS") pos_val = ET.SubElement(tags, "POSITIVE_VALENCE") pos_val.set('score', sev) pos_val.set('annotated_by', dataObj.Nannotators) tree = ET.ElementTree(root) tree.write(fname)
def print_output(self): """ Prints the dump of the xml object to the file specified. This function can be used for debugging purpose and its called at the end of the functional calls. :Arguments: resultfile = Result File :Returns: None """ try: import Framework.Utils.config_Utils as config_Utils resultfile = config_Utils.resultfile tree = ET.ElementTree(self.root) tree.write(resultfile) except UnicodeDecodeError as e: print_exception(e) except Exception as err: print_exception(err)
def getChildTextbyParentTag (datafile, pnode, cnode): """ Seraches XML file for the first parent. Finds the child node and returns its text datafile = xml file searched pnode = parent node cnode = child node """ value = False tree = ElementTree.parse(datafile) root = tree.getroot() node = root.find(pnode) if node is not None: child = node.find(cnode) if child is not None: value = child.text return value else: # print_info("could not find cnode under the given pnode") return value else: # print_info("could not find pnode in the provided file") return value
def getChildAttributebyParentTag (datafile, pnode, cnode, cattrib): """Find the attribute in child node by traversing through the parent node in the given file datafile = xml file searched pnode = parent node cnode = child node cattrob = child node attrib """ tree = ElementTree.parse(datafile) root = tree.getroot() node = root.find(pnode) if node is not None: child = node.find(cnode) if child is not None: value = child.get(cattrib) return value else: # print_info("could not find cnode under the given pnode") return False else: # print_info("could not find pnode in the provided file") return False
def getChildTextbyOtherChild (datafile, pnode, cnode, cvalue, rnode): """ Searches XML file for the parent node. Finds the 1st child node and checks its value if value is a match, then search for second child and return its value datafile = xml file searched pnode = parent node cnode = child node cvalue = child node value rnode = reference node or False if doesn't exist """ tree = ElementTree.parse(datafile) root = tree.getroot() rnodev = False for node in root.findall(pnode): value = node.find(cnode).text if value == cvalue: # print_debug("-D- rnode: '%s'" % rnode) if node.find(rnode) is not None: rnodev = node.find(rnode).text break return rnodev
def verifyParentandChildrenMatch (datafile, pnode, cnode, cvalue, rnode, rvalue): """ Searches XML file for the parent node. Finds the 1st child node and checks its value if value is a match, then search for second child and check if its value matches datafile = xml file searched pnode = parent node cnode = child node cvalue = child node value rnode = reference node rvalue = refernce node value """ tree = ElementTree.parse(datafile) root = tree.getroot() status = False for node in root.findall(pnode): value = node.find(cnode).text if value == cvalue: if node.find(rnode) is not None: cnodev = node.find(rnode).text # print_debug("-D- cnodev: '%s', rvalue : '%s'" % (cnodev, rvalue)) if cnodev == rvalue: # print_debug("-D- BREAK END METHOD verifyParentandChildrenMatch_Status '%s'" % status) return True return status
def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value): """ This method takes an xml document as input and finds all the sub elements (parent/children) containing specified tag and an attribute with the specified value. Returns a list of matching elements. Arguments: datafile = input xml file to be parsed. tag = tag value of the sub-element(parent/child) to be searched for. attrib = attribute name for the sub-element with above given tag should have. value = attribute value that the sub-element with above given tag, attribute should have. """ element_list = [] root = ElementTree.parse(datafile).getroot() for element in root.iterfind(".//%s[@%s='%s']" % (tag, attrib, value)): element_list.append(element) return element_list
def getElementWithTagAttribValueMatch(start, tag, attrib, value): """ When start is an xml datafile, it finds the root and first element with: tag, attrib, value. Or when it's an xml element, it finds the first child element with: tag, attrib, value. If there is not a match, it returns False. """ node = False if isinstance(start, (file, str)): # check if file exist here if file_Utils.fileExists(start): node = ElementTree.parse(start).getroot() else: print_warning('The file={0} is not found.'.format(start)) elif isinstance(start, ElementTree.Element): node = start if node is not False and node is not None: elementName = ".//%s[@%s='%s']" % (tag, attrib, value) element = node.find(elementName) else: element = node return element
def getElementListWithSpecificXpath(datafile, xpath): """ This method takes an xml document as input and finds all the sub elements (parent/children) containing specified xpath Returns a list of matching elements. Arguments: parent = parent element xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html """ element_list = [] root = ElementTree.parse(datafile).getroot() for element in root.iterfind(xpath): element_list.append(element) return element_list
def getConfigElementTextWithSpecificXpath(datafile, xpath): """ This method takes an xml document as input and finds the first sub element (parent/children) containing specified xpath which should be a filepath to a netconf config file Returns the element text attribute Arguments: parent = parent element xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html """ root = ElementTree.parse(datafile).getroot() elem1 = root.find(xpath).text elem2_root = ElementTree.parse(elem1) elem2 = elem2_root.find('config') elem2_string = ElementTree.tostring(elem2) return elem2_string
def del_tags_from_xml(xml, tag_list=[]): """ It deletes the tags either by their names or xpath Arguments: 1.xml: It takes xml file path or xml string as input 2.tag_list: It contains list of tags which needs to be removed Returns: It returns xml string """ if os.path.exists(xml): tree = ElementTree.parse(xml) root = tree.getroot() else: root = ElementTree.fromstring(xml) for tag in tag_list: if 'xpath=' in tag: tag = tag.strip('xpath=') req_tags = getChildElementsListWithSpecificXpath(root, tag) else: req_tags = getChildElementsListWithSpecificXpath(root, ".//{0}".format(tag)) recursive_delete_among_children(root, req_tags) xml_string = ElementTree.tostring(root, encoding='utf-8', method='xml') return xml_string
def __init__(self): self.nodetree = [] #tree that will contain the elements for the .mm output file self.nodetree.append("") #initialise tree self.previous_level = 0 #initialise depth control # def open(self, inputfile): # """ get opml data and load into ElementTree tree """ # if inputfile.endswith('.opml'): # try: # # self.tree = ET.parse(inputfile) # return self.tree # except: # print "Cannot open file "+inputfile+'\n' \ # "File may not exist or file may not be a valid xml file\n" \ # "\nUSAGE\n" # closedown()
def _request(self, action='GET', url='/', data=None, query_params=None): if data is None: data = {} if query_params is None: query_params = {} query_params['version'] = 1 query_params['type'] = 'xml' query_params['key'] = self.options['auth_token'] r = requests.request(action, self.api_endpoint + url, params=query_params) #data=json.dumps(data)) r.raise_for_status() # if the request fails for any reason, throw an error. # TODO: check if the response is an error using tree = ElementTree.ElementTree(ElementTree.fromstring(r.content)) root = tree.getroot() if root.find('reply').find('code').text != '300': raise Exception('An error occurred: {0}, {1}'.format(root.find('reply').find('detail').text, root.find('reply').find('code').text)) return root
def _request(self, action='GET', url='/', data=None, query_params=None): if data is None: data = {} if query_params is None: query_params = {} else: query_params['api_key'] = self.options.get('auth_token') r = requests.request(action, self.api_endpoint + url, params=query_params) tree = ElementTree.ElementTree(ElementTree.fromstring(r.content)) root = tree.getroot() if root.tag == 'error': raise Exception('An error occurred: {0}'.format(root.text)) else: r.raise_for_status() return root
def getConfigFromFile(self): try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET if not os.path.exists(self.fileName) : print "file ", self.fileName, " not exists" return None try: self.docTree = ET.ElementTree(file=self.fileName) except Exception,e: print "%s is NOT well-formed : %s "%(self.fileName,e) return None self.smtpServer = self.getSectiontText("smtpServer") self.smtpPort = self.getSectiontInt("smtpPort") self.sender = self.getSectiontText("sender").strip() self.senderPasswd = self.getSectiontText("senderPasswd") self.rcvType = self.getTextAttribute("receivers","type") self.getReceivers("receivers/user") return None
def get_status_xml(self, command): """Get status XML via HTTP and return it as XML ElementTree.""" # Get XML structure via HTTP get res = requests.get("http://{host}{command}".format( host=self._host, command=command), timeout=self.timeout) # Continue with XML processing only if HTTP status code = 200 if res.status_code == 200: try: # Return XML ElementTree return ET.fromstring(res.text) except ET.ParseError: _LOGGER.error( "Host %s returned malformed XML for: %s", self._host, command) raise ValueError else: _LOGGER.error(( "Host %s returned HTTP status code %s " "when trying to receive data"), self._host, res.status_code) raise ValueError
def probe_metric(service_url, metric): ''' Query the service at the given URL for the given metric value. Assumptions are made about the name of the method and output parameters which are only valid for the WanCommonInterfaceConfig service. ''' envelope = E(QName(ns['s'], 'Envelope'), {QName(ns['s'], 'encodingStyle'): 'http://schemas.xmlsoap.org/soap/encoding/'}) body = sE(envelope, QName(ns['s'], 'Body')) method = sE(body, QName(ns['i'], 'Get{}'.format(metric))) request_tree = ET(envelope) with io.BytesIO() as out: out.write(b'<?xml version="1.0"?>') request_tree.write(out, encoding='utf-8') out.write(b'\r\n') # or else my Belkin F5D8236-4 never responds... req = urllib.request.Request(service_url, out.getvalue()) req.add_header('Content-Type', 'text/xml') req.add_header('SOAPAction', '"{}#{}"'.format(ns['i'], 'Get{}'.format(metric))) with urllib.request.urlopen(req) as result: result_tree = ElementTree.parse(result) return int(result_tree.findtext('.//New{}'.format(metric), namespaces=ns))
def file_init(): """ >>> import io >>> stringfile = io.BytesIO(SAMPLE_XML.encode("utf-8")) >>> tree = ET.ElementTree(file=stringfile) >>> tree.find("tag").tag 'tag' >>> tree.find("section/tag").tag 'tag' >>> tree = ET.ElementTree(file=SIMPLE_XMLFILE) >>> tree.find("element").tag 'element' >>> tree.find("element/../empty-element").tag 'empty-element' """
def parseliteral(): """ >>> element = ET.XML("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode') <html><body>text</body></html> >>> element = ET.fromstring("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode') <html><body>text</body></html> >>> sequence = ["<html><body>", "text</bo", "dy></html>"] >>> element = ET.fromstringlist(sequence) >>> ET.tostring(element) b'<html><body>text</body></html>' >>> b"".join(ET.tostringlist(element)) b'<html><body>text</body></html>' >>> ET.tostring(element, "ascii") b"<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>" >>> _, ids = ET.XMLID("<html><body>text</body></html>") >>> len(ids) 0 >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>") >>> len(ids) 1 >>> ids["body"].tag 'body' """
def __init__(self, xml=None, rootnode=None): if xml is None and rootnode is None: xml = default_xml if rootnode is None: if sys.platform.startswith('win'): enc = 'ISO-8859-1' else: enc = 'UTF-8' self.dom = ElementTree.fromstring(xml, ElementTree.XMLParser(encoding=enc)) else: self.dom = rootnode # determine OME namespaces self.ns = get_namespaces(self.dom) if __name__ == '__main__': if self.ns['ome'] is None: raise Exception("Error: String not in OME-XML format") # generate a uuid if there is none # < OME UUID = "urn:uuid:ef8af211-b6c1-44d4-97de-daca46f16346" omeElem = self.dom if not omeElem.get('UUID'): omeElem.set('UUID', 'urn:uuid:'+str(uuid.uuid4())) self.uuidStr = omeElem.get('UUID')
def __str__(self): # # need to register the ome namespace because BioFormats expects # that namespace to be the default or to be explicitly named "ome" # for ns_key in ["ome"]: ns = self.ns.get(ns_key) or NS_DEFAULT.format(ns_key=ns_key) # ElementTree.register_namespace(ns_key, ns) ElementTree.register_namespace('', ns) # ElementTree.register_namespace("om", NS_ORIGINAL_METADATA) result = StringIO() ElementTree.ElementTree(self.root_node).write(result, encoding=uenc, method="xml", xml_declaration = True # default_namespace = 'http://www.openmicroscopy.org/Schemas/ome/2013-06' ) return result.getvalue()
def append_channel(self, index, name): # add channel new_channel = OMEXML.Channel( ElementTree.SubElement(self.node, qn(self.ns['ome'], "Channel"))) new_channel.SamplesPerPixel = 1 new_channel.ID = "Channel:0:"+str(index) new_channel.Name = name # add a bunch of planes with "TheC"=str(index) for t in range(self.get_SizeT()): for z in range(self.get_SizeZ()): new_plane = OMEXML.Plane( ElementTree.SubElement(self.node, qn(self.ns['ome'], "Plane"))) new_plane.TheC = str(index) new_plane.TheZ = str(z) new_plane.TheT = str(t) # update SizeC self.set_SizeC(self.get_SizeC() + 1) # can be done as a single step just prior to final output
def write(self, pretty=True): if self.documentObject.axes: self.root.append(ET.Element("axes")) for axisObject in self.documentObject.axes: self._addAxis(axisObject) if self.documentObject.rules: self.root.append(ET.Element("rules")) for ruleObject in self.documentObject.rules: self._addRule(ruleObject) if self.documentObject.sources: self.root.append(ET.Element("sources")) for sourceObject in self.documentObject.sources: self._addSource(sourceObject) if self.documentObject.instances: self.root.append(ET.Element("instances")) for instanceObject in self.documentObject.instances: self._addInstance(instanceObject) if pretty: _indent(self.root, whitespace=self._whiteSpace) tree = ET.ElementTree(self.root) tree.write(self.path, encoding="utf-8", method='xml', xml_declaration=True)
def create_job_doc(self, object_name=None, operation=None, contentType='CSV', concurrency=None, external_id_name=None): root = ET.Element("jobInfo") root.set("xmlns", self.jobNS) op = ET.SubElement(root, "operation") op.text = operation obj = ET.SubElement(root, "object") obj.text = object_name if external_id_name: ext = ET.SubElement(root, 'externalIdFieldName') ext.text = external_id_name if concurrency: con = ET.SubElement(root, "concurrencyMode") con.text = concurrency ct = ET.SubElement(root, "contentType") ct.text = contentType buf = BytesIO() tree = ET.ElementTree(root) tree.write(buf, encoding="UTF-8") return buf.getvalue().decode("utf-8")
def run(self, xml, **kwargs): """Method takes either an etree.ElementTree or raw XML text as the first argument. Args: xml(etree.ElementTree or text """ self.output = self.__graph__() if isinstance(xml, str): try: self.source = etree.XML(xml) except ValueError: try: self.source = etree.XML(xml.encode()) except: raise ValueError("Cannot run error {}".format(sys.exc_info()[0])) else: self.source = xml super(XMLProcessor, self).run(**kwargs)
def saveSettingsAndTasks(self): '''Dump current sorting and filtering choices to disk for reloading''' if not self.settingsFile: logger.warning('no settings file found, nothing will be saved') return logger.info('saving task panel\'s settings to disk: %s' % self.settingsFile) settingsToBeSaved = {} settingsToBeSaved['hideFinished'] = str(self.hideButton.isChecked()) settingsToBeSaved['sortState'] = str(self.sortButton.isChecked()) root = ET.Element('ToDoPanel') settingsEle = ET.SubElement(root, 'Settings') for k, v in settingsToBeSaved.iteritems(): settingEle = ET.SubElement(settingsEle, k) settingEle.text = v for task in self.taskStore.tasks: taskDict = task.__dict__ tasksEle = ET.SubElement(root, 'Task') for k, v in taskDict.iteritems(): taskEle = ET.SubElement(tasksEle, k) taskEle.text = str(v) tree = ET.ElementTree(root) tree.write(self.settingsFile)
def __init__(self, filename): self.fd = gzip.open(filename) self.xml = ElementTree() self.xml.parse(self.fd) self.prjs = {} self.tasks = {} self.resources = {} self.vacations = [] self._process_projects() self._process_resources() self._process_tasks() self._process_bookings() self._process_vacations() # __init__()
def __init__(self, dsxml, filename=None): """ Constructor. Default is to create datasource from xml. """ self._filename = filename self._datasourceXML = dsxml self._datasourceTree = ET.ElementTree(self._datasourceXML) self._name = self._datasourceXML.get('name') or self._datasourceXML.get( 'formatted-name') # TDS files don't have a name attribute self._version = self._datasourceXML.get('version') self._caption = self._datasourceXML.get('caption', '') self._connection_parser = ConnectionParser( self._datasourceXML, version=self._version) self._connections = self._connection_parser.get_connections() self._fields = None
def __init__(self, filepath): self._filepath = os.path.abspath(filepath) tree = ElementTree().parse(self._filepath) self._signature = tree.find("SignatureRegister").text.upper() project = tree.find("Project") nifpga = project.find("CompilationResultsTree") \ .find("CompilationResults") \ .find("NiFpga") self._base_address_on_device = int(nifpga.find("BaseAddressOnDevice").text) self._registers = {} for reg_xml in tree.find("VI").find("RegisterList"): reg = Register(reg_xml) if reg.datatype is not None: assert reg.name not in self._registers, \ "One or more registers have the same name '%s', this is not supported" % reg.name self._registers[reg.name] = reg self._fifos = {} for channel_xml in nifpga.find("DmaChannelAllocationList"): fifo = Fifo(channel_xml) self._fifos[fifo.name] = fifo
def create_sitemap(app, exception): """Generates the sitemap.xml from the collected HTML page links""" if (not app.config['html_theme_options'].get('base_url', '') or exception is not None or not app.sitemap_links): return filename = app.outdir + "/sitemap.xml" print "Generating sitemap.xml in %s" % filename root = ET.Element("urlset") root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") for link in app.sitemap_links: url = ET.SubElement(root, "url") ET.SubElement(url, "loc").text = link ET.ElementTree(root).write(filename)
def file_init(): """ >>> import StringIO >>> stringfile = StringIO.StringIO(SAMPLE_XML) >>> tree = ET.ElementTree(file=stringfile) >>> tree.find("tag").tag 'tag' >>> tree.find("section/tag").tag 'tag' >>> tree = ET.ElementTree(file=SIMPLE_XMLFILE) >>> tree.find("element").tag 'element' >>> tree.find("element/../empty-element").tag 'empty-element' """
def parseliteral(): """ >>> element = ET.XML("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout) <html><body>text</body></html> >>> element = ET.fromstring("<html><body>text</body></html>") >>> ET.ElementTree(element).write(sys.stdout) <html><body>text</body></html> >>> sequence = ["<html><body>", "text</bo", "dy></html>"] >>> element = ET.fromstringlist(sequence) >>> print ET.tostring(element) <html><body>text</body></html> >>> print "".join(ET.tostringlist(element)) <html><body>text</body></html> >>> ET.tostring(element, "ascii") "<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>" >>> _, ids = ET.XMLID("<html><body>text</body></html>") >>> len(ids) 0 >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>") >>> len(ids) 1 >>> ids["body"].tag 'body' """
def test_modify_vm_disk_file(): template = os.path.join(WORKSPACE, 'templates/physical_environment/vms/daisy.xml') tree = ET.ElementTree(file=template) root = tree.getroot() disk_path1 = os.path.join('/home/qemu/vms', 'daisy_test1.qcow2') disk_path2 = os.path.join('/home/qemu/vms', 'daisy_test2.qcow2') disks_path = [disk_path1, disk_path2] modify_vm_disk_file(root, disks_path) devices = root.find('./devices') disks = [disk for disk in devices.findall('disk') if disk.attrib['device'] == 'disk'] assert len(disks) == len(disks_path) for i in range(len(disks)): assert disks[i].attrib['type'] == 'file' driver = disks[i].find('driver') assert driver.attrib['name'] == 'qemu' and driver.attrib['type'] == 'qcow2' target = disks[i].find('target') assert target.attrib['bus'] == 'ide' source = disks[i].find('source') assert source.attrib['file'] == disks_path[i]
def test_modify_vm_bridge(): template = os.path.join(WORKSPACE, 'templates/virtual_environment/vms/daisy.xml') tree = ET.ElementTree(file=template) root = tree.getroot() bridge = 'daisy_test' modify_vm_bridge(root, bridge) devices = root.find('./devices') is_match = False for interface in devices.findall('interface'): source = interface.find('source') if interface.attrib.get('type', None) == 'bridge' \ and source is not None \ and source.attrib.get('bridge', None) == bridge: is_match = True break assert is_match
def delete_virtual_network(network_xml): LI('Begin to find and delete network %s' % network_xml) tree = ET.ElementTree(file=network_xml) root = tree.getroot() names = root.findall('./name') assert len(names) == 1 name = names[0].text result = 0 conn = libvirt.open('qemu:///system') for net in conn.listAllNetworks(): if name == net.name(): if net.isActive(): net.destroy() LI('Network %s is destroyed' % name) net.undefine() LI('Network %s is deleted' % name) result = 1 break conn.close() if not result: LI('Network %s is not found' % name)
def to_testlink_xml_content(testsuite): assert isinstance(testsuite, TestSuite) root_suite = Element(Tags.testsuite) root_suite.set(Attributes.name, testsuite.name) cache['testcase_count'] = 0 for suite in testsuite.sub_suites: assert isinstance(suite, TestSuite) if should_skip(suite.name): continue suite_element = SubElement(root_suite, Tags.testsuite) suite_element.set(Attributes.name, suite.name) build_text_field(suite_element, Tags.details, suite.details) build_testcase_xml(suite, suite_element) tree = ElementTree.ElementTree(root_suite) f = BytesIO() tree.write(f, encoding='utf-8', xml_declaration=True) return f.getvalue()
def indent(elem, level=0): """ import xml.etree.ElementTree as ET elem = ET.Element('MyElem') indent(elem) """ i = "\n" + level*"\t" if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + "\t" if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i
def extractEvents(filename): xmlTree = ElementTree(file=filename[:-3]+"apf.xml") #root = xmlTree.getroot() eventArrOneDoc = [] for eventEle in xmlTree.iter(tag="event"): #print eventEle #print eventEle.tag, eventEle.attrib eventArr = extractEvent(eventEle) #print eventArr eventArrOneDoc.extend(eventArr) #print event2str(eventArrOneDoc[0], "\t") return eventArrOneDoc # forth_layer(event): [optional]event_argument, event_mention # fifth_layer(event_mention): extent, ldc_scope, anchor, [optional]event_mention_argument # sixth_layer(event_mention_argument): extent # event_v1 = [sentence_ldc_scope, eventType, eventSubType, anchorText, (argText, role), (argText, role), ...] # event_v2 = [(sentence_ldc_scope, index), eventType, eventSubType, (anchorText, index), (argText, role, index), (argText, role, index), ...]
def fetch(self): """ Fetches the list of deputies for the current term. """ xml = urllib.request.urlopen(self.URL) tree = ET.ElementTree(file=xml) records = self._parse_deputies(tree.getroot()) df = pd.DataFrame(records, columns=( 'congressperson_id', 'budget_id', 'condition', 'congressperson_document', 'civil_name', 'congressperson_name', 'picture_url', 'gender', 'state', 'party', 'phone_number', 'email' )) return self._translate(df)
def _all_presences(self, deputies, start_date, end_date): error_count = 0 for i, deputy in deputies.iterrows(): log.debug(i, deputy.congressperson_name, deputy.congressperson_document) url = self.URL.format(start_date, end_date, deputy.congressperson_document) xml = self._try_fetch_xml(10, url) if xml is None: error_count += 1 else: root = ET.ElementTree(file=xml).getroot() for presence in self._parse_deputy_presences(root): yield presence time.sleep(self.sleep_interval) log.debug("\nErrored fetching", error_count, "deputy presences")
def test_error_rcm_pressurerise(self): """Test for appropriate error if RCM file has pressure rise. """ file_path = os.path.join('testfile_rcm.xml') filename = pkg_resources.resource_filename(__name__, file_path) # add pressure rise to common properties tree = etree.parse(filename) root = tree.getroot() properties = root.find('commonProperties') prop = etree.SubElement(properties, 'property') prop.set('name', 'pressure rise') prop.set('units', '1/ms') prop_value = etree.SubElement(prop, 'value') prop_value.text = '0.10' # write new file, and try to load et = etree.ElementTree(root) with TemporaryDirectory() as temp_dir: filename = os.path.join(temp_dir, 'test.xml') et.write(filename, encoding='utf-8', xml_declaration=True) with pytest.raises(KeywordError) as excinfo: ReSpecTh_to_ChemKED(filename) assert 'Pressure rise cannot be defined for RCM.' in str(excinfo.value)
def onFinishBuilding(app, exception): currentVersion = app.env.config["version"] if "latest_docs_version" in app.env.config["html_context"].keys(): latestVersion = app.env.config["html_context"]["latest_docs_version"] else: latestVersion = "dev" base_domain = app.env.config["html_context"]["SITEMAP_DOMAIN"] file_path = "./_build/algolia_index/index.json" sitemap_path = "./_build/sitemap/sitemap_" + currentVersion + ".xml" checkDirectory(file_path) checkDirectory(sitemap_path) f = open(file_path, 'w+') root = ET.Element("urlset") root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") for link in indexObjs: url = ET.SubElement(root, "url") ET.SubElement(url, "loc").text = base_domain + str(currentVersion) + "/" + link["url"] ET.SubElement(url, "changefreq").text = "daily" ET.SubElement(url, "priority").text = "1" if ( currentVersion == latestVersion ) else "0.5" ET.ElementTree(root).write(sitemap_path) f.write(json.dumps(indexObjs))
def CDATA(text=None): """ A CDATA element factory function that uses the function itself as the tag (based on the Comment factory function in the ElementTree implementation). """ element = etree.Element(CDATA) element.text = text return element # We're replacing the _write method of the ElementTree class so that it would # recognize and correctly print out CDATA sections.
def parse_xml(self, xml): self._element_tree = ET.ElementTree(ET.fromstring(xml)) self.clear() self._walk_xml(self._element_tree.getroot())
def get_xml(self): f = BytesIO() tree = ET.ElementTree(self.root) tree.write(f, encoding='windows-1251', xml_declaration=True) return f.getvalue()
def saveTree(self,store): """ @description: save the treeview in the mama.xml file @param: store the listStore attach to the treeview """ # if there is still an entry in the model config = expanduser('~') +'/.config/mama/mama.xml' try: if not os.path.exists(os.path.dirname(config)): os.makedirs(os.path.dirname(config)) root = ET.Element("data") if len(store) != 0: for i in range(len(store)): iter = store.get_iter(i) if store[iter][0] != '' and store[iter][1] != '': for s in store[iter][0].split('|'): s = s.lower() s = s.replace('*',' ') Type = ET.SubElement(root, "entry") Type.set("name",unicode(store[iter][2],"utf-8")) Key = ET.SubElement(Type, "key") Key.text = unicode(s,"utf-8") Command = ET.SubElement(Type, "command") Command.text = unicode(store[iter][1],"utf-8") Linker = ET.SubElement(Type, "linker") Spacebyplus = ET.SubElement(Type, "spacebyplus") if store[iter][3] is not None or store[iter][4] is not None: Linker.text = unicode(store[iter][3],"utf-8") Spacebyplus.text = unicode(store[iter][4],"utf-8") tree = ET.ElementTree(root).write(config,encoding="utf-8",xml_declaration=True) except IOError: print("Unable to write the file")
def saveTree(self, store): """ @description: save the treeview in the mama.xml file @param: store the listStore attach to the treeview """ # if there is still an entry in the model config = expanduser('~') + '/.config/mama/mama.xml' try: if not os.path.exists(os.path.dirname(config)): os.makedirs(os.path.dirname(config)) root = ET.Element("data") if len(store) != 0: for i in range(len(store)): iter = store.get_iter(i) if store[iter][0] != '' and store[iter][1] != '': for s in store[iter][0].split('|'): s = s.lower() s = s.replace('*', ' ') Type = ET.SubElement(root, "entry") Type.set("name", unicode(store[iter][2], "utf-8")) Key = ET.SubElement(Type, "key") Key.text = unicode(s, "utf-8") Command = ET.SubElement(Type, "command") Command.text = unicode(store[iter][1], "utf-8") Linker = ET.SubElement(Type, "linker") Spacebyplus = ET.SubElement(Type, "spacebyplus") if store[iter][3] is not None and store[iter][ 4] is not None: Linker.text = unicode(store[iter][3], "utf-8") Spacebyplus.text = unicode(store[iter][4], "utf-8") tree = ET.ElementTree(root).write(config, encoding="utf-8", xml_declaration=True) except IOError: print("Unable to write the file")