Python lxml.etree.Element() Examples

The following are 30 code examples of lxml.etree.Element(). 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: search.py    From yang-explorer with Apache License 2.0 7 votes vote down vote up
def search(username, query):
    """
    Search query text in user modules
    Args:
        username: Request username
        query: Search String

    Returns: An XML object with result XPATHs
    """
    logging.debug('Searching query %s in user (%s) modules' % (query, username))
    response = ET.Element('result')

    modulenames = ModuleAdmin.get_modulelist(username)
    for module in modulenames:
        result = search_module(username, module, query)
        for xpath in result:
            path = ET.Element('path')
            path.set('module', module)
            path.text = xpath
            response.append(path)

    return True, response 
Example #2
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 #3
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 #4
Source File: profile_view.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def profile_handler(request):
    """ HTTP request handler for profile request """

    profiles = ET.Element('profiles')
    if request.user.is_authenticated():
        uid = request.user.id
        logging.debug("User Authenticated (%s)" % request.user.username)
        entries = DeviceProfile.objects.filter(Q(user=uid) | Q(shared=True))
        for e in entries:
            profile = _build_device_profile(e)
            profiles.append(profile)

        entries = Collection.objects.all()
        for e in entries:
            profile = _build_collection_profile(e)
            profiles.append(profile)
    return HttpResponse(Response.success('profile', 'ok', xml=profiles)) 
Example #5
Source File: admin.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def get_modules(username):
        """
        Return list of modules available to user + subscribed
        """
        logger.debug("ModuleAdmin.get_modules: enter")

        modules = ET.Element('modulelist')
        user = User.objects.filter(username=username)
        mlist = list()
        for _file in glob.glob(os.path.join(ServerSettings.yang_path(username), '*.yang')):
            mlist.append(os.path.basename(_file))

        mlist.sort()
        for fname in mlist:
            module = ET.Element('module')
            module.text = os.path.basename(fname)
            name = module.text.split('.yang')[0]
            if UserProfile.objects.filter(user=user, module=name).exists():
                module.set('subscribed', 'true')
            modules.append(module)

        logger.info("ModuleAdmin.get_modules: returning (%d) modules .. exit" % len(modules))
        return modules 
Example #6
Source File: adapter.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def run_restconf(username, device, msg):
        """ Execute Restconf request """

        session = RestClient(device)
        if session is None:
            reply = ET.Element('reply')
            reply.text = 'Could not create session for %s' % protocol
            return reply

        if isinstance(msg, str):
            msg = json.loads(msg)

        url = 'http://' + device['host'] + ':' + str(device['port'])
        if msg is None or msg  == '':
            return session.get_capability(url)

        msg['url'] = url + msg['url']
        return session.run(msg) 
Example #7
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 #8
Source File: collection.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def list():
        """ get list of all collection entries """

        cols_elem = ET.Element('collections')
        for col in Col.objects.all():
            path = os.path.join('data', 'collections', col.name)
            if not os.path.exists(path):
                logging.error('Collection has inconstancy : %s !!' % col.name)
                continue
            files = glob.glob(os.path.join(path, '*'))
            for _file in files:
                payload = ET.parse(_file)
                for child in payload.getroot():
                    if child.tag == 'metadata':
                        cols_elem.append(child)
        return cols_elem 
Example #9
Source File: Packets.py    From Timeline with GNU General Public License v3.0 6 votes vote down vote up
def buildXMLNodes(self, element, node):
		if type(node) is str or type(node) is int or type(node) is bool:
			node = str(node)
			element.text = XML.CDATA(node)
		else:
			for k, v in node.iteritems():
				if type(v) is dict:
					# serialize the child dictionary
					child = XML.Element(k)
					self.buildXMLNodes(child, v)
					element.append(child)
				elif type(v) is list:
					if k[-1] == 's':
						name = k[:-1]
					else:
						name = k

					for item in v:
						child = XML.Element(name)
						self.buildXMLNodes(child, item)
						element.append(child)
				else:
					# add attributes to the current element
					element.set(k, unicode(v)) 
