Xmlu class¶
A Python interface to FirmaSAT <http://www.cryptosys.net/firmasat/>.
- class firmasat.Xmlu
XML utilities.
- static asciify(xmlfile_orstr)
Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).
- Parameters:
xmlfile_orstr (str) – Full path to XML file or XML data in a string.
- Returns:
XML document as a string with non-ASCII characters replaced by XML numeric character references.
- Return type:
str
- Raises:
firmasat.Error – if file cannot be found or is illegal XmL.
Note
In almost all cases, the output contains only US-ASCII characters and can safely be used as input to other functions without concern for character encoding issues.
In certain cases, some characters in an XML document cannot be replaced by a numeric character reference, for example where they are used in an element or attribute _name_, such as
Año="2016"
. In these cases, they are left as UTF-8-encoded characters.
- static fix_bom(outputFile, inputFile)
Add a UTF-8 byte order mark (BOM) to a file if not already present.
- Parameters:
outputFile – Name of output file to be created with BOM.
inputFile – Name of input file (must be valid UTF-8 encoded).
- Returns:
0 if successful, otherwise a nonzero error code – see
Err.error_lookup()
.- Return type:
int
- static get_attribute(xmlfile, attributename, elementname)
Extract attribute data from an XML file.
- Parameters:
xmlfile – Full path to XML file.
attributename – Name of attribute.
elementname – Name of element which has attribute.
- Returns:
Attribute data in a string (perhaps empty) or “!NO MATCH!” if match not found.
- Raises:
firmasat.Error – if file cannot be found.
Note
Setting
elementName=""
will output the value of the named attribute from the root element of the XML document. Setting bothelementName=""
andattributeName=""
will output the name of the root element itself.
- static receipt_version(xmlFile)
Find version number of Comprobante (receipt) element or ID number for other supported document types.
- Parameters:
xmlFile – Name of XML file
- Returns:
a positive integer indicating the type and version of SAT document, otherwise a negative error code – see
Err.error_lookup()
.40 = Comprobante document with Version=”4.0”
33 = Comprobante document with version=”3.3”
1010/1020 = Retenciones document with Version=”1.0”/”2.0”
2011/2013 = CatalogoCuentas document with Version=”1.1”/”1.3”
2111/2113 = BalanzaComprobacion document with Version=”1.1”/”1.3”
2211/2213 = PolizasPeriodo document with Version=”1.1”/”1.3”
2312/2313 = AuxiliarFolios document with Version=”1.2”/”1.3”
2411/2413 = AuxiliarCtas document with Version=”1.1”/”1.3”
2511 = SelloDigitalContElec document with Version=”1.1”
4011 = ControlesVolumetricos document with Version=”1.1”
- Return type:
int
- static validate_xml(xmlFile, loose=False)
Validate an XML file against SAT specifications.
- Parameters:
xmlFile – Full path to XML file to be validated.
loose – Use loose XML restrictions.
- Returns:
0 if successful, otherwise a nonzero error code – see
Err.error_lookup()
.- Return type:
int
Note
This just checks that the XML structure is valid. It does not verify the signature (use
Sello.verify_sig()
). If this finds an error, your XML file definitely needs fixing, but some attribute facet errors may not be detected.
- static xml_no_match()
Error message returned when failed to find a match.
Use to test for existence of a node when using
Xmlu.get_attribute()
).- Returns (str):
Error message (default = “!NO MATCH!”)