Python lxml.etree.SubElement() Examples
The following are 30
code examples of lxml.etree.SubElement().
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: dataset.py From calamari with Apache License 2.0 | 6 votes |
def store_text(self, sentence, sample, output_dir, extension): ns = sample['ns'] line = sample['xml_element'] textequivxml = line.find('./ns:TextEquiv[@index="{}"]'.format(self.text_index), namespaces=ns) if textequivxml is None: textequivxml = etree.SubElement(line, "TextEquiv", attrib={"index": str(self.text_index)}) u_xml = textequivxml.find('./ns:Unicode', namespaces=ns) if u_xml is None: u_xml = etree.SubElement(textequivxml, "Unicode") u_xml.text = sentence # check if page can be stored, this requires that (standard in prediction) the pages are passed sequentially if self._last_page_id != sample['page_id']: if self._last_page_id: self._store_page(extension, self._last_page_id) self._last_page_id = sample['page_id']
Example #2
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 6 votes |
def serialize_image(ctx, document, elem, root): """Serialize image element. This is not abstract enough. """ _img = etree.SubElement(root, 'img') # make path configurable if elem.rid in document.relationships[ctx.options['relationship']]: img_src = document.relationships[ctx.options['relationship']][elem.rid].get('target', '') img_name, img_extension = os.path.splitext(img_src) _img.set('src', 'static/{}{}'.format(elem.rid, img_extension)) fire_hooks(ctx, document, elem, _img, ctx.get_hook('img')) return root
Example #3
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 6 votes |
def serialize_comment(ctx, document, el, root): "Serializes comment." # Check if option is turned on if el.comment_type == 'end': ctx.opened_comments.remove(el.cid) else: if el.comment_type != 'reference': ctx.opened_comments.append(el.cid) if ctx.options['comment_span']: link = etree.SubElement(root, 'a') link.set('href', '#') link.set('class', 'comment-link') link.set('id', 'comment-id-' + el.cid) link.text = '' fire_hooks(ctx, document, el, link, ctx.get_hook('comment')) return root
Example #4
Source File: stagingapi.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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 #5
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 6 votes |
def serialize_footnote(ctx, document, el, root): "Serializes footnotes." footnote_num = el.rid if el.rid not in ctx.footnote_list: ctx.footnote_id += 1 ctx.footnote_list[el.rid] = ctx.footnote_id footnote_num = ctx.footnote_list[el.rid] note = etree.SubElement(root, 'sup') link = etree.SubElement(note, 'a') link.set('href', '#') link.text = u'{}'.format(footnote_num) fire_hooks(ctx, document, el, note, ctx.get_hook('footnote')) return root
Example #6
Source File: stagingapi.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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 #7
Source File: stagingapi.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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: xml_serialiser.py From acvtool with Apache License 2.0 | 6 votes |
def add_xml_class(self, package, smali_class): class_insns_covered = 0 class_insns_missed = 0 xml_class = SubElement(package, "class") xml_class.set("name", smali_class.name[1:-1]) class_instructions_added = 0 for m in smali_class.methods: if (m.cover_code > -1): # not abstract and not native method xml_method = self.create_xml_method(xml_class, m) if Granularity.is_instruction(self.granularity): lines_covered = m.covered() lines_missed = m.not_covered() self.add_xml_insn_counter(xml_method, lines_covered, lines_missed, "INSTRUCTION") class_insns_covered += lines_covered class_insns_missed += lines_missed class_instructions_added += 1 if Granularity.is_method(self.granularity): self.add_xml_insn_counter(xml_method, int(m.called), 1-int(m.called), "METHOD") methods_covered = smali_class.mtds_covered() methods_missed = smali_class.mtds_coverable() - methods_covered if class_instructions_added: self.add_xml_insn_counter(xml_class, class_insns_covered, class_insns_missed, "INSTRUCTION") self.add_xml_insn_counter(xml_class, methods_covered, methods_missed, "METHOD")
Example #9
Source File: ruleset.py From gmail-yaml-filters with MIT License | 6 votes |
def ruleset_to_etree(ruleset): xml = etree.Element('feed', nsmap={ None: 'http://www.w3.org/2005/Atom', 'apps': 'http://schemas.google.com/apps/2006', }) etree.SubElement(xml, 'title').text = 'Mail Filters' for rule in sorted(ruleset): if not rule.publishable: continue entry = etree.SubElement(xml, 'entry') etree.SubElement(entry, 'category', term='filter') etree.SubElement(entry, 'title').text = 'Mail Filter' etree.SubElement(entry, 'id').text = 'tag:mail.google.com,2008:filter:{0}'.format(abs(hash(rule))) etree.SubElement(entry, 'updated').text = datetime.now().replace(microsecond=0).isoformat() + 'Z' etree.SubElement(entry, 'content') for construct in sorted(six.itervalues(rule.flatten()), key=attrgetter('key')): etree.SubElement( entry, '{http://schemas.google.com/apps/2006}property', name=construct.key, value=six.text_type(construct.value), ) return xml
Example #10
Source File: anno_coco2voc.py From Dataset_to_VOC_converter with MIT License | 6 votes |
def keypoints2xml_object(anno, xmltree, keypoints_dict, bbox_type='xyxy'): assert bbox_type in ['xyxy', 'xywh'] if bbox_type == 'xyxy': xmin, ymin, w, h = anno['bbox'] xmax = xmin+w ymax = ymin+h else: xmin, ymin, xmax, ymax = anno['bbox'] key_object = etree.SubElement(xmltree, "object") etree.SubElement(key_object, "name").text = anno['category_id'] bndbox = etree.SubElement(key_object, "bndbox") etree.SubElement(bndbox, "xmin").text = str(xmin) etree.SubElement(bndbox, "ymin").text = str(ymin) etree.SubElement(bndbox, "xmax").text = str(xmax) etree.SubElement(bndbox, "ymax").text = str(ymax) etree.SubElement(key_object, "difficult").text = '0' keypoints = etree.SubElement(key_object, "keypoints") for i in range(0, len(keypoints_dict)): keypoint = etree.SubElement(keypoints, keypoints_dict[i+1]) etree.SubElement(keypoint, "x").text = str(anno['keypoints'][i*3]) etree.SubElement(keypoint, "y").text = str(anno['keypoints'][i*3+1]) etree.SubElement(keypoint, "v").text = str(anno['keypoints'][i*3+2]) return xmltree
Example #11
Source File: stagingapi.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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 #12
Source File: xmloperations.py From nsxramlclient with MIT License | 6 votes |
def parse_dict(xml_root_object, dict_to_parse): for subitem in dict_to_parse.items(): # subitem is now a tuple of key, value in the dict xml_subitem_name = subitem[0] if type(subitem[1]) in (str, int, None): if subitem[0][0] == '@': xml_root_object.set(subitem[0][1:], str(subitem[1])) else: xml_subitem = et.SubElement(xml_root_object, xml_subitem_name) xml_subitem.text = str(subitem[1]) elif type(subitem[1]) is dict: xml_subitem = et.SubElement(xml_root_object, xml_subitem_name) parse_dict(xml_subitem, subitem[1]) elif type(subitem[1]) is list: for item in subitem[1]: if type(item) is dict: xml_subitem = et.SubElement(xml_root_object, xml_subitem_name) parse_dict(xml_subitem, item) elif type(item) is str: xml_subitem = et.SubElement(xml_root_object, xml_subitem_name) xml_subitem.text = str(item)
Example #13
Source File: bugowner.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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 #14
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 6 votes |
def serialize_endnote(ctx, document, el, root): "Serializes endnotes." footnote_num = el.rid if el.rid not in ctx.endnote_list: ctx.endnote_id += 1 ctx.endnote_list[el.rid] = ctx.endnote_id footnote_num = ctx.endnote_list[el.rid] note = etree.SubElement(root, 'sup') link = etree.SubElement(note, 'a') link.set('href', '#') link.text = u'{}'.format(footnote_num) fire_hooks(ctx, document, el, note, ctx.get_hook('endnote')) return root
Example #15
Source File: soap.py From shareplum with MIT License | 6 votes |
def __init__(self, command): # type: (str) -> None self.envelope = None self.command = command self.request = None self.updates = None self.batch = None # HEADER GLOBALS SOAPENV_NAMESPACE = "http://schemas.xmlsoap.org/soap/envelope/" SOAPENV = "{%s}" % SOAPENV_NAMESPACE ns0_NAMESPACE = "http://schemas.xmlsoap.org/soap/envelope/" ns1_NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/" xsi_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance" NSMAP = {"SOAP-ENV": SOAPENV_NAMESPACE, "ns0": ns0_NAMESPACE, "ns1": ns1_NAMESPACE, "xsi": xsi_NAMESPACE} # Create Header self.envelope = etree.Element(SOAPENV + "Envelope", nsmap=NSMAP) HEADER = etree.SubElement(self.envelope, "{http://schemas.xmlsoap.org/soap/envelope/}Body") # Create Command self.command = etree.SubElement(HEADER, "{http://schemas.microsoft.com/sharepoint/soap/}" + command) self.start_str = b"""<?xml version="1.0" encoding="utf-8"?>"""
Example #16
Source File: phoneset2ipamap.py From Living-Audio-Dataset with Apache License 2.0 | 6 votes |
def create_output(phone_map,output_file,nuclei=[],add_nuclei=False): root = ET.Element("ipa_mapping") tree = ET.ElementTree(root) for ipa, phone in phone_map.items(): mapping = ET.SubElement(root, "map") mapping.attrib["pron"] = phone mapping.attrib["ipa"] = ipa if add_nuclei: if phone in nuclei: mapping.attrib["nucleus"] = "true" else: mapping.attrib["nucleus"] = "false" mapping_output = open(output_file,"wb") tree.write( mapping_output,pretty_print=True, xml_declaration=True,encoding="utf-8")
Example #17
Source File: wiktionary2lex.py From Living-Audio-Dataset with Apache License 2.0 | 6 votes |
def output2lex(output,filename): lexicon_output = open(filename,"wb") lexicon = ET.Element("lexicon") tree = ET.ElementTree(lexicon) for list_item in output: lex_entry = ET.SubElement(lexicon,"lex") if list_item["pron"] is not None: lex_entry.attrib["ipa"]=list_item["pron"] if list_item["x-sampa"] is not None: lex_entry.attrib["x-sampa"]=list_item["x-sampa"] lex_entry.text=list_item["word"] tree.write( lexicon_output,pretty_print=True, xml_declaration=True,encoding="utf-8")
Example #18
Source File: teamofrivals.py From psychsim with MIT License | 6 votes |
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 #19
Source File: xbundle.py From edx2bigquery with GNU General Public License v2.0 | 6 votes |
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 #20
Source File: ec2utils.py From ec2-api with Apache License 2.0 | 6 votes |
def _render_data(el, data): if isinstance(data, list): for item in data: sub_el = etree.SubElement(el, 'item') _render_data(sub_el, item) elif isinstance(data, dict): _render_dict(el, data) elif hasattr(data, '__dict__'): _render_dict(el, data.__dict__) elif isinstance(data, bool): el.text = str(data).lower() elif isinstance(data, datetime.datetime): el.text = _database_to_isoformat(data) elif isinstance(data, six.binary_type): el.text = data.decode("utf-8") elif data is not None: el.text = six.text_type(data)
Example #21
Source File: OBSLocal.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
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 #22
Source File: sample-xml-skeleton.py From pyang with ISC License | 6 votes |
def sample_element(self, node, parent, module, path): """Create element under `parent`. Declare new namespace if necessary. """ if path is None: return parent, module, None elif not path: # GO ON pass else: if node.arg == path[0]: path = path[1:] else: return parent, module, None res = etree.SubElement(parent, node.arg) mm = node.main_module() if mm != module: res.attrib["xmlns"] = self.ns_uri[mm] module = mm return res, module, path
Example #23
Source File: soap.py From shareplum with MIT License | 6 votes |
def add_query(self, pyquery): # type: (Dict) -> None query = etree.SubElement(self.command, "{http://schemas.microsoft.com/sharepoint/soap/}query") Query = etree.SubElement(query, "Query") if "OrderBy" in pyquery: order = etree.SubElement(Query, "OrderBy") for field in pyquery["OrderBy"]: fieldref = etree.SubElement(order, "FieldRef") if type(field) == tuple: fieldref.set("Name", field[0]) if field[1] == "DESCENDING": fieldref.set("Ascending", "FALSE") else: fieldref.set("Name", field) if "GroupBy" in pyquery: order = etree.SubElement(Query, "GroupBy") for field in pyquery["GroupBy"]: fieldref = etree.SubElement(order, "FieldRef") fieldref.set("Name", field) if "Where" in pyquery: Query.append(pyquery["Where"])
Example #24
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 5 votes |
def serialize_link(ctx, document, elem, root): """Serilaze link element. This works only for external links at the moment. """ _a = etree.SubElement(root, 'a') for el in elem.elements: _ser = ctx.get_serializer(el) if _ser: _td = _ser(ctx, document, el, _a) else: if isinstance(el, doc.Text): children = list(_a) if len(children) == 0: _text = _a.text or u'' _a.text = u'{}{}'.format(_text, el.value()) else: _text = children[-1].tail or u'' children[-1].tail = u'{}{}'.format(_text, el.value()) if elem.rid in document.relationships[ctx.options['relationship']]: _a.set('href', document.relationships[ctx.options['relationship']][elem.rid].get('target', '')) fire_hooks(ctx, document, elem, _a, ctx.get_hook('a')) return root
Example #25
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 5 votes |
def serialize_break(ctx, document, elem, root): "Serialize break element." if elem.break_type == u'textWrapping': _div = etree.SubElement(root, 'br') else: _div = etree.SubElement(root, 'span') if ctx.options['embed_styles']: _div.set('style', 'page-break-after: always;') fire_hooks(ctx, document, elem, _div, ctx.get_hook('page_break')) return root
Example #26
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 5 votes |
def serialize_math(ctx, document, elem, root): """Serialize math element. Math objects are not supported at the moment. This is wht we only show error message. """ _div = etree.SubElement(root, 'span') if ctx.options['embed_styles']: _div.set('style', 'border: 1px solid red') _div.text = 'We do not support Math blocks at the moment.' fire_hooks(ctx, document, elem, _div, ctx.get_hook('math')) return root
Example #27
Source File: serialize.py From python-ooxml with GNU Affero General Public License v3.0 | 5 votes |
def serialize_smarttag(ctx, document, el, root): "Serializes smarttag." if ctx.options['smarttag_span']: _span = etree.SubElement(root, 'span', {'class': 'smarttag', 'data-smarttag-element': el.element}) else: _span = root for elem in el.elements: _ser = ctx.get_serializer(elem) if _ser: _td = _ser(ctx, document, elem, _span) else: if isinstance(elem, doc.Text): children = list(_span) if len(children) == 0: _text = _span.text or u'' _span.text = u'{}{}'.format(_text, elem.text) else: _text = children[-1].tail or u'' children[-1].tail = u'{}{}'.format(_text, elem.text) fire_hooks(ctx, document, el, _span, ctx.get_hook('smarttag')) return root
Example #28
Source File: random-report-gen.gmp.py From gvm-tools with GNU General Public License v3.0 | 5 votes |
def generate_result_elem(vulns, host_ip, host_port, host_asset, host_name): result_elem = e.Element('result', {'id': generate_uuid()}) e.SubElement(result_elem, 'name').text = "a_result" + id_generator() own = e.SubElement(result_elem, 'owner') e.SubElement(own, 'name').text = id_generator() elem = e.Element('modification_time') e.SubElement(result_elem, 'modification_time').text = ( time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time()))[:-2] + ':00' ) # Hell of a Timeformat :D e.SubElement(result_elem, 'comment').text = '' e.SubElement(result_elem, 'creation_time').text = ( time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time() - 20))[ :-2 ] + ':00' ) host_elem = e.Element('host') host_elem.text = host_ip e.SubElement(host_elem, 'asset', {'asset_id': host_asset}).text = '' e.SubElement(host_elem, 'hostname').text = host_name result_elem.append(host_elem) port_elem = e.Element('port') port_elem.text = host_port result_elem.append(port_elem) nvt = vulns[randrange(len(vulns))] e.SubElement(result_elem, 'severity').text = nvt['severity'] nvt_elem = e.Element('nvt', {'oid': nvt['oid']}) result_elem.append(nvt_elem) e.SubElement(result_elem, 'notes').text = 'TestNotes' result_elem.append(elem) return result_elem, nvt['oid'], nvt['severity']
Example #29
Source File: random-report-gen.gmp.py From gvm-tools with GNU General Public License v3.0 | 5 votes |
def generate_report_elem(task, **kwargs): rep_format_id = 'a994b278-1f62-11e1-96ac-406186ea4fc5' rep_id = generate_uuid() outer_report_elem = e.Element( 'report', attrib={ 'extension': 'xml', 'id': rep_id, 'format_id': rep_format_id, 'content_type': 'text/xml', }, ) owner_elem = e.SubElement(outer_report_elem, 'owner') e.SubElement(owner_elem, 'name').text = 'testowner' e.SubElement(outer_report_elem, 'name').text = 'testname' e.SubElement(outer_report_elem, 'writeable').text = str(0) e.SubElement(outer_report_elem, 'in_use').text = str(0) task_elem = e.SubElement(outer_report_elem, 'task', attrib={'id': task[0]}) e.SubElement(task_elem, 'name').text = task[1] repform_elem = e.SubElement( outer_report_elem, 'report_format', attrib={'id': rep_format_id} ) e.SubElement(repform_elem, 'name').text = 'XML' # Generating inner <report> tag outer_report_elem.append(generate_inner_report(rep_id, **kwargs)) return outer_report_elem
Example #30
Source File: sim_files.py From simLAB with GNU General Public License v2.0 | 5 votes |
def setNodeAttribute(self, node, attr, value): nodeId = node.find(attr) if nodeId == None: #create attribute nodeId = etree.SubElement(node, attr) nodeId.text = value