Python lxml.objectify.fromstring() Examples

The following are 30 code examples of lxml.objectify.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 lxml.objectify , or try the search function .
Example #1
Source File: test_parsers.py    From spid-testenv2 with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_deserialization(self):
        xml = """\
<root>
    <child1>some data</child1>
    <child2 AnAttribute="more data"></child2>
    <SpecialChild3>
        <Item AnotherAttribute="foo"></Item>
        <Item EvenAnotherAttribute="bar"></Item>
    </SpecialChild3>
</root>"""
        xml_doc = objectify.fromstring(xml)
        saml_tree = SAMLTree(xml_doc, multi_occur_tags={'Item'})
        self.assertEqual(saml_tree.child1.text, 'some data')
        self.assertEqual(saml_tree.child1.tag, 'child1')
        self.assertEqual(saml_tree.child2.an_attribute, 'more data')
        self.assertEqual(len(saml_tree.special_child3.item), 2)
        self.assertEqual(saml_tree.special_child3.tag, 'special_child3')
        self.assertEqual(saml_tree.special_child3.item[
                         0].another_attribute, 'foo')
        self.assertEqual(saml_tree.special_child3.item[
                         1].even_another_attribute, 'bar') 
Example #2
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def site_scan_history(self, siteid):
        response = request(self, "SiteScanHistory", {"site-id" : siteid})
        history = objectify.fromstring(etree.tostring(response))
        scans = []
        
        for scan in history.ScanSummary:
            vulns = []
            for vuln in scan.vulnerabilities:
                vulns.append(Vulnerability(**dict(vuln.items())))

            summaryItems = dict(scan.items())
            summaryItems['siteid'] = summaryItems['site-id']
            del summaryItems['site-id']
            summaryItems['scanid'] = summaryItems['scan-id']
            del summaryItems['scan-id']
            summaryItems['engineid'] = summaryItems['engine-id']
            del summaryItems['engine-id']
            summaryItems['tasks'] = Tasks(**dict(scan.tasks.items()))
            summaryItems['nodes'] = Nodes(**dict(scan.nodes.items()))
            summaryItems['vulns'] = vulns
            scans.append(ScanSummary(**summaryItems))
            
        return scans 
Example #3
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scan_statistics(self, scanid):
        response = request(self, "ScanStatistics", {'scan-id' : scanid})
        ss = objectify.fromstring(etree.tostring(response)).ScanSummary
        vulns = []
        for vuln in ss.vulnerabilities:
            vulns.append(Vulnerability(**dict(vuln.items())))

        summaryItems = dict(ss.items())
        summaryItems['siteid'] = summaryItems['site-id']
        del summaryItems['site-id']
        summaryItems['scanid'] = summaryItems['scan-id']
        del summaryItems['scan-id']
        summaryItems['engineid'] = summaryItems['engine-id']
        del summaryItems['engine-id']
        summaryItems['tasks'] = Tasks(**dict(ss.tasks.items()))
        summaryItems['nodes'] = Nodes(**dict(ss.nodes.items()))
        summaryItems['vulns'] = vulns
        return ScanSummary(**summaryItems) 
Example #4
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scan_activity(self):
        response = request(self, "ScanActivity")
        scans = objectify.fromstring(etree.tostring(response))
        scanSummaryList = []
        for scan in scans.ScanSummary:
            vulns = []
            for vuln in scan.vulnerabilities:
                vulns.append(Vulnerability(**dict(vuln.items())))

            summaryItems = dict(scan.items())
            summaryItems['siteid'] = summaryItems['site-id']
            del summaryItems['site-id']
            summaryItems['scanid'] = summaryItems['scan-id']
            del summaryItems['scan-id']
            summaryItems['engineid'] = summaryItems['engine-id']
            del summaryItems['engine-id']
            summaryItems['tasks'] = Tasks(**dict(scan.tasks.items()))
            summaryItems['nodes'] = Nodes(**dict(scan.nodes.items()))
            summaryItems['vulns'] = vulns
            scanSummaryList.append(ScanSummary(**summaryItems))
            
        return scanSummaryList 
