From dc16316aed9b37bfbf83f68295e3607934270351 Mon Sep 17 00:00:00 2001 From: dullfig Date: Thu, 8 Jan 2026 15:49:07 -0800 Subject: [PATCH] fixing docs --- structure.md | 12 ++++++++++++ third_party/xmlable/__init__.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/structure.md b/structure.md index 56f6f79..34b4c18 100644 --- a/structure.md +++ b/structure.md @@ -68,6 +68,18 @@ xml-pipeline/ │ ├── scripts/ │ │ └── generate_organism_key.py │ └── __init__.py +├── third_party/ +│ └── xmlable/ +│ ├── __init__.py +│ ├── _errors.py +│ ├── _io.py +│ ├── _lxml_helpers.py +│ ├── _manual.py +│ ├── _user.py +│ ├── _utils.py +│ ├── _xmlify.py +│ ├── _xobject.py +│ └── py.typed ├── xml_pipeline.egg-info/ │ ├── PKG-INFO │ ├── SOURCES.txt diff --git a/third_party/xmlable/__init__.py b/third_party/xmlable/__init__.py index e69de29..41bf01d 100644 --- a/third_party/xmlable/__init__.py +++ b/third_party/xmlable/__init__.py @@ -0,0 +1,32 @@ +"""xmlable — vendored and extended for AgentServer v2.1""" + +from lxml import etree, objectify +from lxml.etree import _Element +from lxml.objectify import ObjectifiedElement +from typing import Type, TypeVar, Any +from io import BytesIO + +from ._xmlify import xmlify + +T = TypeVar("T") + +def _get_xobject(cls: Type[T]) -> Any: + if not hasattr(cls, "get_xobject"): + raise ValueError(f"Class {cls.__name__} is not decorated with @xmlify") + return cls.get_xobject() + +def parse_element(cls: Type[T], element: _Element | ObjectifiedElement) -> T: + """Direct in-memory deserialization from validated lxml Element.""" + xobject = _get_xobject(cls) + obj_element = objectify.fromstring(etree.tostring(element)) + return xobject.xml_in(obj_element, ctx=None) + +def parse_bytes(cls: Type[T], xml_bytes: bytes) -> T: + tree = objectify.parse(BytesIO(xml_bytes)) + root = tree.getroot() + return parse_element(cls, root) + +def parse_string(cls: Type[T], xml_str: str) -> T: + return parse_bytes(cls, xml_str.encode("utf-8")) + +__all__ = ["xmlify", "parse_element", "parse_bytes", "parse_string"]