Python xml.etree.ElementTree.fromstring() Examples

The following are 30 code examples of xml.etree.ElementTree.fromstring(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.etree.ElementTree , or try the search function .
Example #1
Source File: OSMTGC.py    From TGC-Designer-Tools with Apache License 2.0 6 votes vote down vote up
def addOSMFromXML(course_json, xml_data, options_dict={}, printf=print):
    printf("Adding OpenStreetMap from XML")
    op = overpy.Overpass()
    result = op.parse_xml(xml_data)

    printf("Determining the UTM Geo Projection for this area")
    # Find the lat and lon bounding box from the XML directly
    # Can't find the query bounds in overpy
    root = ET.fromstring(xml_data)
    for bounds in root.iter('bounds'):
        latmin = float(bounds.get('minlat'))
        latmax = float(bounds.get('maxlat'))
        lonmin = float(bounds.get('minlon'))
        lonmax = float(bounds.get('maxlon'))
        break
    
    # Create a basic geopointcloud to handle this projection
    pc = GeoPointCloud()
    pc.addFromLatLon((latmin, lonmin), (latmax, lonmax), printf=printf)

    trees = addOSMToTGC(course_json, pc, result, x_offset=float(options_dict.get('adjust_ew', 0.0)), y_offset=float(options_dict.get('adjust_ns', 0.0)), \
                options_dict=options_dict, printf=printf)

    return course_json, trees 
Example #2
Source File: webdav.py    From rucio with Apache License 2.0 6 votes vote down vote up
def get_space_usage(self):
        """
        Get RSE space usage information.

        :returns: a list with dict containing 'totalsize' and 'unusedsize'

        :raises ServiceUnavailable: if some generic error occured in the library.
        """
        endpoint_basepath = self.path2pfn('')
        headers = {'Depth': '0'}

        try:
            root = ET.fromstring(self.session.request('PROPFIND', endpoint_basepath, verify=False, headers=headers, cert=self.session.cert).text)
            usedsize = root[0][1][0].find('{DAV:}quota-used-bytes').text
            try:
                unusedsize = root[0][1][0].find('{DAV:}quota-available-bytes').text
            except Exception as error:
                print('No free space given, return -999')
                unusedsize = -999
            totalsize = int(usedsize) + int(unusedsize)
            return totalsize, unusedsize
        except Exception as error:
            raise exception.ServiceUnavailable(error) 
Example #3
Source File: utils.py    From rucio with Apache License 2.0 6 votes vote down vote up
def parse_replicas_from_string(string):
    """
    Parses the output of list_replicas from a json or metalink string
    into a dictionary. Metalink parsing is tried first and if it fails
    it tries to parse json.

    :param string: the string to parse

    :returns: a list with a dictionary for each file
    """
    try:
        root = ElementTree.fromstring(string)
        return parse_replicas_metalink(root)
    except ElementTree.ParseError as xml_err:
        try:
            return json.loads(string)
        except ValueError as json_err:
            raise MetalinkJsonParsingError(string, xml_err, json_err) 
Example #4
Source File: test_parsers_svd.py    From yasha with MIT License 6 votes vote down vote up
def test_register_folding_commaseparated_index():
    r = cmsis.SvdRegister(et.fromstring(
        """
        <register>
            <dim>3</dim>
            <dimIncrement>4</dimIncrement>
            <dimIndex>A,B,C</dimIndex>
            <name>GPIO_%s</name>
            <addressOffset>4</addressOffset>
        </register>
        """
    ))
    a = r.fold()

    assert len(a) == 3
    assert a[0].name == "GPIO_A"
    assert a[1].name == "GPIO_B"
    assert a[2].name == "GPIO_C" 
Example #5
Source File: test_parsers_svd.py    From yasha with MIT License 6 votes vote down vote up
def test_register_is_dimensionless_after_fold_up():
    r = cmsis.SvdRegister(et.fromstring(
        """
        <register>
            <dim>4</dim>
            <dimIncrement>4</dimIncrement>
            <dimIndex>3-6</dimIndex>
            <name>IRQ%s</name>
            <addressOffset>4</addressOffset>
        </register>
        """
    ))
    for r in r.fold():
        assert r.dim == None
        assert r.dimIndex == None
        assert r.dimIncrement == None 
Example #6
Source File: fetch_disease_trial_mapping.py    From EliIE with MIT License 6 votes vote down vote up
def extract_criteria(cid):   #Using IDs to retrieve eligibility criteria
    output = ""
    if cid is not None:
        url_trial = 'http://clinicaltrials.gov/show/%s?displayxml=true'
        #url_trial ='http://clinicaltrials.gov/search?term=%s&displayxml=true'
        page = download_web_data(url_trial % cid)
        #with codecs.open('temp.txt', 'w','utf8') as writer:
        #    writer.write(page)
        #with codec.open('temp.txt', 'r', 'utf8') as reader:
        if page is not None:
            ct_xml = xml_parser.fromstring (page)
            ec = ct_xml.find ('eligibility')
            if ec is not None:
                # parse to get criteria text
                d = ec.find ('criteria')
                if d is not None:
                    txt = d.find ('textblock')
                    if txt is not None:
                        output = txt.text
    return output


#============== 
Example #7
Source File: metadata.py    From sfdclib with MIT License 6 votes vote down vote up
def _retrieve_deploy_result(self, async_process_id):
        """ Retrieves status for specified deployment id """
        attributes = {
            'client': 'Metahelper',
            'sessionId': self._session.get_session_id(),
            'asyncProcessId': async_process_id,
            'includeDetails': 'true'
            }
        mt_request = msg.CHECK_DEPLOY_STATUS_MSG.format(**attributes)
        headers = {'Content-type': 'text/xml', 'SOAPAction': 'checkDeployStatus'}
        res = self._session.post(self._get_api_url(), headers=headers, data=mt_request)
        root = ET.fromstring(res.text)
        result = root.find(
            'soapenv:Body/mt:checkDeployStatusResponse/mt:result',
            self._XML_NAMESPACES)
        if result is None:
            raise Exception("Result node could not be found: %s" % res.text)

        return result 
Example #8
Source File: metadata.py    From sfdclib with MIT License 6 votes vote down vote up
def _retrieve_retrieve_result(self, async_process_id, include_zip):
        """ Retrieves status for specified retrieval id """
        attributes = {
            'client': 'Metahelper',
            'sessionId': self._session.get_session_id(),
            'asyncProcessId': async_process_id,
            'includeZip': include_zip
        }
        mt_request = msg.CHECK_RETRIEVE_STATUS_MSG.format(**attributes)
        headers = {'Content-type': 'text/xml', 'SOAPAction': 'checkRetrieveStatus'}
        res = self._session.post(self._get_api_url(), headers=headers, data=mt_request)
        root = ET.fromstring(res.text)
        result = root.find(
            'soapenv:Body/mt:checkRetrieveStatusResponse/mt:result',
            self._XML_NAMESPACES)
        if result is None:
            raise Exception("Result node could not be found: %s" % res.text)

        return result 
Example #9
Source File: session.py    From sfdclib with MIT License 6 votes vote down vote up
def login(self):
        url = self.construct_url(self.get_soap_api_uri())
        headers = {'Content-Type': 'text/xml', 'SOAPAction': 'login'}
        password = self._password
        if self._token:
            password += self._token
        data = SfdcSession._LOGIN_TMPL.format(**{'username': self._username, 'password': password})
        r = self.post(url, headers=headers, data=data)
        root = ET.fromstring(r.text)
        if root.find('soapenv:Body/soapenv:Fault', SfdcSession._XML_NAMESPACES):
            raise Exception("Could not log in. Code: %s Message: %s" % (
                root.find('soapenv:Body/soapenv:Fault/faultcode', SfdcSession._XML_NAMESPACES).text,
                root.find('soapenv:Body/soapenv:Fault/faultstring', SfdcSession._XML_NAMESPACES).text))
        self._session_id = root.find('soapenv:Body/d:loginResponse/d:result/d:sessionId', SfdcSession._XML_NAMESPACES).text
        server_url = root.find('soapenv:Body/d:loginResponse/d:result/d:serverUrl', SfdcSession._XML_NAMESPACES).text
        self._instance = re.search("""https://(.*).salesforce.com/.*""", server_url).group(1) 
Example #10
Source File: retrieve_texts.py    From EliIE with MIT License 6 votes vote down vote up
def extract_description(cid):   #Using IDs to retrieve eligibility criteria
    output = ""
    if cid is not None:
        url_trial = 'http://clinicaltrials.gov/show/%s?displayxml=true'
        #url_trial ='http://clinicaltrials.gov/search?term=%s&displayxml=true'
        page = download_web_data(url_trial % cid)
        #with codecs.open('temp.txt', 'w','utf8') as writer:
        #    writer.write(page)
        #with codec.open('temp.txt', 'r', 'utf8') as reader:
        if page is not None:
            ct_xml = xml_parser.fromstring (page)
            summary = ct_xml.find ('brief_summary')
            if summary is not None:
                txt = summary.find ('textblock')
                if txt is not None:
                     output = txt.text
            description = ct_xml.find('detailed_description')
            if description is not None:
                txt2 = summary.find('textblock')
                if txt2 is not None:
                    output = output+txt2.text

    return output 
Example #11
Source File: xml.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def from_xml(data):
    '''
    Convert XML to ElementTree.Element

    Parameters
    ----------
    data : string
        The XML to parse

    Returns
    -------
    :class:`ElementTree.Element`

    '''
    try:
        return ET.fromstring(data)
    except:
        for i, line in enumerate(data.split('\n')):
            print(i+1, line)
        raise 
Example #12
Source File: schema.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def from_xml(cls, data, session=None):
        '''
        Create schema field from XML definition

        Parameters
        ----------
        data : xml-string or ElementTree.Element
            The schema field XML definition
        session : requests.Session
            The ESP session object

        Returns
        -------
        :class:`SchemaField`

        '''
        out = cls('', type='double', key=False)
        out.session = session

        if isinstance(data, six.string_types):
            data = ET.fromstring(data)

        out._set_attributes(data.attrib)

        return out 
Example #13
Source File: retrieve_texts.py    From EliIE with MIT License 6 votes vote down vote up
def extract_criteria(cid):   #Using IDs to retrieve eligibility criteria
    output = ""
    if cid is not None:
        url_trial = 'http://clinicaltrials.gov/show/%s?displayxml=true'
        #url_trial ='http://clinicaltrials.gov/search?term=%s&displayxml=true'
        page = download_web_data(url_trial % cid)
        #with codecs.open('temp.txt', 'w','utf8') as writer:
        #    writer.write(page)
        #with codec.open('temp.txt', 'r', 'utf8') as reader:
        if page is not None:
            ct_xml = xml_parser.fromstring (page)
            ec = ct_xml.find ('eligibility')
            if ec is not None:
                # parse to get criteria text
                d = ec.find ('criteria')
                if d is not None:
                    txt = d.find ('textblock')
                    if txt is not None:
                        output = txt.text
    return output 
Example #14
Source File: tvdb.py    From plugin.video.kmediatorrent with GNU General Public License v3.0 6 votes vote down vote up
def get_all_meta(show_id):
    import xml.etree.ElementTree as ET
    from concurrent import futures
    from kmediatorrent.utils import url_get, joining

    def _get_all_meta():
        r = url_get("%s/all/%s.xml" % (show_base_url(show_id), LANG), headers=HEADERS, with_immunicity=False)
        dom = ET.fromstring(r)
        if not len(dom):
            return
        return update_image_urls(dom2dict(dom))
    with futures.ThreadPoolExecutor(max_workers=2) as pool:
        meta = pool.submit(_get_all_meta)
        banners = pool.submit(get_banners, show_id)
    meta = meta.result()
    meta["series"][0]["episodes"] = meta["episode"]
    meta = meta["series"][0]
    meta["banners"] = banners.result() or []
    return meta 
Example #15
Source File: zipreader.py    From PoseWarper with Apache License 2.0 6 votes vote down vote up
def xmlread(filename):
    global _xml_path_zip
    global _xml_zfile
    path = filename
    pos_at = path.index('@')
    if pos_at == -1:
        print("character '@' is not found from the given path '%s'"%(path))
        assert 0
    path_zip = path[0: pos_at]
    path_xml = path[pos_at + 2:]
    if not os.path.isfile(path_zip):
        print("zip file '%s' is not found"%(path_zip))
        assert 0
    for i in xrange(len(_xml_path_zip)):
        if _xml_path_zip[i] == path_zip:
            data = _xml_zfile[i].open(path_xml)
            return ET.fromstring(data.read())
    _xml_path_zip.append(path_zip)
    print("read new xml file '%s'"%(path_zip))
    _xml_zfile.append(zipfile.ZipFile(path_zip, 'r'))
    data = _xml_zfile[-1].open(path_xml)
    return ET.fromstring(data.read()) 
Example #16
Source File: tvdb.py    From plugin.video.kmediatorrent with GNU General Public License v3.0 6 votes vote down vote up
def search(name, complete=False):
    from kmediatorrent.caching import shelf
    import hashlib
    search_hash = hashlib.sha1(name).hexdigest()
    with shelf("com.thetvdb.search.%s" % search_hash) as show:
        if not show:
            import re
            import xml.etree.ElementTree as ET
            from kmediatorrent.utils import url_get
            dom = ET.fromstring(url_get("%s/api/GetSeries.php" % BASE_URL, params={
                "seriesname": name,
            }, headers=HEADERS, with_immunicity=False))
            if not len(dom):
                return
            meta = dom2dict(dom[0])
            if not complete:
                return update_image_urls(meta)
            show.update(get(meta["id"]))
        return show 
Example #17
Source File: userid.py    From terraform-templates with Apache License 2.0 6 votes vote down vote up
def __init__(self, device, prefix="", ignore_dup_errors=True):
        # Create a class logger
        self._logger = getlogger(__name__ + "." + self.__class__.__name__)
        self.device = device
        self.prefix = prefix
        self.ignore_dup_errors = ignore_dup_errors

        # Build the initial uid-message
        self._uidmessage = ET.fromstring("<uid-message>" +
                                         "<version>1.0</version>" +
                                         "<type>update</type>" +
                                         "<payload/>" +
                                         "</uid-message>")
        # Batch state
        self._batch = False
        self._batch_uidmessage = deepcopy(self._uidmessage) 
Example #18
Source File: tva.py    From movistartv2xmltv with GNU General Public License v2.0 6 votes vote down vote up
def channellist(self,clist):
        root = ET.fromstring(self.xmldata)
        services = root[0][0].findall("{urn:dvb:ipisdns:2006}SingleService")
        for i in services:
            channelid = i[1].attrib["ServiceName"]
            clist[channelid] = {}
            #clist[channelid]["logo"] = i[1].attrib["logoURI"]
            url = "http://172.26.22.23:2001/appclient/incoming/epg/MAY_1/imSer/"+channelid+".jpg"
            clist[channelid]["logo"] = url
            clist[channelid]["address"] = i[0][0].attrib["Address"]
            clist[channelid]["port"] = i[0][0].attrib["Port"]
            clist[channelid]["name"] = i[2][0].text
            clist[channelid]["shortname"] = i[2][1].text
            clist[channelid]["desc"] = i[2][2].text
            clist[channelid]["tags"] = i[2][3][0].text.split("/")
        return clist 
Example #19
Source File: alexaranking.py    From threat_intel with MIT License 6 votes vote down vote up
def _extract_response_xml(self, domain, response):
        """Extract XML content of an HTTP response into dictionary format.

        Args:
            response: HTML Response objects
        Returns:
            A dictionary: {alexa-ranking key : alexa-ranking value}.
        """
        attributes = {}
        alexa_keys = {'POPULARITY': 'TEXT', 'REACH': 'RANK', 'RANK': 'DELTA'}
        try:
            xml_root = ET.fromstring(response._content)
            for xml_child in xml_root.findall('SD//'):
                if xml_child.tag in alexa_keys and \
                        alexa_keys[xml_child.tag] in xml_child.attrib:
                    attributes[xml_child.tag.lower(
                    )] = xml_child.attrib[alexa_keys[xml_child.tag]]
        except ParseError:
            # Skip ill-formatted XML and return no Alexa attributes
            pass
        attributes['domain'] = domain
        return {'attributes': attributes} 
Example #20
Source File: internals.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, etree):
        r"""
        Initialize a new Element wrapper for ``etree``.

        If ``etree`` is a string, then it will be converted to an
        Element object using ``ElementTree.fromstring()`` first:

            >>> ElementWrapper("<test></test>")
            <Element "<?xml version='1.0' encoding='utf8'?>\n<test />">

        """
        if isinstance(etree, compat.string_types):
            etree = ElementTree.fromstring(etree)
        self.__dict__['_etree'] = etree 