Example #5
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def engine_activity(self, engineid):
        response = request(self, "EngineActivity", {"engine-id" : engineid})
        activity = objectify.fromstring(etree.tostring(response))
        ss = activity.ScanSummary[0]
        vulns = []
        for vuln in ss.vulnerabilities:
            vulns.append(Vulnerability(**dict(vuln.items())))

        summaryItems = dict(ss.items())
        summaryItems['siteid'] = summaryItems['site-id']
        del summaryItems['site-id']
        summaryItems['scanid'] = summaryItems['scan-id']
        del summaryItems['scan-id']
        summaryItems['engineid'] = summaryItems['engine-id']
        del summaryItems['engine-id']
        summaryItems['tasks'] = Tasks(**dict(ss.tasks.items()))
        summaryItems['nodes'] = Nodes(**dict(ss.nodes.items()))
        summaryItems['vulns'] = vulns
        return ScanSummary(**summaryItems) 
Example #6
Source File: virtualbox_ovf_test.py    From kiwi with GNU General Public License v3.0 6 votes vote down vote up
def test_ovf_parameters(self):
        args = {
            'vm_name': 'jumper',
            'root_uuid': 'uuid',
            'vm_description': 'Leap 15 image',
            'disk_image_capacity': 21,
        }
        ovf_file = self.ovf_template.get_template().substitute(args)
        ovf = objectify.fromstring(ovf_file)

        for (attr, value) in ovf.DiskSection.Disk.items():
            if 'capacity' in attr:
                assert value == str(args['disk_image_capacity'])

        vm_name_attr, vm_name_value = ovf.VirtualSystem.items()[0]
        assert 'id' in vm_name_attr and vm_name_value == args['vm_name']

        cimos_id_attr, cimos_id_value = \
            ovf.VirtualSystem.OperatingSystemSection.items()[0]
        assert 'id' in cimos_id_attr and int(cimos_id_value) == 101

        assert ovf.VirtualSystem.OperatingSystemSection.Description \
            == args['vm_description'] 
Example #7
Source File: crypto.py    From spid-testenv2 with GNU Affero General Public License v3.0 6 votes vote down vote up
def sign_http_post(xmlstr, key, cert, message=False, assertion=True):
    logger.debug('http-post signing')

    signer = XMLSigner(
        signature_algorithm='rsa-sha256',
        digest_algorithm='sha256',
        c14n_algorithm='http://www.w3.org/2001/10/xml-exc-c14n#',
    )
    root = fromstring(xmlstr)

    if assertion:
        logger.debug('signing assertion')
        assertions = root.findall('{%s}Assertion' % SAML)
        for assertion in assertions:
            issuer = assertion.find('{%s}Issuer' % SAML)
            issuer.addnext(fromstring(
                '<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="placeholder"></ds:Signature>'))
            root = signer.sign(root, reference_uri=assertion.attrib['ID'], key=key, cert=cert)
    if message:
        issuer = root.find('{%s}Issuer' % SAML)
        issuer.addnext(fromstring(
            '<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="placeholder"></ds:Signature>'))
        root = signer.sign(root, key=key, cert=cert)
    return tostring(root, pretty_print=False) 
Example #8
Source File: parser.py    From geos with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fromstring(text, schema=None):
    """Parses a KML text string
    
    This function parses a KML text string and optionally validates it against 
    a provided schema object"""
    if schema:
        parser = objectify.makeparser(schema = schema.schema)
        return objectify.fromstring(text, parser=parser)
    else:
        return objectify.fromstring(text) 
Example #9
Source File: xmltools.py    From bioservices with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, obj):
        """obj can be easyXML data set"""
        from lxml import objectify
        try:
            self.root = objectify.fromstring(obj.data)
        except:
            # try something else
            self.root = objectify.fromstring(obj)
        self.obj = obj 
Example #10
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_sites(self):
        response = request(self, "SiteListing")
        sites = objectify.fromstring(etree.tostring(response))
        sitesList = []
        siteSummaryList = []
        for site in sites.SiteSummary:
            sitesList.append(dict(site.items()))

        for site in sitesList:
            siteSummaryList.append(SiteSummary(site['description'], site['id'], site['name'], site['riskfactor'], site['riskscore']))

        return siteSummaryList 
