Python xml.etree.cElementTree.fromstring() Examples

The following are 30 code examples of xml.etree.cElementTree.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.cElementTree , or try the search function .
Example #1
Source File: biarchtool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def add_explicit_disable(self, wipebinaries=False):

        self._init_biarch_packages()

        resulturl = self.makeurl(['source', self.project])
        result = ET.fromstring(self.cached_GET(resulturl))

        for pkg in self.packages:

            changed = False

            logger.debug("processing %s", pkg)
            if not pkg in self.package_metas:
                logger.error("%s not found", pkg)
                continue
            pkgmeta = self.package_metas[pkg]

            build = pkgmeta.findall("./build")
            if not build:
                logger.debug('disable %s for %s', pkg, self.arch)
                bn = pkgmeta.find('build')
                if bn is None:
                    bn = ET.SubElement(pkgmeta, 'build')
                ET.SubElement(bn, 'disable', { 'arch': self.arch })
                changed = True

            if changed:
                try:
                    pkgmetaurl = self.makeurl(['source', self.project, pkg, '_meta'])
                    self.http_PUT(pkgmetaurl, data=ET.tostring(pkgmeta))
                    if self.caching:
                        self._invalidate__cached_GET(pkgmetaurl)
                    if wipebinaries:
                        self.http_POST(self.makeurl(['build', self.project], {
                            'cmd': 'wipe',
                            'arch': self.arch,
                            'package': pkg }))
                except HTTPError as e:
                    logger.error('failed to update %s: %s', pkg, e) 
Example #2
Source File: layout_volume.py    From manila with Apache License 2.0 6 votes vote down vote up
def delete_snapshot(self, context, snapshot, share_server=None):
        """Deletes a snapshot."""

        gluster_mgr = self._share_manager(snapshot['share'])
        backend_snapshot_name = self._find_actual_backend_snapshot_name(
            gluster_mgr, snapshot)
        args = ('--xml', 'snapshot', 'delete', backend_snapshot_name,
                '--mode=script')
        out, err = gluster_mgr.gluster_call(
            *args,
            log=("Error deleting snapshot"))

        if not out:
            raise exception.GlusterfsException(
                _('gluster snapshot delete %s: no data received') %
                gluster_mgr.volume
            )

        outxml = etree.fromstring(out)
        gluster_mgr.xml_response_check(outxml, args[1:]) 
Example #3
Source File: WXBizMsgCrypt_py3.py    From TaskBot with GNU General Public License v3.0 6 votes vote down vote up
def extract(self, xmltext):
        """提取出xml数据包中的加密消息
        @param xmltext: 待提取的xml字符串
        @return: 提取出的加密消息字符串
        """
        try:
            xml_tree = ET.fromstring(xmltext)
            encrypt = xml_tree.find("Encrypt")
            touser_name = xml_tree.find("ToUserName")
            return ierror.WXBizMsgCrypt_OK, encrypt.text, touser_name.text
        except Exception as e:
            print(e)
            return ierror.WXBizMsgCrypt_ParseXml_Error, None, None 
Example #4
Source File: layout_directory.py    From manila with Apache License 2.0 6 votes vote down vote up
def _get_directory_usage(self, share):
        share_dir = '/' + share['name']

        args = ('--xml', 'volume', 'quota', self.gluster_manager.volume,
                'list', share_dir)

        try:
            out, err = self.gluster_manager.gluster_call(*args)
        except exception.GlusterfsException:
            LOG.error('Unable to get quota share %s', share['name'])
            raise

        volxml = etree.fromstring(out)
        usage_byte = volxml.find('./volQuota/limit/used_space').text
        usage = utils.translate_string_size_to_float(usage_byte)

        return usage 
Example #5
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 #6
Source File: common.py    From manila with Apache License 2.0 6 votes vote down vote up
def _get_vol_option_via_info(self, option):
        """Get the value of an option set on a GlusterFS volume via volinfo."""
        args = ('--xml', 'volume', 'info', self.volume)
        out, err = self.gluster_call(*args, log=("retrieving volume info"))

        if not out:
            raise exception.GlusterfsException(
                'gluster volume info %s: no data received' %
                self.volume
            )

        volxml = etree.fromstring(out)
        self.xml_response_check(volxml, args[1:], './volInfo/volumes/count')
        for e in volxml.findall(".//option"):
            o, v = (volxml_get(e, a) for a in ('name', 'value'))
            if o == option:
                return v 
