Python lxml.etree.tostring() Examples

The following are 30 code examples of lxml.etree.tostring(). 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.etree , or try the search function .
Example #1
Source File: tool.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def write_all_groups(self):
        self._check_supplements()
        summary = dict()
        archs = ['*'] + self.all_architectures
        for name in self.groups:
            group = self.groups[name]
            if not group.solved:
                continue
            summary[name] = group.summary()
            fn = '{}.group'.format(group.name)
            with open(os.path.join(self.output_dir, fn), 'w') as fh:
                comment = group.comment
                for arch in archs:
                    x = group.toxml(arch, self.ignore_broken, comment)
                    # only comment first time
                    comment = None
                    x = ET.tostring(x, pretty_print=True, encoding='unicode')
                    x = re.sub(r'\s*<!-- reason:', ' <!-- reason:', x)
                    fh.write(x)
        return summary 
Example #2
Source File: main.py    From dcc with Apache License 2.0 6 votes vote down vote up
def androaxml_main(inp, outp=None, resource=None):
    ret_type = androconf.is_android(inp)
    if ret_type == "APK":
        a = apk.APK(inp)
        if resource:
            if resource not in a.files:
                print("The APK does not contain a file called '{}'".format(resource), file=sys.stderr)
                sys.exit(1)

            axml = AXMLPrinter(a.get_file(resource)).get_xml_obj()
        else:
            axml = a.get_android_manifest_xml()
    elif ".xml" in inp:
        axml = AXMLPrinter(read(inp)).get_xml_obj()
    else:
        print("Unknown file type")
        sys.exit(1)

    buff = etree.tostring(axml, pretty_print=True, encoding="utf-8")
    if outp:
        with open(outp, "wb") as fd:
            fd.write(buff)
    else:
        sys.stdout.write(highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"), TerminalFormatter())) 
Example #3
Source File: belvaParseXML.py    From Basic-Expression-Lexicon-Variation-Algorithms-BELVA with GNU General Public License v3.0 6 votes vote down vote up
def parseXMLxpathSearch(xml_source, xpathString):
#---------------------------------------------------------------------------------

    return_values = []

    try:
        root = etree.XML(xml_source)

        data_points = root.xpath(xpathString)
    
        for data in data_points:
            return_values.append(etree.tostring(data))
            data.clear()

    except:
        pass

    return return_values

#---------------------------------------------------------------------------------
# parse XML and return value asked (designed for errors via stdout) 
Example #4
Source File: xml.py    From python-gvm with GNU General Public License v3.0 6 votes vote down vote up
def pretty_print(xml):
    """Prints beautiful XML-Code

    This function gets a string containing the xml, an object of
    List[lxml.etree.Element] or directly a lxml element.

    Print it with good readable format.

    Arguments:
        xml (str, List[lxml.etree.Element] or lxml.etree.Element):
            xml as string,
            List[lxml.etree.Element] or directly a lxml element.

    """
    if isinstance(xml, list):
        for item in xml:
            if etree.iselement(item):
                print(etree.tostring(item, pretty_print=True).decode("utf-8"))
            else:
                print(item)
    elif etree.iselement(xml):
        print(etree.tostring(xml, pretty_print=True).decode("utf-8"))
    elif isinstance(xml, str):
        tree = secET.fromstring(xml)
        print(etree.tostring(tree, pretty_print=True).decode("utf-8")) 
Example #5
Source File: populate.py    From phageParser with MIT License 6 votes vote down vote up
def addpositionstodict(gendict):
    print("Downloading position information from web...")
    for accidwithloc in tqdm(gendict):
        if 'Start' in gendict[accidwithloc]:
            continue
        accid = '_'.join(accidwithloc.split('_')[:-1])
        url = ('http://crispr.i2bc.paris-saclay.fr/crispr/crispr_db.php?'
               'checked%5B%5D={}'.format(accid))
        page = requests.get(url)
        htmltable = html.fromstring(page.content).xpath(
            "//table[normalize-space(@class)='primary_table']")[1]
        strtable = etree.tostring(htmltable)
        # converts to pandas df and then to numpy array then drop titles
        arrtable = pandas.read_html(strtable)[0].as_matrix()[2:]
        for row in arrtable:
            if row[0] in gendict:
                gendict[row[0]]['Start'] = row[2]
                gendict[row[0]]['Stop'] = row[3]
            else:
                if row[1] != 'questionable':
                    print("Can't find %s in local files" % row[0])
    return gendict 
Example #6
Source File: belvaParseXML.py    From Basic-Expression-Lexicon-Variation-Algorithms-BELVA with GNU General Public License v3.0 6 votes vote down vote up
def parseHTMLxpathSearch(http_source, xpathString):
#---------------------------------------------------------------------------------

    return_values = []


    http_source= str(http_source).replace('\x00','')
    try:
        html = lxml.html.fromstring(http_source)

        for data in html.xpath(xpathString):
            return_values.append(etree.tostring(data.content))
            data.clear()

    except:
        pass

    return return_values



#---------------------------------------------------------------------------------
# parse HTML and return value asked 
Example #7
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def create_package_container(self, project, package, meta=None, disable_build=False):
        """
        Creates a package container without any fields in project/package
        :param project: project to create it
        :param package: package name
        :param meta: package metadata
        :param disable_build: should the package be created with build
                              flag disabled
        """
        if not meta:
            meta = '<package name="{}"><title/><description/></package>'
            meta = meta.format(package)

        if disable_build:
            root = ET.fromstring(meta)
            elm = ET.SubElement(root, 'build')
            ET.SubElement(elm, 'disable')
            meta = ET.tostring(root)

        url = self.makeurl(['source', project, package, '_meta'])
        http_PUT(url, data=meta) 
Example #8
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def delete_to_prj(self, act, project):
        """
        Hides Package in project
        :param act: action for delete request
        :param project: project to hide in
        """

        tar_pkg = act.tgt_package
        # need to get the subpackages before we wipe it
        sub_packages = self.get_sub_packages(tar_pkg, project)
        self.create_and_wipe_package(project, tar_pkg)

        for sub_pkg in sub_packages:
            self.create_and_wipe_package(project, sub_pkg)

            # create a link so unselect can find it
            root = ET.Element('link', package=tar_pkg, project=project)
            url = self.makeurl(['source', project, sub_pkg, '_link'])
            http_PUT(url, data=ET.tostring(root))

        return tar_pkg 
Example #9
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def ensure_staging_archs(self, project):
        meta = ET.parse(http_GET(self.project_meta_url(project)))
        repository = meta.find('repository[@name="{}"]'.format(self.cmain_repo))

        changed = False
        for arch in self.cstaging_archs:
            if not repository.xpath('./arch[text()="{}"]'.format(arch)):
                elm = ET.SubElement(repository, 'arch')
                elm.text = arch
                changed = True

        if not changed:
            return

        meta = ET.tostring(meta)
        http_PUT(self.project_meta_url(project), data=meta) 
Example #10
Source File: vpn_connection.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def _format_vpn_connection(vpn_connection, customer_gateways, os_ikepolicies,
                           os_ipsecpolicies, os_ipsec_site_connections,
                           external_ips):
    config_dict = _format_customer_config(
        vpn_connection, customer_gateways, os_ikepolicies, os_ipsecpolicies,
        os_ipsec_site_connections, external_ips)
    config = ec2utils.dict_to_xml(config_dict, 'vpn_connection')
    config.attrib['id'] = vpn_connection['id']
    config_str = etree.tostring(config, xml_declaration=True, encoding='UTF-8',
                                pretty_print=True)
    return {'vpnConnectionId': vpn_connection['id'],
            'vpnGatewayId': vpn_connection['vpn_gateway_id'],
            'customerGatewayId': vpn_connection['customer_gateway_id'],
            'state': 'available',
            'type': 'ipsec.1',
            'routes': [{'destinationCidrBlock': cidr,
                        'state': 'available'}
                       for cidr in vpn_connection['cidrs']],
            'vgwTelemetry': [],
            'options': {'staticRoutesOnly': True},
            'customerGatewayConfiguration': config_str} 
Example #11
Source File: apirequest.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def _render_response(self, response_data, request_id):
        response_el = ec2utils.dict_to_xml(
            {'return': 'true'} if response_data is True else response_data,
            self.action + 'Response')
        response_el.attrib['xmlns'] = ('http://ec2.amazonaws.com/doc/%s/'
                                       % self.version)
        request_id_el = etree.Element('requestId')
        request_id_el.text = request_id
        response_el.insert(0, request_id_el)

        response = etree.tostring(response_el, pretty_print=True)

        # Don't write private key to log
        if self.action != "CreateKeyPair":
            LOG.debug(response)
        else:
            LOG.debug("CreateKeyPair: Return Private Key")

        return response 
Example #12
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def create_package_container(self, project, package, meta=None, disable_build=False):
        """
        Creates a package container without any fields in project/package
        :param project: project to create it
        :param package: package name
        :param meta: package metadata
        :param disable_build: should the package be created with build
                              flag disabled
        """
        if not meta:
            meta = '<package name="{}"><title/><description/></package>'
            meta = meta.format(package)

        if disable_build:
            root = ET.fromstring(meta)
            elm = ET.SubElement(root, 'build')
            ET.SubElement(elm, 'disable')
            meta = ET.tostring(root)

        url = self.makeurl(['source', project, package, '_meta'])
        http_PUT(url, data=meta) 
Example #13
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def delete_to_prj(self, act, project):
        """
        Hides Package in project
        :param act: action for delete request
        :param project: project to hide in
        """

        tar_pkg = act.tgt_package
        # need to get the subpackages before we wipe it
        sub_packages = self.get_sub_packages(tar_pkg, project)
        self.create_and_wipe_package(project, tar_pkg)

        for sub_pkg in sub_packages:
            self.create_and_wipe_package(project, sub_pkg)

            # create a link so unselect can find it
            root = ET.Element('link', package=tar_pkg, project=project)
            url = self.makeurl(['source', project, sub_pkg, '_link'])
            http_PUT(url, data=ET.tostring(root))

        return tar_pkg 
Example #14
Source File: OBSLocal.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, name, project, devel_project=None):
        self.name = name
        self.project = project

        meta = """
            <package project="{1}" name="{0}">
              <title></title>
              <description></description>
            </package>""".format(self.name, self.project.name)

        if devel_project:
            root = ET.fromstring(meta)
            ET.SubElement(root, 'devel', { 'project': devel_project })
            meta = ET.tostring(root)

        url = osc.core.make_meta_url('pkg', (self.project.name, self.name), APIURL)
        osc.core.http_PUT(url, data=meta)
        print('created {}/{}'.format(self.project.name, self.name))
        self.project.add_package(self)

    # delete from instance 