Example #11
Source File: main.py    From jira-issues-importer with Apache License 2.0 5 votes vote down vote up
def read_xml_sourcefile(file_name):
  all_text = open(file_name).read()
  return objectify.fromstring(all_text) 
Example #12
Source File: authenticator.py    From openconnect-sso with GNU General Public License v3.0 5 votes vote down vote up
def parse_response(resp):
    resp.raise_for_status()
    xml = objectify.fromstring(resp.content)
    t = xml.get("type")
    if t == "auth-request":
        return parse_auth_request_response(xml)
    elif t == "complete":
        return parse_auth_complete_response(xml) 
Example #13
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_engines(self):
        response = request(self, "EngineListing")
        engines = objectify.fromstring(etree.tostring(response))
        enginesList = []
        engineSummaryList = []
        for engine in engines.EngineSummary:
            enginesList.append(dict(engine.items()))

        for engine in enginesList:
            engineSummaryList.append(EngineSummary(engine['id'], engine['name'], engine['address'], engine['port'], engine['status'], engine['scope']))

        return engineSummaryList 
Example #14
Source File: eagle_http.py    From Eagle-Http-API with MIT License 5 votes vote down vote up
def parse_xml_response(self, text):
        try:
            self.xmlTree = objectify.fromstring(text)
            module = __import__('api_classes')
            # print self.xmlTree.tag
            class_ = getattr(module, self.xmlTree.tag)
            instance = class_(self.json, self.xmlTree, text)
            setattr(self, self.xmlTree.tag, instance)
            return instance
        except:
            raise 
Example #15
Source File: pnexpose.py    From pnexpose with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, nameOrConn, templateOrID):
        if nameOrConn.authtoken:
            response = request(nameOrConn, "SiteConfig", {"site-id" : templateOrID})
            siteData = objectify.fromstring(etree.tostring(response))
            siteProperties = dict(siteData.Site.items())
            self.id = int(siteProperties['id'])
            self.name = siteProperties['name']
            self.description = siteProperties['description']
            self.riskfactor = float(siteProperties['riskfactor'])
            self.isDynamic = siteProperties['isDynamic']
            self.assets = list(siteData.Site.Hosts.host)
        else:
            self.name = nameOrConn
            self.scan_template = templateOrID 
Example #16
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def _reload(obj):
        obj = objectify.fromstring(etree.tostring(obj))
        objectify.deannotate(obj, xsi_nil=True, cleanup_namespaces=True)
        return obj 
Example #17
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def __init__(self, document=None):
        """Initialize constructor for MavenPom class.

        :document: Parse the content of the file.
        :returns: None

        """
        if not document:
            raise ValueError("No content is provided for parsing")

        self.document = document.strip()
        if not isinstance(self.document, (bytes, bytearray)):
            self.document = self.document.encode()

        self.root = objectify.fromstring(self.document)

        # create a dependencies element if doesn't exist
        if getattr(self.root, 'dependencies', None) is None:
            _prev = getattr(self.root, 'dependencyManagement', None)\
                or getattr(self.root, 'properties', None)\
                or getattr(self.root, 'name', None)
            if _prev is not None:
                _prev.addnext(objectify.Element('dependencies'))
            else:
                self.root.dependencies = objectify.ObjectifiedElement()
            self.root = self._reload(self.root)

        self.dependency_set = set([self.Dependency(d) for d in getattr(
            self.root.dependencies, 'dependency', [])]) 
Example #18
Source File: make_tiny_imagenet.py    From outlier-exposure with Apache License 2.0 5 votes vote down vote up
def parse_xml_file(filename):
  with open(filename, 'r') as f:
    xml = f.read()
  ann = objectify.fromstring(xml)
  if ann.filename != '%s':
    img_filename = '%s.JPEG' % ann.filename
  else:
    img_filename = filename[filename.rfind('/')+1:-3] + 'JPEG'
  bbox = ann.object.bndbox
  bbox = [bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax]
  bbox = [int(x) for x in bbox]
  name = str(ann.object.name)
  return img_filename, bbox, name 