Example #21
Source File: 41-weblogic-2018-2894.py    From vulscan with MIT License 6 votes vote down vote up
def get_current_work_path(host):
    geturl = host + "/ws_utc/resources/setting/options/general"
    ua = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0'}
    request = requests.get(geturl)
    values = []
    if request.status_code == 404:
        exit("{} 404 not found".format(geturl))
    elif "Deploying Application".lower() in request.text.lower():
        print("First Deploying waiting a moment")
        time.sleep(30)
        request = requests.get(geturl, headers=ua)
    if "</defaultValue>" in request.content:
        root = ET.fromstring(request.content)
        value = root.find("section").find("options")
        for e in value:
            for sub in e:
                if e.tag == "parameter" and sub.tag == "defaultValue":
                    values.append(sub.text)
    if values:
        return values[0]
    else:
        exit(request.content) 
Example #22
Source File: connections.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def message(self,message):
        if self.isHandshakeComplete == False:
            Connection.message(self,message)
            return

        #logging.info("MSG: " + message)

        xml = None
        o = None

        for c in message:
            if c == '{' or c == '[':
                o = json.loads(str(message))
                break
            elif c == '<':
                xml = ElementTree.fromstring(str(message))
                break

        if o != None:
            self.processJson(o)
        elif xml != None:
            self.processXml(xml) 
