1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-09-07 13:48:43 +02:00

Move xml_indent, xml_quote and xml_unquote to osc.util.xml module

This commit is contained in:
2024-02-12 09:06:01 +01:00
parent ff31a5cd4b
commit 556c97d7ee
3 changed files with 86 additions and 58 deletions

View File

@@ -7,6 +7,10 @@ and work with related XML data.
import xml.sax.saxutils
from xml.etree import ElementTree as ET
from ..util.xml import xml_escape
from ..util.xml import xml_indent
from ..util.xml import xml_unescape
def get(apiurl, path, query=None):
"""
@@ -205,41 +209,3 @@ def write_xml_node_to_file(node, path, indent=True):
if indent:
xml_indent(node)
ET.ElementTree(node).write(path)
def xml_escape(string):
"""
Escape the string so it's safe to use in XML and xpath.
"""
entities = {
"\"": """,
"'": "'",
}
if isinstance(string, bytes):
return xml.sax.saxutils.escape(string.decode("utf-8"), entities=entities).encode("utf-8")
return xml.sax.saxutils.escape(string, entities=entities)
def xml_unescape(string):
"""
Decode XML entities in the string.
"""
entities = {
""": "\"",
"'": "'",
}
if isinstance(string, bytes):
return xml.sax.saxutils.unescape(string.decode("utf-8"), entities=entities).encode("utf-8")
return xml.sax.saxutils.unescape(string, entities=entities)
def xml_indent(root):
"""
Indent XML so it looks pretty after printing or saving to file.
"""
if hasattr(ET, "indent"):
# ElementTree supports indent() in Python 3.9 and newer
ET.indent(root)
else:
from .. import core as osc_core
osc_core.xmlindent(root)