lxmlh package

Submodules

lxmlh.config module

lxmlh config module.

lxmlh.helpers module

lxml helper methods.

lxmlh.helpers.create_attribute(name: str, attr_type: type, schema_file: str, validator: Callable | None = None) property[source]

Helper wrapper method for creating setters and getters for an attribute

lxmlh.helpers.create_attribute_list(name: str, attr_type: type, schema_file: str) property[source]

Helper wrapper method for creating setters and getters for an attribute list.

lxmlh.helpers.create_element_text(name: str, element_type: type, schema_file: str) property[source]

Helper wrapper method for creating setters and getters for an element text.

lxmlh.helpers.fill_in_defaults(node: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, static_defaults: ~typing.Dict[str, ~typing.Dict[str, str]], dynamic_defaults: ~typing.Dict[str, ~typing.Dict[str, ~typing.Callable[[<MagicMock name='mock.etree.ElementBase' id='139649806833504'>], str]]]) None[source]

Helper method for filling in defaults in all tree given any node.

lxmlh.helpers.get_attribute(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, attribute: str, attr_type: type = <class 'str'>) Any[source]

Helper wrapper method for retrieving XML attributes. Returns TYPE_DEFAULTS[type] if attribute doesn’t exist.

lxmlh.helpers.get_attribute_list(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, attribute: str, attr_type: type = <class 'str'>) List[Any][source]

Helper wrapper method for retrieving XML list attributes. Returns empty list if attributes don’t exist.

lxmlh.helpers.get_element(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str) <MagicMock name='mock.etree.ElementBase' id='139649806833504'>[source]

Helper wrapper method for retrieving XML elements as custom XML classes.

lxmlh.helpers.get_element_list(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str) ElementBase' id='139649806833504'>][source]

Helper wrapper method for retrieving XML list elements as a list of custom XML classes.

lxmlh.helpers.get_element_text(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str, element_type: type = <class 'str'>) str[source]

Helper wrapper method for retrieving XML element text as a string. Returns TYPE_DEFAULTS[str] if no text exists or element is None.

lxmlh.helpers.get_inner_text_list(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str)[source]

Helper wrapper method for retrieving the list of last nodes in a chain of XML elements.

lxmlh.helpers.set_attribute(element: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, key: str, value: str, schema_file: str, validator: ~typing.Callable | None) None[source]

Helper method for setting XML attributes. Raises DocumentInvalid exception on inappropriate setting according to XSD schema.

lxmlh.helpers.set_attribute_list(element: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, key: str, values: ~typing.List[~typing.Any], schema_file: str) None[source]

Helper method for setting XML list attributes. Raises DocumentInvalid exception on inappropriate setting according to XSD schema.

lxmlh.helpers.set_element_text(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str, value: str, schema_file: str) None[source]

Helper method for setting XML element text. Raises DocumentInvalid exception on inappropriate setting according to XSD schema.

lxmlh.parsers module

lxml parser methods.

lxmlh.parsers.parse_directory(dir_path: str | ~pathlib.Path, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>, valid_suffixes: ~typing.List[str] | None = None) ElementBase' id='139649806833504'>]][source]

Parses a directory of XML files to a list of custom Python classes. Parameters: dir_path: the directory path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to custom Python classes. valid_suffixes: a list of valid file suffixes which will only be considered for parsing. If None, defaults to [“.xml”]. Returns a list of tuples of file paths and corresponding custom Python classes representing the root of the XML file.

lxmlh.parsers.parse_file(file: str | ~pathlib.Path | ~_io.StringIO, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>) <MagicMock name='mock.etree.ElementBase' id='139649806833504'>[source]

Parses an XML file to custom classes. Parameters: file: the str|Path path to the XML file or its StringIO representation. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to python classes. Returns a custom ElementBase class representing the root of the XML file.

lxmlh.parsers.parse_zip_file(file_path: str | ~pathlib.Path, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>, valid_suffixes: ~typing.List[str] | None = None) ElementBase' id='139649806833504'>]][source]

Parses a ZIP file of XML files to a list of custom Python classes. Parameters: file_path: the ZIP file path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to custom Python classes. valid_suffixes: a list of valid file suffixes which will only be considered for parsing. If None, defaults to [“.xml”]. Returns a list of tuples of file paths and corresponding custom Python classes representing the root of the XML file.

lxmlh.parsers.save_file(root: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, path: str, pretty_print: bool = True, xml_declaration: bool = True, encoding: str = 'UTF-8', static_defaults: ~typing.Dict[str, ~typing.Dict[str, str]] | None = None, dynamic_defaults: ~typing.Dict[str, ~typing.Dict[str, ~typing.Callable[[<MagicMock name='mock.etree.ElementBase' id='139649806833504'>], str]]] | None = None) None[source]

Saves a custom class to an XML file. Parameters: root: the custom class representing the root of the XML file. path: the path to save the XML file.

lxmlh.parsers.validate_directory(dir_path: str | Path, schema_path: str, valid_suffixes: List[str] | None = None) List[Tuple[Path, None | List[str]]][source]

Validates a directory of XML files against a given schema. Parameters: dir_path: the directory path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. valid_suffixes: a list of valid file suffixes which will only be considered for validating. If None, defaults to [“.xml”].