Example #10
Source File: xbundle.py    From edx2bigquery with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, keep_urls=False, force_studio_format=False, skip_hidden=False, keep_studio_urls=False):
        '''
        if keep_urls=True then the original url_name attributes are kept upon import and export,
        if nonrandom (ie non-Studio).
        
        if keep_studio_urls=True and keep_urls=True, then keep random urls.
        '''
        self.course = etree.Element('course')
        self.metadata = etree.Element('metadata')
        self.urlnames = []
        self.xml = None				# only used if XML xbundle file was read in
        self.keep_urls = keep_urls
        self.force_studio_format = force_studio_format	# sequential must be followed by vertical in export
        self.skip_hidden = skip_hidden
        self.keep_studio_urls = keep_studio_urls
        return

        
    #----------------------------------------
    # creation by parts 
Example #11
Source File: profile_view.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def _build_proto(proto, addr, port, uname, pwd):
    """ Build one proto xml instance """

    transport = ET.Element('transport')
    transport.set('type', proto)

    elem = ET.Element('address')
    elem.text = addr
    transport.append(elem)

    elem = ET.Element('port')
    elem.text = port
    transport.append(elem)

    elem = ET.Element('username')
    elem.text = uname
    transport.append(elem)

    elem = ET.Element('password')
    elem.text = pwd
    transport.append(elem)

    return transport 
Example #12
Source File: runner.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def get_capability(self):
        """ Returns device capabilities """

        logging.debug('get_capability ..')
        reply = ET.Element('reply')
        if not self.connect():
            reply.text = 'NetConf Session could not be established {%s}' % str(self)
            return reply
        self.disconnect()
        if self.handle.server_capabilities:
            caps = sorted(self.handle.server_capabilities)
            reply.text = '\n'.join((c for c in caps if c.startswith('urn:ietf:params:netconf:')))
            reply.text += '\n\n'
            reply.text += '\n'.join((c for c in caps if not c.startswith('urn:ietf:params:netconf:')))
            logging.info('Received device capabilities ..')
        return reply 
Example #13
Source File: xbundle.py    From edx2bigquery with GNU General Public License v2.0 6 votes vote down vote up
def import_metadata_from_directory(self, dir):
        # load policies
        # print "ppath = ", (path(dir) / 'policies/*')
        for pdir in glob.glob(path(dir) / 'policies/*'):
            # print "pdir=",pdir
            policies = etree.Element('policies')
            policies.set('semester',os.path.basename(pdir))
            for fn in glob.glob(path(pdir) / '*.json'):
                x = etree.SubElement(policies,os.path.basename(fn).replace('_','').replace('.json',''))
                x.text = open(fn).read()
            self.add_policies(policies)
        
        # load about files
        for afn in glob.glob(dir / 'about/*'):
            try:
                self.add_about_file(os.path.basename(afn), open(afn).read())
            except Exception as err:
                print "Oops, failed to add file %s, error=%s" % (afn, err) 
Example #14
Source File: xbundle.py    From edx2bigquery with GNU General Public License v2.0 6 votes vote down vote up
def export_to_directory(self, exdir='./'):
        '''
        Export xbundle to edX xml directory
        First insert all the intermediate descriptors needed.
        Do about and XML separately.
        '''
        coursex = etree.Element('course')
        semester = self.course.get('semester')
        coursex.set('url_name',semester)
        coursex.set('org',self.course.get('org'))
        coursex.set('course',self.course.get('course'))

        self.export = self.make_descriptor(self.course, semester)
        self.export.append(self.course)
        self.add_descriptors(self.course)

        # print self.pp_xml(self.export)

        self.dir = self.mkdir(path(exdir) / self.course_id())
        self.export_meta_to_directory()
        self.export_xml_to_directory(self.export[0])

        # write out top-level course.xml

        open(self.dir/'course.xml','w').write(self.pp_xml(coursex)) 