Example #15
Source File: OBSLocal.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def create_group(self, name, users=[]):

        meta = """
        <group>
          <title>{}</title>
        </group>
        """.format(name)

        if len(users):
            root = ET.fromstring(meta)
            persons = ET.SubElement(root, 'person')
            for user in users:
                ET.SubElement(persons, 'person', {'userid': user} )
            meta = ET.tostring(root)

        if not name in self.groups:
            self.groups.append(name)
        url = osc.core.makeurl(APIURL, ['group', name])
        osc.core.http_PUT(url, data=meta) 
Example #16
Source File: bugowner.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def add_bugowner(self, package, owner):
        url = self.makeurl(['source', self.project, package, '_meta'])
        root = ET.fromstring(self.cached_GET(url))
        idname = 'userid' if owner.kind == 'person' else 'groupid'
        # XXX: can't use 'and' here to filter for bugowner too
        exists = root.findall('./{}[@{}="{}"]'.format(owner.kind, idname, owner.name))
        for node in exists:
            if node.get('role') == 'bugowner':
                logger.debug("%s/%s already has %s %s", self.project, package, owner.kind, owner.name)
            return

        node = ET.SubElement(root, owner.kind)
        node.set(idname, owner.name)
        node.set('role', 'bugowner')

        data = ET.tostring(root)
        logger.debug(data)
        self.http_PUT(url, data=data) 