Example #7
Source File: cve_lookup.py    From LibScanner with GNU General Public License v3.0 6 votes vote down vote up
def parse_dbs(folder):
    """
    parse the XML dbs and build an in-memory lookup
    :param folder: the folder full of *.xml files
    :return:
    """
    root = None
    for filename in glob.glob(folder+'/*.xml'):
        with open(filename) as f:
            db_string = f.read() # remove the annoying namespace
            db_string = re.sub(' xmlns="[^"]+"', '', db_string, count=1)
            # xmlstring.append(db_string)
            data = ET.fromstring(db_string)
            if root is None:
                root = data
            else:
                root.extend(data)

    return root


#root = ET.fromstring("\n".join(xmlstring))
# namespace ="http://nvd.nist.gov/feeds/cve/1.2" 
Example #8
Source File: cve_lookup.py    From LibScanner with GNU General Public License v3.0 6 votes vote down vote up
def get_packages_swid(package_list):
    """
    Get the packages from a swid string
    :param package_strs:
    :return:
    """
    package_xml = None
    packages = defaultdict(set)
    errors = []
    for xml_doc in package_list.split("\n"):
        try:
            # remove the <? ?> if any
            xml_doc = re.sub('<\?[^>]+\?>', '', xml_doc)
            # use DET since this is untrusted data
            data = DET.fromstring(xml_doc)
            name, version = data.attrib['name'], data.attrib['version']
            version = version.split("-")[0]
            packages[name].add(version)

        except Exception as e:
            errors.append(str(e))

    return errors, packages 
Example #9
Source File: update_crawler.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def latest_packages(self):
        apiurl = self.apiurl
        prj = self.from_prj
        if prj.startswith('openSUSE.org:'):
            apiurl = 'https://api.opensuse.org'
            prj = prj[len('openSUSE.org:'):]
        data = self.cached_GET(makeurl(apiurl,
                               ['project', 'latest_commits', prj]))
        lc = ET.fromstring(data)
        packages = set()
        for entry in lc.findall('{http://www.w3.org/2005/Atom}entry'):
            title = entry.find('{http://www.w3.org/2005/Atom}title').text
            if title.startswith('In '):
                packages.add(title[3:].split(' ')[0])
        return sorted(packages) 
Example #10
Source File: update_crawler.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def is_maintenance_project(self, prj):
        root = ET.fromstring(self.get_project_meta(prj))
        return root.get('kind', None) == 'maintenance_release' 
Example #11
Source File: kv_client.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def list_collection(self, collection=None, app=None, owner="nobody"):
        """
        :collection: collection name. When euqals "None", return all
        collections in the system.
        :return: a list containing the connection names if successful, throws
        KVNotExists if no such colection or other exception if other error
        happened
        """

        uri = self._get_config_endpoint(app, owner, collection)

        content = self._do_request(uri, method="GET")
        m = re.search(r'xmlns="([^"]+)"', content)
        path = "./entry/title"
        if m:
            ns = m.group(1)
            path = "./{%s}entry/{%s}title" % (ns, ns)

        collections = et.fromstring(content)
        return [node.text for node in collections.iterfind(path)] 
Example #12
Source File: biarchtool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def get_filelist(self, project, package, expand = False):
        query = {}
        if expand:
            query['expand'] = 1
        root = ET.fromstring(self.cached_GET(self.makeurl(['source', self.project, package], query)))
        return [ node.get('name') for node in root.findall('entry') ] 
Example #13
Source File: biarchtool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def fill_package_meta(self):
        url = self.makeurl(['search', 'package'], "match=[@project='%s']" % self.project)
        root = ET.fromstring(self.cached_GET(url))
        for p in root.findall('package'):
            name = p.attrib['name']
            self.package_metas[name] = p 
Example #14
Source File: biarchtool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def _init_rdeps(self):
        if self.rdeps is not None:
            return
        self.rdeps = dict()
        url = self.makeurl(['build', self.project, 'standard', self.arch, '_builddepinfo' ], {'view': 'revpkgnames'})
        x = ET.fromstring(self.cached_GET(url))
        for pnode in x.findall('package'):
            name = pnode.get('name')
            for depnode in pnode.findall('pkgdep'):
                depname = depnode.text
                if depname == name:
                    logger.warning('%s requires itself for build', name)
                    continue
                self.rdeps.setdefault(name, set()).add(depname) 