Example #15
Source File: xbundle.py    From edx2bigquery with GNU General Public License v2.0 6 votes vote down vote up
def add_descriptors(self, xml, parent=''):
        '''
        Recursively walk through self.course and add descriptors
        A descriptor is an intermediate tag, which points to content
        via a url_name.  These are used by edX to simplify loading
        of course content.
        '''
        for elem in xml:
            if self.force_studio_format:
                if xml.tag=='sequential' and not elem.tag=='vertical':	# studio needs seq -> vert -> other
                    # move child into vertical
                    vert = etree.Element('vertical')
                    elem.addprevious(vert)
                    vert.append(elem)
                    elem = vert			# continue processing on the vertical
            if elem.tag in self.DescriptorTags and not elem.get('url_name',''):
                desc = self.make_descriptor(elem, parent=parent)
                elem.addprevious(desc)
                desc.append(elem)		# move descriptor to become new parent of elem
                self.add_descriptors(elem, desc.get('url_name'))	# recurse

#-----------------------------------------------------------------------------
# tests 
Example #16
Source File: teamofrivals.py    From psychsim with MIT License 6 votes vote down vote up
def mapSave(regions,filename):
    """
    Saves a region map to an XML file
    """
    root = ET.Element('map')
    for name,table in regions.items():
        node = ET.SubElement(root,'region')
        node.set('name',name)
        if table.has_key('value'): node.set('value',str(table['value']))
        if table.has_key('occupants'): node.set('occupants',str(table['occupants']))
        node.set('owner',str(table['owner']))
        for neighbor in table['neighbors']:
            subnode = ET.SubElement(node,'neighbor')
            subnode.set('name',neighbor)
    tree = ET.ElementTree(root)
    tree.write(filename,pretty_print=True)
    return tree 
Example #17
Source File: parse.py    From rets with MIT License 6 votes vote down vote up
def parse_search(response: Response) -> SearchResult:
    try:
        elem = parse_xml(response)
    except RetsApiError as e:
        if e.reply_code == 20201:  # No records found
            return SearchResult(0, False, ())
        raise

    count_elem = elem.find('COUNT')
    if count_elem is not None:
        count = int(count_elem.get('Records'))
    else:
        count = None

    try:
        data = tuple(_parse_data(elem))
    except RetsParseError:
        data = None

    return SearchResult(
        count=count,
        # python xml.etree.ElementTree.Element objects are always considered false-y
        max_rows=elem.find('MAXROWS') is not None,
        data=data,
    ) 
Example #18
Source File: kbinxml.py    From kbinxml with MIT License 6 votes vote down vote up
def mem_size(self):
        '''used when allocating memory ingame'''

        data_len = self._data_mem_size
        node_count = len(list(self.xml_doc.iter(tag=etree.Element)))

        if self.compressed:
            size = 52 * node_count + data_len + 630
        else:
            tags_len = 0
            for e in self.xml_doc.iter(tag=etree.Element):
                e_len = max(len(e.tag), 8)
                e_len = (e_len + 3) & ~3
                tags_len += e_len

            size = 56 * node_count + data_len + 630 + tags_len

        # debugging
        #print('nodes:{} ({}) data:{} ({})'.format(node_count,hex(node_count), data_len, hex(data_len)))

        return (size + 8) & ~7 
Example #19
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def add_ignored_request(self, request_id, comment):
        url = self.makeurl(['staging', self.project, 'excluded_requests'])
        root = ET.Element('excluded_requests')
        req = ET.SubElement(root, 'request', { 'id': str(request_id), 'description': comment })
        http_POST(url, data=ET.tostring(root)) 
Example #20
Source File: core.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def attribute_value_save(apiurl, project, name, value, namespace='OSRT', package=None):
    root = ET.Element('attributes')

    attribute = ET.SubElement(root, 'attribute')
    attribute.set('namespace', namespace)
    attribute.set('name', name)

    ET.SubElement(attribute, 'value').text = value

    # The OBS API of attributes is super strange, POST to update.
    url = makeurl(apiurl, list(filter(None, ['source', project, package, '_attribute'])))
    http_POST(url, data=ET.tostring(root)) 