Example #17
Source File: obs_clone.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def entity_clone(apiurl_source, apiurl_target, path, sanitize=None, clone=None, after=None):
    if not hasattr(entity_clone, 'cloned'):
        entity_clone.cloned = []

    if path[0] == 'source' and not project_fence(path[1]):
        # Skip projects outside of fence by marking as cloned.
        if path not in entity_clone.cloned:
            entity_clone.cloned.append(path)

    if path in entity_clone.cloned:
        print('skip {}'.format('/'.join(path)))
        return

    print('clone {}'.format('/'.join(path)))
    entity_clone.cloned.append(path)

    url = makeurl(apiurl_source, path)
    entity = ET.parse(http_GET(url)).getroot()

    if sanitize:
        sanitize(entity)
    if clone:
        clone(apiurl_source, apiurl_target, entity)

    url = makeurl(apiurl_target, path)
    http_PUT(url, data=ET.tostring(entity))

    if after:
        after(apiurl_source, apiurl_target, entity) 
Example #18
Source File: utils.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def clone(self, element):
        """Clone an element."""
        return self.fromstring(self.tostring(element)) 
Example #19
Source File: staging-installcheck.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def check_xml(self, url, state, name):
        check = ET.Element('check')
        if url:
            se = ET.SubElement(check, 'url')
            se.text = url
        se = ET.SubElement(check, 'state')
        se.text = state
        se = ET.SubElement(check, 'name')
        se.text = name
        return ET.tostring(check) 
