def deserialize(resource: Union[str, Path, bytes], target_class: Optional[Type[T]] = None) -> Optional[T]: parser = XmlParser(context=XmlContext()) obj = None if os.path.isfile(resource): resource = Path(resource) if isinstance(resource, str): obj = parser.from_string(resource, target_class) if isinstance(resource, Path): obj = parser.from_path(resource, target_class) if isinstance(resource, bytes): obj = parser.from_bytes(resource, target_class) if obj and hasattr(obj, 'complemento') and obj.complemento: complementos = __deserialize_complementos(obj) setattr(obj.complemento, 'any_element', complementos) return obj
def __init__(self, str_xml): Document.__init__(self) parser = XmlParser() self.score = parser.from_string(str_xml, ScorePartwise) self.tempo = _find_tempo(self.score)
def CreateFromDocument(document): parser = XmlParser(config=ParserConfig(fail_on_unknown_properties=True)) if isinstance(document, str): return parser.from_string(document, ismrmrdHeader) return parser.from_bytes(document, ismrmrdHeader)
def loads( sdf: str, *, version: str = None, custom_constructor: Dict[Type[T], Callable] = None, handler: str = None, ): """Convert an XML string into a sdformat.models tree. Parameters ---------- sdf : str The SDFormat XML to be parsed. version : str The SDFormat version to use while parsing. If None (default) it will automatically determine the version from the <sdf> element. If specified the given version will be used instead. custom_constructor : Dict[Type[T], Callable] Overwrite the default constructor for a certain model class with callable. This is useful for doing pre- or post-initialization of bound classes or to replace them entirely. handler : str The handler that the parser should use when traversing the XML. If unspecified the default xsData parser will be used (lxml if it is installed, otherwise xml.etree). Possible values are: "XmlEventHandler" A xml.etree event-based handler. "LxmlEventHandler" A lxml.etree event-based handler. Returns ------- SdfRoot : object An instance of ``skbot.ignition.models.vXX.Sdf`` where XX corresponds to the version of the SDFormat XML. Notes ----- ``custom_constructure`` is currently disabled and has no effect. It will become available with xsData v21.8. Examples -------- .. minigallery:: skbot.ignition.sdformat.loads """ if custom_constructor is None: custom_constructor = dict() def custom_class_factory(clazz, params): if clazz in custom_constructor: return custom_constructor[clazz](**params) return clazz(**params) if version is None: version = get_version(sdf) if handler in ["XmlSaxHandler", "LxmlSaxHandler"]: warnings.warn( "SAX handlers have been deprecated in xsData >= 21.9;" " falling back to EventHandler. If you need the SAX handler, please open an issue." " To make this warning dissapear change `handler` to the corresponding EventHandler.", DeprecationWarning, ) if handler == "XmlSaxHandler": handler = "XmlEventHandler" elif handler == "LxmlSaxHandler": handler = "LxmlEventHandler" handler_class = { None: handlers.default_handler(), "XmlEventHandler": handlers.XmlEventHandler, "LxmlEventHandler": handlers.LxmlEventHandler, }[handler] binding_location = _parser_roots[version] bindings = importlib.import_module(binding_location, __name__) sdf_parser = XmlParser( ParserConfig(class_factory=custom_class_factory), context=xml_ctx, handler=handler_class, ) try: root_el = sdf_parser.from_string(sdf, bindings.Sdf) except XSDataParserError as e: raise ParseError("Invalid SDFormat XML.") from e return root_el