lxmlh.parsers.validate_file(file: str | Path | StringIO, schema_path: str) None | List[str][source]

Validate a file against a given schema. Needed because the default parser doesn’t provide any usable error context. Parameters: file: the str|Path path to the XML file or its StringIO representation. schema_path: the path to the XSD schema file. Returns None if the file validates, or a list of errors as strings.

lxmlh.parsers.validate_zip_file(file_path: str | Path, schema_path: str, valid_suffixes: List[str] | None = None) None[source]

Validates a ZIP file of XML files against a given schema. Parameters: dir_path: the ZIP file path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. valid_suffixes: a list of valid file suffixes which will only be considered for validating. If None, defaults to [“.xml”].

Module contents

lxmlh.

lxmlh.create_attribute(name: str, attr_type: type, schema_file: str, validator: Callable | None = None) property[source]

Helper wrapper method for creating setters and getters for an attribute

lxmlh.create_attribute_list(name: str, attr_type: type, schema_file: str) property[source]

Helper wrapper method for creating setters and getters for an attribute list.

lxmlh.create_element_text(name: str, element_type: type, schema_file: str) property[source]

Helper wrapper method for creating setters and getters for an element text.

lxmlh.fill_in_defaults(node: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, static_defaults: ~typing.Dict[str, ~typing.Dict[str, str]], dynamic_defaults: ~typing.Dict[str, ~typing.Dict[str, ~typing.Callable[[<MagicMock name='mock.etree.ElementBase' id='139649806833504'>], str]]]) None[source]

Helper method for filling in defaults in all tree given any node.

lxmlh.get_element(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str) <MagicMock name='mock.etree.ElementBase' id='139649806833504'>[source]

Helper wrapper method for retrieving XML elements as custom XML classes.

lxmlh.get_element_list(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str) ElementBase' id='139649806833504'>][source]

Helper wrapper method for retrieving XML list elements as a list of custom XML classes.

lxmlh.get_inner_text_list(parent: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, element: str)[source]

Helper wrapper method for retrieving the list of last nodes in a chain of XML elements.

lxmlh.parse_directory(dir_path: str | ~pathlib.Path, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>, valid_suffixes: ~typing.List[str] | None = None) ElementBase' id='139649806833504'>]][source]

Parses a directory of XML files to a list of custom Python classes. Parameters: dir_path: the directory path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to custom Python classes. valid_suffixes: a list of valid file suffixes which will only be considered for parsing. If None, defaults to [“.xml”]. Returns a list of tuples of file paths and corresponding custom Python classes representing the root of the XML file.

lxmlh.parse_file(file: str | ~pathlib.Path | ~_io.StringIO, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>) <MagicMock name='mock.etree.ElementBase' id='139649806833504'>[source]

Parses an XML file to custom classes. Parameters: file: the str|Path path to the XML file or its StringIO representation. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to python classes. Returns a custom ElementBase class representing the root of the XML file.

lxmlh.parse_zip_file(file_path: str | ~pathlib.Path, schema_path: str, lookup: <MagicMock name='mock.etree.CustomElementClassLookup' id='139649806921008'>, valid_suffixes: ~typing.List[str] | None = None) ElementBase' id='139649806833504'>]][source]

Parses a ZIP file of XML files to a list of custom Python classes. Parameters: file_path: the ZIP file path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. lookup: the lookup class for mapping XML elements to custom Python classes. valid_suffixes: a list of valid file suffixes which will only be considered for parsing. If None, defaults to [“.xml”]. Returns a list of tuples of file paths and corresponding custom Python classes representing the root of the XML file.

lxmlh.save_file(root: <MagicMock name='mock.etree.ElementBase' id='139649806833504'>, path: str, pretty_print: bool = True, xml_declaration: bool = True, encoding: str = 'UTF-8', static_defaults: ~typing.Dict[str, ~typing.Dict[str, str]] | None = None, dynamic_defaults: ~typing.Dict[str, ~typing.Dict[str, ~typing.Callable[[<MagicMock name='mock.etree.ElementBase' id='139649806833504'>], str]]] | None = None) None[source]

Saves a custom class to an XML file. Parameters: root: the custom class representing the root of the XML file. path: the path to save the XML file.

lxmlh.validate_directory(dir_path: str | Path, schema_path: str, valid_suffixes: List[str] | None = None) List[Tuple[Path, None | List[str]]][source]

Validates a directory of XML files against a given schema. Parameters: dir_path: the directory path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. valid_suffixes: a list of valid file suffixes which will only be considered for validating. If None, defaults to [“.xml”].

lxmlh.validate_file(file: str | Path | StringIO, schema_path: str) None | List[str][source]

Validate a file against a given schema. Needed because the default parser doesn’t provide any usable error context. Parameters: file: the str|Path path to the XML file or its StringIO representation. schema_path: the path to the XSD schema file. Returns None if the file validates, or a list of errors as strings.

lxmlh.validate_zip_file(file_path: str | Path, schema_path: str, valid_suffixes: List[str] | None = None) None[source]

Validates a ZIP file of XML files against a given schema. Parameters: dir_path: the ZIP file path, should contain files of only the schema_path version. schema_path: the path to the XSD schema file. valid_suffixes: a list of valid file suffixes which will only be considered for validating. If None, defaults to [“.xml”].