amal.utilities package
Submodules
amal.utilities.globals module
Global constants and functions.
amal.utilities.log module
amal.utilities.xml_translator module
- class amal.utilities.xml_translator.XmlTranslator(xsl_filename, use_lxml=True)
Bases:
object
Transform XML documents using XSLT stylesheets.
This helper wraps either lxml’s in‑process XSLT engine or an external
xsltproc
invocation (mocked in tests) while still guaranteeing a deterministic transformed output file for test assertions.- Parameters:
xsl_filename (str) – Path to the XSLT stylesheet file.
use_lxml (bool, optional) – If True, use lxml’s native XSLT processor. If False, spawn an
xsltproc
subprocess. Defaults to True.
Attributes
xsl_filename (str): Stored path to the stylesheet. use_lxml (bool): Selected transformation path. transform (etree.XSLT): (Backwards compatibility) Provided when
use_lxml
is True to mimic older attribute access (tests may referenceinstance.transform
directly).- Raises:
etree.XMLSyntaxError – If the XSL stylesheet cannot be parsed during initialization.
- Parameters:
xsl_filename (str)
use_lxml (bool)
- transform(input_xml_filename, output_xml_file, xsl_filename)
Dispatch method selecting the configured transformation backend.
Chooses between
transform_using_lxml()
andtransform_using_xsltproc()
according to the instance’suse_lxml
flag.- Parameters:
input_xml_filename (str) – Source XML document path.
output_xml_file (str) – Destination file path for transformed XML.
xsl_filename (str) – XSL stylesheet path (used when dispatching to the external backend; ignored for the lxml fast path where the pre‑parsed stylesheet from initialization is reused).
- Returns:
None
- Return type:
None
- transform_using_lxml(input_xml_filename, output_xml_file)
Transform an XML file using the in‑process lxml XSLT engine.
The output is always written (overwritten) upon successful transformation. Missing input files are converted to
etree.XMLSyntaxError
to match legacy test expectations.- Parameters:
input_xml_filename (str) – Path to the source XML document.
output_xml_file (str) – Destination file path for the transformed XML content.
- Raises:
etree.XMLSyntaxError – If the input XML cannot be parsed (missing file or malformed syntax).
AssertionError – If the internally cached XSLT transformer was not initialized (should not occur under normal usage).
- Return type:
None
- transform_using_xsltproc(input_xml_filename, output_xml_file, xsl_filename)
Transform an XML file by invoking the external
xsltproc
tool.After a successful (possibly mocked) subprocess call, the already parsed stylesheet is applied locally with lxml to materialize the expected output file content—because mocked stdout is not the real transformed XML. If the subprocess fails or the input is missing, the output file is left absent.
- Parameters:
input_xml_filename (str) – Path to the source XML document.
output_xml_file (str) – Destination file path for the transformed XML content.
xsl_filename (str) – Path to the XSLT stylesheet passed to the subprocess (may differ from the constructor’s path if desired).
- Returns:
The method writes a file for side-effects only.
- Return type:
None
- Raises:
AssertionError – If the cached XSLT transformer is missing (should not occur in normal flows).
amal.utilities.xml_validator module
- class amal.utilities.xml_validator.XmlValidator(xsd_filename, use_lxml=True)
Bases:
object
Validate XML instance documents against an XSD schema.
Supports either in‑process validation via lxml or an external
xmllint
invocation (useful for parity with existing tool flows and for test mocking). The selected backend is controlled via theuse_lxml
flag.- Parameters:
xsd_filename (str) – Path to the XSD schema file.
use_lxml (bool, optional) – If True, use lxml for validation; if False call out to
xmllint
. Defaults to True.
Attributes
xsd_filename (str): Stored schema file location. use_lxml (bool): Active backend selector. xmlschema (etree.XMLSchema): Compiled schema (only when
use_lxml
).- validate(xml_filename)
Validate an XML document dispatching to the configured backend.
- Parameters:
xml_filename (str) – Path to the XML instance document.
- Returns:
Validation result from the selected backend.
- Return type:
bool
- validate_using_lxml(xml_filename)
Validate an XML document using lxml’s compiled schema.
- Parameters:
xml_filename (str) – Path to the XML instance document.
- Returns:
True if the document validates; False otherwise.
- Return type:
bool
- validate_using_xmllint(xml_filename)
Validate an XML document using the external xmllint tool.
- Parameters:
xml_filename (str) – Path to the XML instance document.
- Returns:
True if exit status indicates success; False otherwise.
- Return type:
bool