Example #15
Source File: biarchtool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def _filter_packages_by_time(self, packages):
        x = ET.fromstring(self.cached_GET(self.makeurl(['source', self.project, '000product', '_history'], {'limit': '1'})))
        producttime = int(x.find('./revision/time').text)
        for pkg in packages:
            try:
                x = ET.fromstring(self.cached_GET(self.makeurl(['source', self.project, pkg, '_history'], {'rev': '1'})))
            # catch deleted packages
            except HTTPError as e:
                if e.code == 404:
                    continue
                raise e

            packagetime = int(x.find('./revision/time').text)
#            if producttime > packagetime:
#                continue
            yield pkg 
Example #16
Source File: parser.py    From teye_scanner_for_book with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #17
Source File: accept_command.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def reset_rebuild_data(self, project):
        data = self.api.pseudometa_file_load('support_pkg_rebuild')
        if data is None:
            return

        root = ET.fromstring(data)
        for stg in root.findall('staging'):
            if stg.get('name') == project:
                stg.find('rebuild').text = 'unknown'
                stg.find('supportpkg').text = ''

        # reset accepted staging project rebuild state to unknown and clean up
        # supportpkg list
        content = ET.tostring(root)
        if content != data:
            self.api.pseudometa_file_save('support_pkg_rebuild', content, 'accept command update') 
Example #18
Source File: fcc_submitter.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def check_multiple_specfiles(self, project, package):
        try:
            url = makeurl(self.apiurl, ['source', project, package], { 'expand': '1' } )
        except HTTPError as e:
            if e.code == 404:
                return None
            raise e
        root = ET.fromstring(http_GET(url).read())
        data = {}
        linkinfo = root.find('linkinfo')
        if linkinfo is not None:
            data['linkinfo'] = linkinfo.attrib['package']
        else:
            data['linkinfo'] = None

        files = [ entry.get('name').replace('.spec', '') for entry in root.findall('entry') if entry.get('name').endswith('.spec') ]
        data['specs'] = files

        if len(files) > 1:
            return data
        else:
            return False 
Example #19
Source File: update.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def get_max_revision(self, job):
        repo = self.repo_prefix + '/'
        repo += self.maintenance_project.replace(':', ':/')
        repo += ':/{!s}'.format(job['id'])
        max_revision = 0
        for channel in job['channels']:
            crepo = repo + '/' + channel.replace(':', '_')
            xml = requests.get(crepo + '/repodata/repomd.xml')
            if not xml.ok:
                self.logger.info("{} skipped .. need wait".format(crepo))
                # if one fails, we skip it and wait
                return False
            root = ET.fromstring(xml.text)
            rev = root.find('.//{http://linux.duke.edu/metadata/repo}revision')
            rev = int(rev.text)
            if rev > max_revision:
                max_revision = rev
        return max_revision 
Example #20
Source File: update.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def patch_id(repo):
        url = repo + 'repodata/repomd.xml'
        repomd = requests.get(url)
        if not repomd.ok:
            return None
        root = ET.fromstring(repomd.text)

        cs = root.find(
            './/{http://linux.duke.edu/metadata/repo}data[@type="updateinfo"]/{http://linux.duke.edu/metadata/repo}location')
        try:
            url = repo + cs.attrib['href']
        except AttributeError:
            return None

        repomd = requests.get(url).content
        root = ET.fromstring(decompress(repomd))
        return root.find('.//id').text

    # take the first package name we find - often enough correct 
Example #21
Source File: saml.py    From awsprocesscreds with Apache License 2.0 6 votes vote down vote up
def _parse_roles(self, assertion):
        attribute = '{urn:oasis:names:tc:SAML:2.0:assertion}Attribute'
        attr_value = '{urn:oasis:names:tc:SAML:2.0:assertion}AttributeValue'
        awsroles = []
        root = ET.fromstring(base64.b64decode(assertion).decode('ascii'))
        for attr in root.iter(attribute):
            if attr.get('Name') == \
                    'https://aws.amazon.com/SAML/Attributes/Role':
                for value in attr.iter(attr_value):
                    parts = [p.strip() for p in value.text.split(',')]
                    # Deals with "role_arn,pricipal_arn" or its reversed order
                    if 'saml-provider' in parts[0]:
                        role = {'PrincipalArn': parts[0], 'RoleArn': parts[1]}
                    else:
                        role = {'PrincipalArn': parts[1], 'RoleArn': parts[0]}
                    awsroles.append(role)
        return awsroles 