Example #19
Source File: utils.py    From jaide with GNU General Public License v2.0 5 votes vote down vote up
def xpath(source_xml, xpath_expr, req_format='string'):
    """ Filter xml based on an xpath expression.

    Purpose: This function applies an Xpath expression to the XML
           | supplied by source_xml. Returns a string subtree or
           | subtrees that match the Xpath expression. It can also return
           | an xml object if desired.

    @param source_xml: Plain text XML that will be filtered
    @type source_xml: str or lxml.etree.ElementTree.Element object
    @param xpath_expr: Xpath expression that we will filter the XML by.
    @type xpath_expr: str
    @param req_format: the desired format of the response, accepts string or
                     | xml.
    @type req_format: str

    @returns: The filtered XML if filtering was successful. Otherwise,
            | an empty string.
    @rtype: str or ElementTree
    """
    tree = source_xml
    if not isinstance(source_xml, ET.Element):
        tree = objectify.fromstring(source_xml)
    # clean up the namespace in the tags, as namespaces appear to confuse
    # xpath method
    for elem in tree.getiterator():
        # beware of factory functions such as Comment
        if isinstance(elem.tag, basestring):
            i = elem.tag.find('}')
            if i >= 0:
                elem.tag = elem.tag[i+1:]

    # remove unused namespaces
    objectify.deannotate(tree, cleanup_namespaces=True)
    filtered_list = tree.xpath(xpath_expr)
    # Return string from the list of Elements or pure xml
    if req_format == 'xml':
        return filtered_list
    matches = ''.join(etree.tostring(
        element, pretty_print=True) for element in filtered_list)
    return matches if matches else "" 
Example #20
Source File: testing.py    From testplan with Apache License 2.0 5 votes vote down vote up
def compare(self, xml_str, encoding="utf-8"):
        """Compare with xml string input."""
        # fromstring complains when we have UTF declaration like
        # `<?xml version="1.0" encoding="utf-8" ?>`, so we use bytes instead
        # https://stackoverflow.com/a/38244227
        xml_bytes = bytes(bytearray(xml_str, encoding=encoding))
        xml_obj = objectify.fromstring(xml_bytes)
        self._compare_obj(xml_obj) 
Example #21
Source File: structures_generator.py    From opcua-modeling-tool with MIT License 5 votes vote down vote up
def make_model_from_string(self, xml):
        obj = objectify.fromstring(xml)
        self._make_model(obj) 
Example #22
Source File: parser.py    From spid-testenv2 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _deserialize(self):
        xml_doc = objectify.fromstring(self._request.saml_request)
        return self._saml_class(xml_doc) 
Example #23
Source File: xmltools.py    From bioservices with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, data, encoding="utf-8"):
        """.. rubric:: Constructor

        :param data: an XML document format
        :param fixing_unicode: use only with HGNC service to fix issue with the
            XML returned by that particular service. No need to use otherwise.
            See :class:`~bioservices.hgnc.HGNC` documentation for details.
        :param encoding: default is utf-8 used. Used to fix the HGNC XML only.


        The data parameter must be a string containing the XML document. If you
        have an URL instead, use :class:`readXML`

        """
        #if fixing_unicode:
        #    x = unicodefix.FixingUnicode(data, verbose=False, encoding=encoding)
        #    self.data = x.fixed_string.encode("utf-8")
        #else:
        self.data = data[:]

        try:
            self.root = ET.fromstring(self.data)
        except:
            self.root = self.data[:]
        self._soup = None
        self.prettify = self.soup.prettify
        self.findAll = self.soup.findAll 
