mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-13 12:35:05 +00:00
load_xml strips namespaces without QName; services import on first use and proxy providers fetch their catalogue on first query; the font scan is skipped when no subtitle names a font and is cached per process. Serve CPU -40% and track requests -28% on 20 MB manifests; CLI startup 2.7 s -> 1.2 s; --list 27-46% faster across six services tested.
24 lines
789 B
Python
24 lines
789 B
Python
from typing import Union
|
|
|
|
from lxml import etree
|
|
from lxml.etree import ElementTree
|
|
|
|
|
|
def load_xml(xml: Union[str, bytes]) -> ElementTree:
|
|
"""Safely parse XML data to an ElementTree, without namespaces in tags.
|
|
|
|
The strip uses string operations, not etree.QName: on a 20 MB manifest the QName walk costs several times the parse.
|
|
"""
|
|
if not isinstance(xml, bytes):
|
|
xml = xml.encode("utf8")
|
|
root = etree.fromstring(xml)
|
|
for elem in root.iter(etree.Element):
|
|
tag = elem.tag
|
|
if tag[0] == "{":
|
|
elem.tag = tag.partition("}")[2]
|
|
attrib = elem.attrib
|
|
for name in [name for name in attrib if name[0] == "{"]:
|
|
attrib[name.partition("}")[2]] = attrib.pop(name)
|
|
etree.cleanup_namespaces(root)
|
|
return root
|