Example #22
Source File: myq-garage.py    From myq-garage with MIT License 6 votes vote down vote up
def isy_get_var_id(name):
    varname = str(ISY_VAR_PREFIX + name.replace(" ", "_"))
    try:
        r = requests.get('http://' + ISY_HOST + ':' + ISY_PORT + '/rest/vars/definitions/2', auth=HTTPBasicAuth(ISY_USERNAME, ISY_PASSWORD))
    except requests.exceptions.RequestException as err:
        LOGGER.error('Caught Exception in isy_get_var_id: ' + str(err))
        return  
    #LOGGER.info('Get_Var_ID: Request response: %s %s', r.status_code, r.text)
    tree = ElementTree.fromstring(r.text)
    LOGGER.info('Searching ISY Definitions for %s', varname)
    valid = False
    for e in tree.findall('e'):
        if e.get('name') == varname:
                valid = True
                id, name = e.get('id'), e.get('name')
    if valid:
        #id, name = child.get('id'), child.get('name')
        LOGGER.info('State variable: %s found with ID: %s', name, id)        
    else:
        print("State variable: " + varname + " not found in ISY variable list")
        print("Fix your state variables on the ISY. Then enable the ISY section again.")
        sys.exit(5)
    init, value = isy_get_var_state(id)
    LOGGER.info('ISY Get Var ID Return - id: %s - varname: %s - init: %s - value: %s', id, varname, init, value)
    return id, varname, init, value 
Example #23
Source File: GXDLMSTranslator.py    From Gurux.DLMS.Python with GNU General Public License v2.0 6 votes vote down vote up
def XmlToData(self, xml):
        s = GXDLMSXmlSettings(self.outputType, self.hex, self.showStringAsHex, self.tagsByName)
        self.__getAllDataNodes(ET.fromstring(xml).iter(), s)
        return s.data 
Example #24
Source File: xmlutil.py    From sqrmelon with MIT License 6 votes vote down vote up
def parseXMLWithIncludes(xmlFilePath):
    assert isinstance(xmlFilePath, FilePath)
    text = xmlFilePath.content()

    subs = []
    for result in re.finditer(r'<!--[ \t]*#[ \t]*include[ \t]+(.+)[ \t]*-->', text):
        inline = result.group(1).strip()
        inlineText = xmlFilePath.parent().join(inline).content()
        subs.append((result.start(0), result.end(0), inlineText))

    for start, end, repl in reversed(subs):
        text = '{}{}{}'.format(text[:start], repl, text[end:])

    xRoot = cElementTree.fromstring(text)
    xmlFixSlashesRecursively(xRoot)
    return xRoot 
Example #25
Source File: torque.py    From pulsar with Apache License 2.0 6 votes vote down vote up
def parse_status(self, status, job_ids):
        # in case there's noise in the output, find the big blob 'o xml
        tree = None
        rval = {}
        for line in status.strip().splitlines():
            try:
                tree = et.fromstring(line.strip())
                assert tree.tag == 'Data'
                break
            except Exception:
                tree = None
        if tree is None:
            log.warning('No valid qstat XML return from `qstat -x`, got the following: %s' % status)
            return None
        else:
            for job in tree.findall('Job'):
                id = job.find('Job_Id').text
                if id in job_ids:
                    state = job.find('job_state').text
                    # map PBS job states to Galaxy job states.
                    rval[id] = self._get_job_state(state)
        return rval 
