Python xml.etree.ElementTree.tostring() Examples
The following are 30
code examples of xml.etree.ElementTree.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
xml.etree.ElementTree
, or try the search function
.
Example #1
Source File: base.py From robosuite with MIT License | 7 votes |
def get_model(self, mode="mujoco_py"): """ Returns a MjModel instance from the current xml tree. """ available_modes = ["mujoco_py"] with io.StringIO() as string: string.write(ET.tostring(self.root, encoding="unicode")) if mode == "mujoco_py": from mujoco_py import load_model_from_xml model = load_model_from_xml(string.getvalue()) return model raise ValueError( "Unkown model mode: {}. Available options are: {}".format( mode, ",".join(available_modes) ) )
Example #2
Source File: base.py From terraform-templates with Apache License 2.0 | 7 votes |
def remove_commit_lock(self, admin=None, scope="shared", exceptions=True, retry_on_peer=True): self._logger.debug("%s: Remove commit lock requested for scope %s" % (self.id, scope)) cmd = ET.Element("request") subel = ET.SubElement(cmd, "commit-lock") subel = ET.SubElement(subel, "remove") if admin is not None: subel = ET.SubElement(subel, "admin") subel.text = admin try: self.xapi.op(ET.tostring(cmd, encoding='utf-8'), vsys=scope, retry_on_peer=retry_on_peer) except (pan.xapi.PanXapiError, err.PanDeviceXapiError) as e: if not re.match(r"Commit lock is not currently held", str(e)): raise else: if exceptions: raise err.PanLockError(str(e), pan_device=self) else: self._logger.debug(str(e)) return False self.commit_locked = False return True
Example #3
Source File: base.py From terraform-templates with Apache License 2.0 | 6 votes |
def remove_config_lock(self, scope="shared", exceptions=True, retry_on_peer=True): self._logger.debug("%s: Remove config lock requested for scope %s" % (self.id, scope)) cmd = ET.Element("request") subel = ET.SubElement(cmd, "config-lock") subel = ET.SubElement(subel, "remove") try: self.xapi.op(ET.tostring(cmd, encoding='utf-8'), vsys=scope, retry_on_peer=retry_on_peer) except (pan.xapi.PanXapiError, err.PanDeviceXapiError) as e: if not re.match(r"Config is not currently locked for scope (shared|vsys\d)", str(e)): raise else: if exceptions: raise err.PanLockError(str(e), pan_device=self) else: self._logger.debug(str(e)) return False self.config_locked = False return True
Example #4
Source File: xmpp_request_handler.py From browserscope with Apache License 2.0 | 6 votes |
def _generate_chat(self, to, from_, body): data = _FormData() data.add_text('from', from_, 'plain') data.add_text('to', to, 'plain') data.add_text('body', body, 'plain') message_element = ElementTree.Element( ElementTree.QName('jabber:client', 'message'), {'from': from_, 'to': to, 'type': 'chat'}) body_element = ElementTree.SubElement( message_element, ElementTree.QName('jabber:client', 'body')) body_element.text = body data.add_text('stanza', ElementTree.tostring(message_element, encoding='utf-8'), 'xml') return data
Example #5
Source File: modular_input.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def _do_scheme(self): scheme = Scheme(self.title) scheme.description = self.description scheme.use_external_validation = self.use_external_validation scheme.streaming_mode = Scheme.streaming_mode_xml scheme.use_single_instance = self.use_single_instance for argument in self.extra_arguments(): name = argument['name'] title = argument.get('title', None) description = argument.get('description', None) validation = argument.get('validation', None) data_type = argument.get('data_type', Argument.data_type_string) required_on_edit = argument.get('required_on_edit', False) required_on_create = argument.get('required_on_create', False) scheme.add_argument( Argument(name, title=title, description=description, validation=validation, data_type=data_type, required_on_edit=required_on_edit, required_on_create=required_on_create)) return ET.tostring(scheme.to_xml(), encoding=SCHEME_ENCODING)
Example #6
Source File: xmpp_request_handler.py From browserscope with Apache License 2.0 | 6 votes |
def _generate_presence_available(self, to, from_, show=None): data = _FormData() data.add_text('from', from_, 'plain') data.add_text('to', to, 'plain') # If the "presence" attribute is absent, "available" is assumed and it is # not sent by Google Talk. presence_element = ElementTree.Element( ElementTree.QName('jabber:client', 'presence'), {'from': from_, 'to': to}) if show: # This is currently a dead code path. # The show element is optional according to RFC 3921, 2.2.2.1. data.add_text('show', show, 'plain') show_element = ElementTree.SubElement( presence_element, ElementTree.QName('jabber:client', 'show')) show_element.text = show data.add_text('stanza', ElementTree.tostring(presence_element, 'utf-8'), 'xml') return data
Example #7
Source File: wfapi.py From terraform-templates with Apache License 2.0 | 6 votes |
def xml_root(self): if self.xml_element_root is None: return None s = etree.tostring(self.xml_element_root, encoding=_encoding) if not s: return None self._log(DEBUG3, 'xml_root: %s', type(s)) self._log(DEBUG3, 'xml_root.decode(): %s', type(s.decode(_encoding))) return s.decode(_encoding) # XXX Unicode notes # 2.7 # decode() str (bytes) -> unicode # encode() unicode -> str (bytes) # encode() of str will call decode() # 3.x # decode() bytes -> str (unicode) # encode() str (unicode) -> bytes # cannot encode() bytes # cannot decode() str
Example #8
Source File: xmls.py From plugin.video.emby with GNU General Public License v3.0 | 6 votes |
def tvtunes_nfo(path, urls): ''' Create tvtunes.nfo ''' try: xml = etree.parse(path).getroot() except Exception: xml = etree.Element('tvtunes') for elem in xml.getiterator('tvtunes'): for file in list(elem): elem.remove(file) for url in urls: etree.SubElement(xml, 'file').text = url indent(xml) write_xml(etree.tostring(xml, 'UTF-8'), path)
Example #9
Source File: userid.py From terraform-templates with Apache License 2.0 | 6 votes |
def send(self, uidmessage): """Send a uidmessage to the User-ID API of a firewall Used for adhoc User-ID API calls that are not supported by other methods in this class. This method cannot be batched. Args: uidmessage (str): The UID Message in XML to send to the firewall """ if self._batch: return else: cmd = ET.tostring(uidmessage) try: self.device.xapi.user_id(cmd=cmd, vsys=self.device.vsys) except (err.PanDeviceXapiError, PanXapiError) as e: # Check if this is just an error about duplicates or nonexistant tags # If so, ignore the error. Most operations don't care about this. message = str(e) if self.ignore_dup_errors and (message.endswith("already exists, ignore") or message.endswith("does not exist, ignore unreg")): return else: raise e
Example #10
Source File: outputhandler.py From benchexec with Apache License 2.0 | 6 votes |
def _write_pretty_result_xml_to_file(self, xml, filename): """Writes a nicely formatted XML file with DOCTYPE, and compressed if necessary.""" if self.compress_results: actual_filename = filename + ".bz2" open_func = bz2.BZ2File else: # write content to temp file first to prevent losing data # in existing file if writing fails actual_filename = filename + ".tmp" open_func = open with io.TextIOWrapper( open_func(actual_filename, "wb"), encoding="utf-8" ) as file: rough_string = ElementTree.tostring(xml, encoding="unicode") reparsed = minidom.parseString(rough_string) doctype = minidom.DOMImplementation().createDocumentType( "result", RESULT_XML_PUBLIC_ID, RESULT_XML_SYSTEM_ID ) reparsed.insertBefore(doctype, reparsed.documentElement) reparsed.writexml( file, indent="", addindent=" ", newl="\n", encoding="utf-8" ) if self.compress_results: # try to delete uncompressed file (would have been overwritten in no-compress-mode) try: os.remove(filename) except OSError: pass self.all_created_files.discard(filename) self.all_created_files.add(actual_filename) else: os.rename(actual_filename, filename) self.all_created_files.add(filename) return filename
Example #11
Source File: xapi.py From terraform-templates with Apache License 2.0 | 6 votes |
def xml_result(self): if self.element_result is None: return None s = ''.encode() if self.element_result.text: s += self.element_result.text.encode(_encoding) for elem in self.element_result: s += etree.tostring(elem, encoding=_encoding) if not s: return None self._log(DEBUG3, 'xml_result: %s', type(s)) self._log(DEBUG3, 'xml_result.decode(): %s', type(s.decode(_encoding))) return s.decode(_encoding)
Example #12
Source File: views.py From plugin.video.emby with GNU General Public License v3.0 | 6 votes |
def node_index(self, folder, view, mixed=False): file = os.path.join(folder, "index.xml") index = self.sync['SortedViews'].index(view['Id']) try: xml = etree.parse(file).getroot() xml.set('order', str(index)) except Exception: xml = self.node_root('main', index) etree.SubElement(xml, 'label') label = xml.find('label') label.text = view['Name'] if not mixed else "%s (%s)" % (view['Name'], _(view['Media'])) indent(xml) write_xml(etree.tostring(xml, 'UTF-8'), file)
Example #13
Source File: base.py From terraform-templates with Apache License 2.0 | 6 votes |
def add_commit_lock(self, comment=None, scope="shared", exceptions=True, retry_on_peer=True): self._logger.debug("%s: Add commit lock requested for scope %s" % (self.id, scope)) cmd = ET.Element("request") subel = ET.SubElement(cmd, "commit-lock") subel = ET.SubElement(subel, "add") if comment is not None: subel = ET.SubElement(subel, "comment") subel.text = comment try: self.xapi.op(ET.tostring(cmd, encoding='utf-8'), vsys=scope, retry_on_peer=retry_on_peer) except (pan.xapi.PanXapiError, err.PanDeviceXapiError) as e: if not re.match(r"Commit lock is already held", str(e)): raise else: if exceptions: raise err.PanLockError(str(e), pan_device=self) else: self._logger.debug(str(e)) return False self.commit_locked = True return True
Example #14
Source File: base.py From terraform-templates with Apache License 2.0 | 6 votes |
def op(self, cmd=None, vsys=None, xml=False, cmd_xml=True, extra_qs=None, retry_on_peer=False): """Perform operational command on this device Args: cmd (str): The operational command to execute vsys (str): Vsys id. xml (bool): Return value should be a string (Default: False) cmd_xml (bool): True: cmd is not XML, False: cmd is XML (Default: True) extra_qs: Extra parameters for API call retry_on_peer (bool): Try on active Firewall first, then try on passive Firewall Returns: xml.etree.ElementTree: The result of the operational command. May also return a string of XML if xml=True """ element = self.xapi.op(cmd, vsys, cmd_xml, extra_qs, retry_on_peer=retry_on_peer) if xml: return ET.tostring(element, encoding='utf-8') else: return element
Example #15
Source File: modular_input.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def _do_scheme(self): scheme = Scheme(self.title) scheme.description = self.description scheme.use_external_validation = self.use_external_validation scheme.streaming_mode = Scheme.streaming_mode_xml scheme.use_single_instance = self.use_single_instance for argument in self.extra_arguments(): name = argument['name'] title = argument.get('title', None) description = argument.get('description', None) validation = argument.get('validation', None) data_type = argument.get('data_type', Argument.data_type_string) required_on_edit = argument.get('required_on_edit', False) required_on_create = argument.get('required_on_create', False) scheme.add_argument( Argument(name, title=title, description=description, validation=validation, data_type=data_type, required_on_edit=required_on_edit, required_on_create=required_on_create)) return ET.tostring(scheme.to_xml(), encoding=SCHEME_ENCODING)
Example #16
Source File: base.py From terraform-templates with Apache License 2.0 | 6 votes |
def applyall(cls, parent): device = parent.nearest_pandevice() logger.debug(device.id + ": applyall called on %s type" % cls) objects = parent.findall(cls) if not objects: return # Create the xpath xpath = objects[0].xpath_nosuffix() # Create the root element lasttag = cls.XPATH.rsplit("/", 1)[-1] element = ET.Element(lasttag) # Build the full element from the objects for obj in objects: device.xml_combine(element, [obj.element(), ]) # Apply the element to the xpath device.set_config_changed() device.xapi.edit(xpath, ET.tostring(element, encoding='utf-8'), retry_on_peer=cls.HA_SYNC) for obj in objects: obj._check_child_methods("apply")
Example #17
Source File: jenkins-cobertura.py From bob with GNU General Public License v3.0 | 6 votes |
def checkCobertura(config, checkoutSteps, buildSteps, packageSteps, **kwargs): found = False for s in checkoutSteps: if s.getPackage().getName().endswith("unittests"): found = True for s in buildSteps: if s.getPackage().getName().endswith("unittests"): found = True for s in packageSteps: if s.getPackage().getName().endswith("unittests"): found = True if found: root = ElementTree.fromstring(config) publishers = root.find("publishers") if publishers.find("hudson.plugins.cobertura.CoberturaPublisher") is None: publishers.append(PLUGIN) config = ElementTree.tostring(root, encoding="UTF-8") return config
Example #18
Source File: gate.py From SEM with MIT License | 5 votes |
def document_to_file(self, document, couples, output, encoding="utf-8", **kwargs): gateDocument = self.document_to_data(document, couples=couples) content = ET.tostring(gateDocument, encoding="utf-8").decode(u"utf-8") if is_string(output): with codecs.open(output, u"w", encoding) as O: O.write(u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') O.write(content) #O.write(self.document_to_unicode(document, couples, encoding=encoding, **kwargs)) else: output.write(u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') output.write(content)
Example #19
Source File: tei_analec.py From SEM with MIT License | 5 votes |
def document_to_file(self, document, couples, output, encoding="utf-8", **kwargs): teiCorpus = self.document_to_data(document, couples=couples) content = ET.tostring(teiCorpus, encoding="utf-8").decode("utf-8") if is_string(output): with codecs.open(output, "w", "utf-8") as O: O.write(u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') O.write(content) else: output.write(u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') output.write(content)
Example #20
Source File: panorama.py From terraform-templates with Apache License 2.0 | 5 votes |
def commit_all(self, sync=False, sync_all=True, exception=False, devicegroup=None, serials=(), cmd=None): """Trigger a commit-all (commit to devices) on Panorama Args: sync (bool): Block until the Panorama commit is finished (Default: False) sync_all (bool): Block until every Firewall commit is finished, requires sync=True (Default: False) exception (bool): Create an exception on commit errors (Default: False) devicegroup (str): Limit commit-all to a single device-group serials (list): Limit commit-all to these serial numbers cmd (str): Commit options in XML format Returns: dict: Commit results """ self._logger.debug("Commit-all initiated on device: %s" % (self.id,)) if cmd is None: # XXX: This only works on PAN-OS 7.0+ e = ET.Element("commit-all") if devicegroup is not None and cmd is None: sp = ET.SubElement(e, "shared-policy") dg = ET.SubElement(sp, "device-group") dg_e = ET.SubElement(dg, "entry", {"name": devicegroup}) if serials: d = ET.SubElement(dg_e, "devices") for serial in serials: ET.SubElement(d, "entry", {"name": serial}) cmd = ET.tostring(e) elif isinstance(cmd, pan.commit.PanCommit): cmd = cmd.cmd() elif isinstance(cmd, ET.Element): cmd = ET.tostring(cmd) result = self._commit(sync=sync, sync_all=sync_all, commit_all=True, exception=exception, cmd=cmd) return result
Example #21
Source File: tei_reden.py From SEM with MIT License | 5 votes |
def document_to_unicode(self, document, couples, encoding="utf-8", **kwargs): return u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' \ + ET.tostring(self.document_to_data(document, couples), encoding=encoding)
Example #22
Source File: gate.py From SEM with MIT License | 5 votes |
def document_to_unicode(self, document, couples, **kwargs): return u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' \ + ET.tostring(self.document_to_data(document, couples), encoding=encoding)
Example #23
Source File: tei_np.py From SEM with MIT License | 5 votes |
def document_to_file(self, document, couples, output, encoding="utf-8", **kwargs): teiCorpus = self.document_to_data(document, couples=couples) with open(output, "w") as O: O.write(u'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n') O.write(ET.tostring(teiCorpus, encoding="utf-8")) #O.write(self.document_to_unicode(document, couples, encoding=encoding, **kwargs))
Example #24
Source File: VisualStudio.py From bob with GNU General Public License v3.0 | 5 votes |
def generateFilters(self): items = [] filters = {} def makeFilter(category, uuid, filePath): trail = [ category ] for i in filePath.relative_to(self.workspacePath).parent.parts: uuid = sha1NsUuid(uuid, i) trail.append(i) name = "\\".join(trail) if name not in filters: f = ElementTree.Element("Filter", {"Include" : name}) ElementTree.SubElement(f, "UniqueIdentifier").text = "{" + str(uuid) + "}" filters[name] = ElementTree.tostring(f, encoding="unicode") return "\\".join(trail) if self.headers: g = ElementTree.Element("ItemGroup") for i in self.headers: s = ElementTree.SubElement(g, "ClInclude", {"Include" : str(i)}) ElementTree.SubElement(s, "Filter").text = makeFilter("Header Files", FILTERS_HEADERS_UUID, i) items.append(ElementTree.tostring(g, encoding="unicode")) if self.sources: g = ElementTree.Element("ItemGroup") for i in self.sources: s = ElementTree.SubElement(g, "ClCompile", {"Include" : str(i)}) ElementTree.SubElement(s, "Filter").text = makeFilter("Source Files", FILTERS_SOURCES_UUID, i) items.append(ElementTree.tostring(g, encoding="unicode")) if self.resources: g = ElementTree.Element("ItemGroup") for i in self.resources: s = ElementTree.SubElement(g, "Text", {"Include" : str(i)}) ElementTree.SubElement(s, "Filter").text = makeFilter("Resource Files", FILTERS_RESOURCES_UUID, i) items.append(ElementTree.tostring(g, encoding="unicode")) return FILTERS_TEMPLATE.format(FILTERS="\n".join(f for n,f in sorted(filters.items())), ITEMS="\n".join(items))
Example #25
Source File: config.py From terraform-templates with Apache License 2.0 | 5 votes |
def xml(self, xpath=None): nodes = self.__find_xpath(xpath) if not nodes: return None s = ''.encode() for elem in nodes: s += etree.tostring(elem, encoding=_encoding) if not s: return None self._log(DEBUG3, 'xml: %s', type(s)) self._log(DEBUG3, 'xml.decode(): %s', type(s.decode(_encoding))) return s.decode(_encoding)
Example #26
Source File: xapi.py From terraform-templates with Apache License 2.0 | 5 votes |
def xml_root(self): if self.element_root is None: # May not be set due to ParseError, so return response return self.xml_document s = etree.tostring(self.element_root, encoding=_encoding) if not s: return None self._log(DEBUG3, 'xml_root: %s', type(s)) self._log(DEBUG3, 'xml_root.decode(): %s', type(s.decode(_encoding))) return s.decode(_encoding)
Example #27
Source File: test_pfmarkdown.py From pagure with GNU General Public License v2.0 | 5 votes |
def test_obj_anchor_tag_pr(self): """Assert links to pull requests are generated correctly""" pr = model.PullRequest(title="The pull request summary") expected_markup = ( b'<a href="http://eh/" title="The pull request ' b'summary">My Pull Request</a>' ) element = pfmarkdown._obj_anchor_tag( "jcline", None, None, pr, "My Pull Request" ) self.assertEqual(expected_markup, ElementTree.tostring(element))
Example #28
Source File: firewall.py From terraform-templates with Apache License 2.0 | 5 votes |
def create_vsys(self): """Create the vsys on the live device that this Firewall object represents""" if self.vsys.startswith("vsys"): element = ET.Element("entry", {"name": self.vsys}) if self.vsys_name is not None: ET.SubElement(element, "display-name").text = self.vsys_name self.set_config_changed() self.xapi.set(self._root_xpath_vsys(None), ET.tostring(element), retry_on_peer=True)
Example #29
Source File: base.py From terraform-templates with Apache License 2.0 | 5 votes |
def element_str(self): """The XML form of this object (and its children) as a string.""" return ET.tostring(self.element(), encoding='utf-8')
Example #30
Source File: base.py From terraform-templates with Apache License 2.0 | 5 votes |
def equal(self, panobject, force=False, compare_children=True): """Compare this object to another object Equality of the objects is determined by the XML they generate, not by the values of their variables. Args: panobject (VersionedPanObject): The object to compare with this object force (bool): Do not raise a PanObjectError if the objects are different classes compare_children (bool): Include children of the PanObject in the comparison Raises: PanObjectError: Raised if the objects are different types that would not normally be comparable Returns: bool: True if the XML of the objects is equal, False if not """ if not panobject: return False if type(self) != type(panobject) and not force: msg = 'Object {0} is not compareable to {1}' raise err.PanObjectError(msg.format(self, panobject)) xml_self = ET.tostring(self.element(compare_children, True), encoding='utf-8') xml_other = ET.tostring(panobject.element(compare_children, True), encoding='utf-8') return xml_self == xml_other