mirror of
https://github.com/openSUSE/osc.git
synced 2025-09-07 13:48:43 +02:00
_private.api: Add xml_escape() function
This commit is contained in:
@@ -4,6 +4,7 @@ and work with related XML data.
|
||||
"""
|
||||
|
||||
|
||||
import xml.sax.saxutils
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
|
||||
@@ -120,6 +121,19 @@ def write_xml_node_to_file(node, path, indent=True):
|
||||
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_indent(root):
|
||||
"""
|
||||
Indent XML so it looks pretty after printing or saving to file.
|
||||
|
Reference in New Issue
Block a user