Example #26
Source File: java_violations_reporter.py    From diff_cover with Apache License 2.0 6 votes vote down vote up
def parse_reports(self, reports):
        """
        Args:
            reports: list[str] - output from the report
        Return:
            A dict[Str:Violation]
            Violation is a simple named tuple Defined above
        """
        violations_dict = defaultdict(list)
        for report in reports:
            xml_document = etree.fromstring("".join(report))
            node_files = xml_document.findall(".//file")
            for node_file in node_files:
                for error in node_file.findall('violation'):
                    line_number = error.get('beginline')
                    error_str = "{}: {}".format(error.get('rule'),
                                                error.text.strip())
                    violation = Violation(int(line_number), error_str)
                    filename = GitPathTool.relative_path(node_file.get('name'))
                    filename = filename.replace(os.sep, "/")
                    violations_dict[filename].append(violation)

        return violations_dict 
Example #27
Source File: XnatUtils.py    From dax with MIT License 6 votes vote down vote up
def __init__(self, intf, proj, subj, sess):
        """
        Entry point for the CachedImageSession class

        :param intf: pyxnat Interface object
        :param proj: XNAT project ID
        :param subj: XNAT subject ID/label
        :param sess: XNAT session ID/label
        :return: None

        """
        self.reset_cached_time()
        experiment = intf.select_experiment(proj, subj, sess)
        xml_str = experiment.get()
        self.sess_element = ET.fromstring(xml_str)
        self.project = proj
        self.subject = subj
        self.session = sess
        self.intf = intf  # cache for later usage
        self.full_object_ = experiment
        self.sess_info_ = None
        self.scans_ = None
        self.assessors_ = None
        self.datatype_ = None
        self.creation_timestamp_ = None 
Example #28
Source File: java_violations_reporter.py    From diff_cover with Apache License 2.0 6 votes vote down vote up
def parse_reports(self, reports):
        """
        Args:
            reports: list[str] - output from the report
        Return:
            A dict[Str:Violation]
            Violation is a simple named tuple Defined above
        """
        violations_dict = defaultdict(list)
        for report in reports:
            xml_document = etree.fromstring("".join(report))
            files = xml_document.findall(".//file")
            for file_tree in files:
                for error in file_tree.findall('error'):
                    line_number = error.get('line')
                    error_str = "{}: {}".format(error.get('severity'),
                                                   error.get('message'))
                    violation = Violation(int(line_number), error_str)
                    filename = GitPathTool.relative_path(file_tree.get('name'))
                    violations_dict[filename].append(violation)
        return violations_dict 
Example #29
Source File: test_violations_reporter.py    From diff_cover with Apache License 2.0 6 votes vote down vote up
def test_non_python_violations_empty_path(self):
        """
        In the wild empty sources can happen. See https://github.com/Bachmann1234/diff-cover/issues/88
        Best I can tell its mostly irrelevant but I mostly dont want it crashing
        """

        xml = etree.fromstring('''
        <coverage line-rate="0.178" branch-rate="0.348" version="1.9" timestamp="1545037553" lines-covered="675" lines-valid="3787" branches-covered="260" branches-valid="747">
        <sources>
        <source></source>
        </sources>
        </coverage>
        ''')

        coverage = XmlCoverageReporter([xml])

        self.assertEqual(
            set(),
            coverage.violations('')
        )

        self.assertEqual(
            set(),
            coverage.measured_lines('')
        ) 
Example #30
Source File: XnatToBids.py    From dax with MIT License 6 votes vote down vote up
def dataset_description_file(BIDS_DIR, XNAT, project):
    """
    Build BIDS dataset description json file
    :param BIDS_DIR: BIDS directory
    :param XNAT: XNAT interface
    :param project: XNAT Project
    """
    BIDSVERSION = "1.0.1"
    dataset_description = dict()
    dataset_description['BIDSVersion'] = BIDSVERSION
    dataset_description['Name'] = project
    dataset_description['DatasetDOI'] = XNAT.host
    project_info = XNAT.select('/project/' + project).get()
    project_info = ET.fromstring(project_info)
    PI_element = project_info.findall('{http://nrg.wustl.edu/xnat}PI')
    if len(PI_element) > 0:
        dataset_description['Author'] = PI_element[0][1].text, PI_element[0][0].text
    else:
        dataset_description['Author'] = "No Author defined on XNAT"
    dd_file = os.path.join(BIDS_DIR, project)
    if not os.path.exists(dd_file):
        os.makedirs(dd_file)
    with open(os.path.join(dd_file, 'dataset_description.json'), 'w+') as f:
        json.dump(dataset_description, f, indent=2)