Example #23
Source File: wfapi.py    From terraform-templates with Apache License 2.0 5 votes vote down vote up
def __set_xml_response(self, message_body):
        self._log(DEBUG2, '__set_xml_response: %s', repr(message_body))
        self.response_type = 'xml'

        _message_body = message_body.decode(_encoding)
        if len(_message_body) == 0:
            return True

        self.response_body = _message_body

        # ParseError: "XML or text declaration not at start of entity"
        # fix: remove leading blank lines if exist
        _message_body = message_body
        while (_message_body[0:1] == b'\r' or
               _message_body[0:1] == b'\n'):
            _message_body = _message_body[1:]

        if len(_message_body) == 0:
            return True

        try:
            element = etree.fromstring(_message_body)
        except etree.ParseError as msg:
            self._msg = 'ElementTree.fromstring ParseError: %s' % msg
            return False

        self.xml_element_root = element

        return True 
Example #24
Source File: pfam_db.py    From phageParser with MIT License 5 votes vote down vote up
def get_translation_url(translation):
    url = "http://pfam.xfam.org/search/sequence"
    data = {'seq': translation, 'output': 'xml'}

    response = requests.get(url, data=data)

    tree = ElementTree.fromstring(response.content)

    return tree[0][1].text 
Example #25
Source File: vls.py    From NEIE-Assistant with GNU General Public License v3.0 5 votes vote down vote up
def __sopa_post(self, method, body):
        head = {'Username': self.username, 'Password': self.password}
        self.__encode_object(head)

        headers = {'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'http://www.open.com.cn/' + method}
        payload = self.__xml(method, head, body)

        r = requests.post('http://' + self.path + '/WebService/ServiceV3.asmx', headers=headers, data=payload)
        root = ET.fromstring(r.text)
        result = {}
        body = root[0][0]
        for child in body:
            if child.text != None:
                result[child.tag[24:]] = child.text
        return result 
Example #26
Source File: cmsis.py    From yasha with MIT License 5 votes vote down vote up
def __init__(self, file):
        if isinstance(file, str):
            self.root = ElementTree.fromstring(file)
        else:
            tree = ElementTree.parse(file)
            self.root = tree.getroot()

        self.cpu = None
        self.device = None
        self.peripherals = []
        self.peripherals_dict = {}  # Lookup by peripheral name 
Example #27
Source File: adwind.py    From RATDecoders with MIT License 5 votes vote down vote up
def get_config(self):
        '''
        This is the main entry
        :return:
        '''

        key = "awenubisskqi"

        # Get the file from the zip
        zip_xml = self.file_info.file_from_zip('config.xml')

        # No Easy way to detect version so just try it

        try:
            xml_string = crypto.decrypt_des_ecb(key[:-4], zip_xml)
        except ValueError:
            xml_string = crypto.decrypt_arc4(key, zip_xml)

        # Convert to XML
        xml = ET.fromstring(xml_string)

        # Read the XML to a config dict
        config_dict = {}

        for child in xml:
            if child.text.startswith("Adwind RAT"):
                config_dict['Version'] = child.text
            else:
                config_dict[child.attrib['key']] = child.text

        # Parse the config
        config_dict = self.parse_config(config_dict)

        # Set the config to the class for use
        self.config = config_dict 
Example #28
Source File: test_parsers_svd.py    From yasha with MIT License 5 votes vote down vote up
def test_register_element():
    reg = cmsis.SvdRegister(et.fromstring(
        """
        <register>
            <name>TimerCtrl0</name>
            <description>Timer Control Register</description>
            <addressOffset>0x0</addressOffset>
            <access>read-write</access>
            <resetValue>0x00008001</resetValue>
            <resetMask>0x0000ffff</resetMask>
            <size>32</size>
            <writeConstraint>
                <writeAsRead>true</writeAsRead>
                <useEnumeratedValues>true</useEnumeratedValues>
                <range>
                    <minimum>0</minimum>
                    <maximum>5</maximum>
                </range>
            </writeConstraint>
        </register>
        """
    ))
    assert reg.name == "TimerCtrl0"
    assert reg.description == "Timer Control Register"
    assert reg.addressOffset == 0
    assert reg.access == "read-write"
    assert reg.resetValue == 0x8001
    assert reg.resetMask == 0xffff
    assert reg.size == 32
    assert reg.writeConstraint.writeAsRead == True
    assert reg.writeConstraint.useEnumeratedValues == True
    assert reg.writeConstraint.range == (0,5) 
Example #29
Source File: jbifrost.py    From RATDecoders with MIT License 5 votes vote down vote up
def parse_xml(xml_string):
        # Convert to XML
        xml = ET.fromstring(xml_string)

        # Read the XML to a config dict
        properties_dict = {}

        for child in xml:
            if child.attrib:
                properties_dict[child.attrib['key']] = child.text

        return properties_dict 
Example #30
Source File: test_parsers_svd.py    From yasha with MIT License 5 votes vote down vote up
def test_peripheral_element():
    periph = cmsis.SvdPeripheral(et.fromstring(
        """
        <peripheral>
            <name>TIMER0</name>
            <version>1.0</version>
            <description>A standard timer</description>
            <baseAddress>0x40002000</baseAddress>
            <addressBlock>
                <offset>0x0</offset>
                <size>0x400</size>
                <usage>registers</usage>
                <protection>s</protection>
            </addressBlock>
            <interrupt>
                <name>TIM0_INT</name>
                <value>34</value>
            </interrupt>
        </peripheral>
        """
    ))

    assert len(periph.interrupts) == 1
    assert periph.name == "TIMER0"
    assert periph.version == "1.0"
    assert periph.description == "A standard timer"
    assert periph.baseAddress == 1073750016
    assert periph.addressBlock.offset == 0
    assert periph.addressBlock.size == 1024
    assert periph.addressBlock.usage == "registers"
    assert periph.addressBlock.protection == "s"