Example #21
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def del_ignored_request(self, request_id):
        url = self.makeurl(['staging', self.project, 'excluded_requests'])
        root = ET.Element('excluded_requests')
        req = ET.SubElement(root, 'request', { 'id': str(request_id) })
        http_DELETE(url, data=ET.tostring(root)) 
Example #22
Source File: core.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def create_delete_request(apiurl, target_project, target_package=None, message=None):
    if not message:
        message = message_suffix('created')

    request = ETL.Element('request')

    state = ETL.Element('state')
    state.set('name', 'new')
    request.append(state)

    description = ETL.Element('description')
    description.text = message
    request.append(description)

    action = ETL.Element('action')
    action.set('type', 'delete')
    request.append(action)

    target = ETL.Element('target')
    target.set('project', target_project)
    if target_package:
        target.set('package', target_package)
    action.append(target)

    url = makeurl(apiurl, ['request'], {'cmd': 'create'})
    root = ETL.parse(http_POST(url, data=ETL.tostring(request))).getroot()
    return root.get('id')

# Should exist within osc.core like create_submit_request(), but rather it was
# duplicated in osc.commandline. 
Example #23
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def add_required_check(self, project, check):
        root = ET.Element('required_checks')
        name = ET.SubElement(root, 'name')
        name.text = check

        meta = ET.parse(http_GET(self.project_meta_url(project)))
        repository = meta.find('repository[@name="{}"]'.format(self.cmain_repo))

        for arch_element in repository.findall('arch'):
            architecture = arch_element.text
            url = self.makeurl(['status_reports', 'built_repositories', project,
                                self.cmain_repo, architecture, 'required_checks'])
            http_POST(url, data=ET.tostring(root)) 
Example #24
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def submit_to_prj(self, act, project):
        """
        Links sources from request to project
        :param act: action for submit request
        :param project: project to link into
        """

        src_prj = act.src_project
        src_rev = act.src_rev
        src_pkg = act.src_package
        tar_pkg = act.tgt_package

        # If adi project, check for baselibs.conf in all specs to catch both
        # dynamically generated and static baselibs.conf.
        if self.is_adi_project(project):
            baselibs = False
            specfile = source_file_load(self.apiurl, src_prj, src_pkg, '{}.spec'.format(src_pkg), src_rev)
            if specfile and 'baselibs.conf' in specfile:
                baselibs = True
        else:
            baselibs = None

        for sub_pkg in self.get_sub_packages(tar_pkg, project):
            self.create_package_container(project, sub_pkg)

            root = ET.Element('link', package=tar_pkg, cicount='copy')
            url = self.makeurl(['source', project, sub_pkg, '_link'])
            http_PUT(url, data=ET.tostring(root))

            if baselibs is False:
                specfile = source_file_load(self.apiurl, src_prj, src_pkg, '{}.spec'.format(sub_pkg), src_rev)
                if specfile and 'baselibs.conf' in specfile:
                    baselibs = True

        if baselibs:
            # Adi package has baselibs.conf, ensure all staging archs are enabled.
            self.ensure_staging_archs(project)

        return tar_pkg 
Example #25
Source File: test_etree_check_command_transform.py    From python-gvm with GNU General Public License v3.0 5 votes vote down vote up
def test_success_response(self):
        transform = EtreeCheckCommandTransform()

        root = etree.Element('foo_response')
        root.set('status', '200')

        response = etree.tostring(root).decode('utf-8')

        result = transform(response)

        self.assertTrue(etree.iselement(result))
        self.assertEqual(result.tag, 'foo_response')
        self.assertEqual(result.get('status'), '200') 
Example #26
Source File: rabbit-openqa.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def openqa_check_xml(self, url, state, name):
        check = ET.Element('check')
        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 #27