Example #20
Source File: utils.py    From python-tbk with MIT License 5 votes vote down vote up
def xml_to_string(tree):
    return etree.tostring(tree).decode("utf-8") 
Example #21
Source File: file_utils.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def multibuild_from_glob(destination, pathname):
    root = ET.Element('multibuild')
    for name in sorted(glob.glob(os.path.join(destination, pathname))):
        package = ET.SubElement(root, 'package')
        package.text = os.path.splitext(os.path.basename(name))[0]

    with open(os.path.join(destination, '_multibuild'), 'w+b') as f:
        f.write(ET.tostring(root, pretty_print=True)) 
Example #22
Source File: test_rpcbuilder.py    From genielibs with Apache License 2.0 5 votes vote down vote up
def test_get_payload_identityref_namespace(self):
        """Test get_payload() where the value has its own namespace.

        Seen most commonly with identityrefs.
        """
        cfgs = [{
            "xpath": "/ocif:interfaces/ocif:interface/ocif:config/ocif:type",
            "value": "ianaift:ethernetCsmacd",
        }]
        payload = self.rpcbld.get_payload(cfgs, et.Element("y"))

        xml = et.tostring(payload, encoding='unicode', pretty_print=True)
        self.assertEqual(u"""\
<y>
  <ocif:interfaces xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type" \
xmlns:ocif="http://openconfig.net/yang/interfaces">
    <ocif:interface>
      <ocif:config>
        <ocif:type>ianaift:ethernetCsmacd</ocif:type>
      </ocif:config>
    </ocif:interface>
  </ocif:interfaces>
</y>
""", xml)

        payload_min = self.rpcbld_minimal.get_payload(cfgs, et.Element("y"))
        xml_min = et.tostring(payload_min, encoding="unicode",
                              pretty_print=True)
        self.assertEqual(u"""\
<y>
  <interfaces xmlns="http://openconfig.net/yang/interfaces">
    <interface>
      <config>
        <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">\
ianaift:ethernetCsmacd</type>
      </config>
    </interface>
  </interfaces>
</y>
""", xml_min) 
Example #23
Source File: test_rpcbuilder.py    From genielibs with Apache License 2.0 5 votes vote down vote up
def test_get_payload(self):
        """Test the YSNetconfRPCBuilder.get_payload() instance method."""
        cfgs = [{
            "xpath": "/ocif:interfaces/ocif:interface/ocif:name",
            "value": "eth0",
        }]
        payload = self.rpcbld.get_payload(cfgs, et.Element("x"))

        # get_payload returns an Element tree.
        xml = et.tostring(payload, encoding='unicode', pretty_print=True)
        self.assertEqual("""\
<x>
  <ocif:interfaces xmlns:ocif="http://openconfig.net/yang/interfaces">
    <ocif:interface>
      <ocif:name>eth0</ocif:name>
    </ocif:interface>
  </ocif:interfaces>
</x>
""", xml)

        payload_min = self.rpcbld_minimal.get_payload(cfgs, et.Element('x'))
        # get_payload returns an Element tree.
        xml_min = et.tostring(payload_min, encoding='unicode',
                              pretty_print=True)
        self.assertEqual("""\
<x>
  <interfaces xmlns="http://openconfig.net/yang/interfaces">
    <interface>
      <name>eth0</name>
    </interface>
  </interfaces>
</x>
""", xml_min) 
Example #24
Source File: utils.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def parse(self, html_string, getRefs=None, **kwds):
        """Return the dictionary generated from the given html string;
        getRefs can be used to force the gathering of movies/persons/characters
        references."""
        self.reset()
        if getRefs is not None:
            self.getRefs = getRefs
        else:
            self.getRefs = self._defGetRefs
        # Useful only for the testsuite.
        if not isinstance(html_string, unicode):
            html_string = unicode(html_string, 'latin_1', 'replace')
        html_string = subXMLRefs(html_string)
        # Temporary fix: self.parse_dom must work even for empty strings.
        html_string = self.preprocess_string(html_string)
        html_string = html_string.strip()
        if self.usingModule == 'beautifulsoup':
            # tag attributes like title="&#x22;Family Guy&#x22;" will be
            # converted to title=""Family Guy"" and this confuses BeautifulSoup.
            html_string = html_string.replace('""', '"')
            # Browser-specific escapes create problems to BeautifulSoup.
            html_string = html_string.replace('<!--[if IE]>', '"')
            html_string = html_string.replace('<![endif]-->', '"')
        #print html_string.encode('utf8')
        if html_string:
            dom = self.get_dom(html_string)
            #print self.tostring(dom).encode('utf8')
            try:
                dom = self.preprocess_dom(dom)
            except Exception, e:
                self._logger.error('%s: caught exception preprocessing DOM',
                                    self._cname, exc_info=True)
            if self.getRefs:
                try:
                    self.gather_refs(dom)
                except Exception, e:
                    self._logger.warn('%s: unable to gather refs: %s',
                                    self._cname, exc_info=True) 
