Refactored formatter
This commit is contained in:
@@ -1,31 +1,18 @@
|
|||||||
from lxml import etree
|
from lxml import etree
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
def prettify(source: str) -> str:
|
|
||||||
"""Method used to pretty format given XML
|
|
||||||
|
|
||||||
:param source: XML
|
def format(source: str, prettify: bool) -> str:
|
||||||
:return: prettified XML
|
"""Method used to format XML
|
||||||
"""
|
|
||||||
prolog = ""
|
|
||||||
prolog_start = source.find("<?")
|
|
||||||
|
|
||||||
if prolog_start != -1:
|
:param source: XML to format
|
||||||
prolog_end = source.find("?>") + 2
|
:param prettify: sets if XML must be prettified
|
||||||
prolog = source[prolog_start:prolog_end] + "\n"
|
(have added intendations etc.) or not
|
||||||
source = source[prolog_end: ]
|
:return: formatted XML
|
||||||
|
|
||||||
parser = etree.XMLParser(remove_blank_text=True)
|
|
||||||
xml = etree.fromstring(source, parser=parser)
|
|
||||||
return prolog + etree.tostring(xml, pretty_print=True).decode()
|
|
||||||
|
|
||||||
def minimize(source: str) -> str:
|
|
||||||
"""Method used to minimize XML by deleting not needed whitespaces.
|
|
||||||
|
|
||||||
:param source: XML
|
|
||||||
:return: minimized XML
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Prolog is removed when XML is parsed
|
||||||
|
# so program has to copy it
|
||||||
prolog = ""
|
prolog = ""
|
||||||
prolog_start = source.find("<?")
|
prolog_start = source.find("<?")
|
||||||
|
|
||||||
@@ -34,9 +21,14 @@ def minimize(source: str) -> str:
|
|||||||
prolog = source[prolog_start:prolog_end]
|
prolog = source[prolog_start:prolog_end]
|
||||||
source = source[prolog_end: ]
|
source = source[prolog_end: ]
|
||||||
|
|
||||||
|
byte_input = BytesIO(source.encode("utf-8"))
|
||||||
parser = etree.XMLParser(remove_blank_text=True)
|
parser = etree.XMLParser(remove_blank_text=True)
|
||||||
xml = etree.fromstring(source, parser=parser)
|
xml = etree.parse(byte_input, parser=parser)
|
||||||
return prolog + etree.tostring(xml, pretty_print=False).decode()
|
|
||||||
|
if prettify:
|
||||||
|
prolog += "\n"
|
||||||
|
|
||||||
|
return prolog + etree.tostring(xml, pretty_print=prettify).decode()
|
||||||
|
|
||||||
|
|
||||||
def xpath(source: str, xpath: str) -> str:
|
def xpath(source: str, xpath: str) -> str:
|
||||||
|
|||||||
@@ -74,9 +74,9 @@ def process_xml(request: request, type: str) -> str:
|
|||||||
elif (type == "xpath"):
|
elif (type == "xpath"):
|
||||||
response_json['result'] = Parser.xpath(data, process)
|
response_json['result'] = Parser.xpath(data, process)
|
||||||
elif (type == "prettify"):
|
elif (type == "prettify"):
|
||||||
response_json['result'] = Parser.prettify(data)
|
response_json['result'] = Parser.format(data, True)
|
||||||
elif (type == "minimize"):
|
elif (type == "minimize"):
|
||||||
response_json['result'] = Parser.minimize(data)
|
response_json['result'] = Parser.format(data, False)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Valid operation types are: xsd, xslt, xpath")
|
raise ValueError("Valid operation types are: xsd, xslt, xpath")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user