Source File: core.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def create_change_devel_request(apiurl, source_project, source_package,
                                target_project, target_package=None, message=None):
    if not message:
        message = message_suffix('created')

    request = ETL.Element('request')

    state = ETL.Element('state')
    state.set('name', 'new')
    request.append(state)

    description = ETL.Element('description')
    description.text = message
    request.append(description)

    action = ETL.Element('action')
    action.set('type', 'change_devel')
    request.append(action)

    source = ETL.Element('source')
    source.set('project', source_project)
    source.set('package', source_package)
    action.append(source)

    target = ETL.Element('target')
    target.set('project', target_project)
    target.set('package', target_package)
    action.append(target)

    url = makeurl(apiurl, ['request'], {'cmd': 'create'})
    root = ETL.parse(http_POST(url, data=ETL.tostring(request))).getroot()
    return root.get('id') 
Example #28
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def add_ignored_request(self, request_id, comment):
        url = self.makeurl(['staging', self.project, 'excluded_requests'])
        root = ET.Element('excluded_requests')
        req = ET.SubElement(root, 'request', { 'id': str(request_id), 'description': comment })
        http_POST(url, data=ET.tostring(root)) 
Example #29
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def submit_to_prj(self, act, project):
        """
        Links sources from request to project
        :param act: action for submit request
        :param project: project to link into
        """

        src_prj = act.src_project
        src_rev = act.src_rev
        src_pkg = act.src_package
        tar_pkg = act.tgt_package

        # If adi project, check for baselibs.conf in all specs to catch both
        # dynamically generated and static baselibs.conf.
        if self.is_adi_project(project):
            baselibs = False
            specfile = source_file_load(self.apiurl, src_prj, src_pkg, '{}.spec'.format(src_pkg), src_rev)
            if specfile and 'baselibs.conf' in specfile:
                baselibs = True
        else:
            baselibs = None

        for sub_pkg in self.get_sub_packages(tar_pkg, project):
            self.create_package_container(project, sub_pkg)

            root = ET.Element('link', package=tar_pkg, cicount='copy')
            url = self.makeurl(['source', project, sub_pkg, '_link'])
            http_PUT(url, data=ET.tostring(root))

            if baselibs is False:
                specfile = source_file_load(self.apiurl, src_prj, src_pkg, '{}.spec'.format(sub_pkg), src_rev)
                if specfile and 'baselibs.conf' in specfile:
                    baselibs = True

        if baselibs:
            # Adi package has baselibs.conf, ensure all staging archs are enabled.
            self.ensure_staging_archs(project)

        return tar_pkg 
Example #30
Source File: stagingapi.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def update_adi_frozenlinks(self, name, src_prj):
        xpath = {
            'package': "@project='%s' and devel/@project='%s'" % (self.project, src_prj),
        }
        collection = search(self.apiurl, **xpath)['package']

        # all packages had matched devel project defined
        pkglist = [p.attrib['name'] for p in collection.findall('package')]

        flink = ET.Element('frozenlinks')
        fl_prj = ET.SubElement(flink, 'frozenlink', {'project': self.project})

        project_sourceinfo = ET.fromstring(show_project_sourceinfo(self.apiurl, self.project, True))
        for si in project_sourceinfo.findall('sourceinfo'):
            pkg = si.get('package')
            if pkg in pkglist:
                ET.SubElement(fl_prj, 'package', {'name': pkg, 'srcmd5': si.get('srcmd5'), 'vrev': si.get('vrev')})
            # check multiple spec ie. sub-package
            for linked in si.findall('linked'):
                if linked.get('package') in pkglist:
                    ET.SubElement(fl_prj, 'package', {'name': pkg, 'srcmd5': si.get('lsrcmd5'), 'vrev': si.get('vrev')})

        # commit frozenlinks
        url = self.makeurl(['source', name, '_project', '_frozenlinks'], {'meta': '1'})
        l = ET.tostring(flink)
        http_PUT(url, data=l)