Example #24
Source File: utils.py    From spid-testenv2 with GNU Affero General Public License v3.0 5 votes vote down vote up
def saml_to_dict(xmlstr):
    root = objectify.fromstring(xmlstr)

    def _obj(elem):
        children = {}
        for child in elem.iterchildren():
            subdict = _obj(child)

            if child.tag in MULTIPLE_OCCURRENCES_TAGS:
                existing = children.get(child.tag, None)
                if isinstance(existing, list):
                    existing.append(subdict)
                else:
                    children[child.tag] = [subdict]
            else:
                children[child.tag] = subdict

        text = elem.text
        if text is not None:
            text = text.strip()

        return {
            'attrs': dict(elem.attrib),
            'children': children,
            'text': text,
        }

    return {
        root.tag: _obj(root)
    } 
Example #25
Source File: crypto.py    From spid-testenv2 with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, certificate, request, verifier=None):
        self._cert = certificate
        self._request = request
        self._verifier = verifier or XMLVerifier()
        self._xml_doc = objectify.fromstring(request.saml_request) 
Example #26
Source File: lxmlGramplet.py    From addons-source with GNU General Public License v2.0 5 votes vote down vote up
def xsd(self, xsd, filename):
        """
        Look at schema, validation, conform, structure, content, etc...
        Code for 1.7.1
        """

        # syntax check against XSD for file format

        schema = etree.XMLSchema(file=xsd)

        parser = objectify.makeparser(schema = schema)

        tree = etree.parse(filename)
        root = tree.getroot()

        database = objectify.fromstring(etree.tostring(root, encoding="UTF-8"), parser)
        LOG.info(_('Matches XSD schema.'))

        #dump = objectify.dump(database)
        #print(dump) 
Example #27
Source File: outcome_response.py    From ontask_b with MIT License 5 votes vote down vote up
def process_xml(self, xml):
        """
        Parse OutcomeResponse data form XML.
        """
        try:
            root = objectify.fromstring(xml)
            # Get message idenifier from header info
            self.message_identifier = root.imsx_POXHeader.\
                imsx_POXResponseHeaderInfo.\
                imsx_messageIdentifier

            status_node = root.imsx_POXHeader.\
                imsx_POXResponseHeaderInfo.\
                imsx_statusInfo

            # Get status parameters from header info status
            self.code_major = status_node.imsx_codeMajor
            self.severity = status_node.imsx_severity
            self.description = status_node.imsx_description
            self.message_ref_identifier = str(status_node.
                                              imsx_messageRefIdentifier)
            self.operation = status_node.imsx_operationRefIdentifier

            try:
                # Try to get the score
                self.score = str(root.imsx_POXBody.readResultResponse.
                                 result.resultScore.textString)
            except AttributeError:
                # Not a readResult, just ignore!
                pass
        except:
            pass 
Example #28
Source File: outcome_request.py    From ontask_b with MIT License 5 votes vote down vote up
def process_xml(self, xml):
        """
        Parse Outcome Request data from XML.
        """
        root = objectify.fromstring(xml)
        self.message_identifier = str(root.imsx_POXHeader.
                                      imsx_POXRequestHeaderInfo.imsx_messageIdentifier)
        try:
            result = root.imsx_POXBody.replaceResultRequest
            self.operation = REPLACE_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
            self.score = str(result.resultRecord.result.
                             resultScore.textString)
        except Exception:
            pass

        try:
            result = root.imsx_POXBody.deleteResultRequest
            self.operation = DELETE_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
        except Exception:
            pass

        try:
            result = root.imsx_POXBody.readResultRequest
            self.operation = READ_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
        except Exception:
            pass 
Example #29
Source File: base.py    From ryu with Apache License 2.0 5 votes vote down vote up
def from_xml(cls, xmlstring):
        et = objectify.fromstring(xmlstring)
        return cls.from_et(et) 
Example #30
Source File: qualys_web.py    From VulnWhisperer with Apache License 2.0 5 votes vote down vote up
def get_was_scan_count(self, status):
        """
        Checks number of scans, used to control the api limits
        """
        parameters = (
            E.ServiceRequest(
                E.filters(
                    E.Criteria({'field': 'status', 'operator': 'EQUALS'}, status))))
        xml_output = self.qgc.request(self.COUNT_WASSCAN, parameters)
        root = objectify.fromstring(xml_output.encode('utf-8'))
        return root.count.text