Example #25
Source File: obs_clone.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def project_clone(apiurl_source, apiurl_target, project):
    users_clone(apiurl_source, apiurl_target, project)
    project_workaround(project)

    # Write stripped version that does not include repos with path references.
    url = makeurl(apiurl_target, ['source', project.get('name'), '_meta'])
    stripped = deepcopy(project)
    project_references_remove(stripped)
    http_PUT(url, data=ET.tostring(stripped))

    for link in project.xpath('link[@project]'):
        if not project_fence(link.get('project')):
            project.remove(link)
            break

        # Valid reference to project and thus should be cloned.
        path = ['source', link.get('project'), '_meta']
        entity_clone(apiurl_source, apiurl_target, path, clone=project_clone)

    # Clone projects referenced in repository paths.
    for repository in project.findall('repository'):
        for target in repository.xpath('./path') + repository.xpath('./releasetarget'):
            if not project_fence(target.get('project')):
                project.remove(repository)
                break

            # Valid reference to project and thus should be cloned.
            path = ['source', target.get('project'), '_meta']
            entity_clone(apiurl_source, apiurl_target, path, clone=project_clone) 
Example #26
Source File: SamlSigner.py    From ADFSpoof with Apache License 2.0 5 votes vote down vote up
def sign_XML(self, params, id_attribute, algorithm, digest):
        saml_string = string.Template(self.saml_template).substitute(params)
        data = etree.fromstring(saml_string)

        signed_xml = XMLSigner(c14n_algorithm="http://www.w3.org/2001/10/xml-exc-c14n#", signature_algorithm=algorithm, digest_algorithm=digest).sign(data, key=self.key, cert=[self.cert], reference_uri=params.get('AssertionID'), id_attribute=id_attribute)
        signed_saml_string = etree.tostring(signed_xml).replace(b'\n', b'')
        signed_saml_string = re.sub(b'-----(BEGIN|END) CERTIFICATE-----', b'', signed_saml_string)
        return signed_saml_string 
Example #27
Source File: utils.py    From network-programmability-stream with MIT License 5 votes vote down vote up
def prettify_xml(xml: Union[str, etree._Element]) -> str:
    if isinstance(xml, etree._Element):
        result = etree.tostring(xml, pretty_print=True).decode("utf-8")
    else:
        result = parseString(xml).toprettyxml("  ")
    return result 
Example #28
Source File: nc_dial_in_subscribe.py    From network-programmability-stream with MIT License 5 votes vote down vote up
def print_callback(self, notif):
        data = etree.tostring(notif.datastore_ele, pretty_print=True).decode("utf-8")
        result = (
            f"-->>\n"
            f"Event time      : {notif.event_time}\n"
            f"Subscription Id : {notif.subscription_id}\n"
            f"Type            : {notif.type}\n"
            f"Data            :\n{data}\n"
            f"<<--"
        )
        print(result) 
Example #29
Source File: txtml.py    From py-edgar with GNU General Public License v3.0 5 votes vote down vote up
def to_xml(cls, doc):
    return html.unescape(tostring(doc).decode("utf8")) 
Example #30
Source File: utils.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def tostring(self, element):
        """Convert the element to a string."""
        if isinstance(element, (unicode, str)):
            return unicode(element)
        else:
            try:
                return self._tostring(element, encoding=unicode)
            except Exception, e:
                self._logger.error('%s: unable to convert to string',
                                    self._cname, exc_info=True)
                return u''