diff --git a/README.md b/README.md index 8e3b146..1f78dc5 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ When running **Pain001**, you will need to specify four arguments: The currently supported types are: - pain.001.001.03 + - pain.001.001.04 - pain.001.001.09 - An `xml_template_file_path`: This is the path to the XML template file you are using that contains variables that will be replaced by the values in your diff --git a/pain001/__main__.py b/pain001/__main__.py index d0b313c..28716d5 100644 --- a/pain001/__main__.py +++ b/pain001/__main__.py @@ -184,16 +184,6 @@ def check_variable(variable, name): print(f"The data file '{data_file_path}' does not exist.") sys.exit(1) - # Validate the XML file and raise a SystemExit exception if invalid - # is_valid = validate_via_xsd( - # xml_template_file_path, xsd_schema_file_path - # ) - # if not is_valid: - # logger.error( - # f"Error: XML located at {xml_template_file_path} is invalid." - # ) - # sys.exit(1) - process_files( xml_message_type, xml_template_file_path, diff --git a/pain001/xml/generate_xml.py b/pain001/xml/generate_xml.py index 0a58787..8137102 100644 --- a/pain001/xml/generate_xml.py +++ b/pain001/xml/generate_xml.py @@ -21,13 +21,37 @@ def create_common_elements(parent, row, mapping): + """Create common elements "PmtInfId" and "PmtMtd" in the XML tree using + data from the CSV or SQLite Data Files. + + Parameters + ---------- + parent : xml.etree.ElementTree.Element + Parent element in the XML tree. + row : list + List of strings, each string is a row of the Data file. + mapping : dict + Dictionary with the mapping between XML tags and Data columns. + """ + for xml_tag, csv_column in mapping.items(): if xml_tag in ["PmtInfId", "PmtMtd"]: create_xml_element(parent, xml_tag, row[csv_column]) def create_xml_v3(root, data, mapping): - # print("XML v3") + """Create the XML tree for the pain.001.001.03 schema. + + Args: + root (ElementTree.Element): The root element of the XML tree. + data (list): A list of dictionaries containing the data to be added + to the XML document. + mapping (dict): A dictionary mapping the Data column names to the XML + element names. + + Returns: + The root element of the XML tree. + """ # Create CstmrCdtTrfInitn element cstmr_cdt_trf_initn_element = ET.Element("CstmrCdtTrfInitn") @@ -61,23 +85,23 @@ def create_xml_v3(root, data, mapping): create_common_elements(PmtInf_element, row, mapping) # Create new "BtchBookg" element in the XML tree using data - # from the CSV file + # from the Data file create_xml_element( PmtInf_element, "BtchBookg", row["batch_booking"].lower() ) # Create new "NbOfTxs" element in the XML tree using data from - # the CSV file + # the Data file create_xml_element(PmtInf_element, "NbOfTxs", row["nb_of_txs"]) # Create new "CtrlSum" element in the XML tree using data from - # the CSV file + # the Data file create_xml_element( PmtInf_element, "CtrlSum", f"{row['control_sum']}" ) # Create new "PmtTpInf" element in the XML tree using data from - # the CSV file + # the Data file PmtTpInf_element = ET.Element("PmtTpInf") child_element = ET.Element("SvcLvl") child_element2 = ET.Element("Cd") @@ -87,7 +111,7 @@ def create_xml_v3(root, data, mapping): PmtInf_element.append(PmtTpInf_element) # Create new "ReqdExctnDt" element in the XML tree using data - # from the CSV file + # from the Data file create_xml_element( PmtInf_element, "ReqdExctnDt", @@ -95,7 +119,7 @@ def create_xml_v3(root, data, mapping): ) # Create new "Dbtr" element in the XML tree using data from - # the CSV file + # the Data file Dbtr_element = ET.Element("Dbtr") child_element = ET.Element("Nm") child_element.text = row["debtor_name"] @@ -103,7 +127,7 @@ def create_xml_v3(root, data, mapping): PmtInf_element.append(Dbtr_element) # Create new "DbtrAcct" element in the XML tree using data - # from the CSV file + # from the Data file DbtrAcct_element = ET.Element("DbtrAcct") child_element = ET.Element("Id") child_element2 = ET.Element("IBAN") @@ -114,7 +138,7 @@ def create_xml_v3(root, data, mapping): PmtInf_element.append(DbtrAcct_element) # Create new "DbtrAgt" element in the XML tree using data - # from the CSV file + # from the Data file DbtrAgt_element = ET.Element("DbtrAgt") child_element = ET.Element("FinInstnId") child_element2 = ET.Element("BIC") @@ -125,18 +149,18 @@ def create_xml_v3(root, data, mapping): PmtInf_element.append(DbtrAgt_element) # Create new "ChrgBr" element in the XML tree using data - # from the CSV file + # from the Data file child_element = ET.Element("ChrgBr") # replace with the appropriate value child_element.text = row["charge_bearer"] PmtInf_element.append(child_element) # Create new "CdtTrfTxInf" element in the XML tree using data - # from the CSV file + # from the Data file CdtTrfTxInf_element = ET.Element("CdtTrfTxInf") # Create new "PmtId" element in the XML tree using data - # from the CSV file + # from the Data file PmtId_element = ET.Element("PmtId") child_element = ET.Element("EndToEndId") child_element.text = row["payment_id"] @@ -144,7 +168,7 @@ def create_xml_v3(root, data, mapping): CdtTrfTxInf_element.append(PmtId_element) # Create new "Amt" element in the XML tree using data - # from the CSV file + # from the Data file Amt_element = ET.Element("Amt") child_element = ET.Element("InstdAmt") child_element.text = row["payment_amount"] @@ -153,7 +177,7 @@ def create_xml_v3(root, data, mapping): CdtTrfTxInf_element.append(Amt_element) # Create new "CdtrAgt" element in the XML tree using data - # from the CSV file + # from the Data file CdtrAgt_element = ET.Element("CdtrAgt") child_element = ET.Element("FinInstnId") child_element2 = ET.Element("BIC") @@ -163,7 +187,7 @@ def create_xml_v3(root, data, mapping): CdtTrfTxInf_element.append(CdtrAgt_element) # Create new "Cdtr" element in the XML tree using data - # from the CSV file + # from the Data file Cdtr_element = ET.Element("Cdtr") child_element = ET.Element("Nm") child_element.text = row["creditor_name"] @@ -171,7 +195,7 @@ def create_xml_v3(root, data, mapping): CdtTrfTxInf_element.append(Cdtr_element) # Create new "RmtInf" element in the XML tree using data - # from the CSV file + # from the Data file RmtInf_element = ET.Element("RmtInf") child_element = ET.Element("Ustrd") child_element.text = row["remittance_information"] @@ -191,7 +215,7 @@ def create_xml_v4(root, data, mapping): Args: root: The root element of the XML document. data: A list of dictionaries containing the payment data. - mapping: A dictionary that maps XML element names to CSV column names. + mapping: A dictionary that maps XML element names to Data column names. Returns: The root element of the XML document. @@ -205,7 +229,7 @@ def create_xml_v4(root, data, mapping): GrpHdr_element = ET.Element("GrpHdr") cstmr_cdt_trf_initn_element.append(GrpHdr_element) - # Loop through the first row of the CSV file and create new + # Loop through the first row of the Data file and create new # "MsgId", "CreDtTm" and "NbOfTxs" elements in the XML tree for xml_tag, csv_column in mapping.items(): if xml_tag in ["MsgId", "CreDtTm", "NbOfTxs"]: @@ -319,7 +343,18 @@ def create_xml_v4(root, data, mapping): def create_xml_v9(root, data, mapping): - print("XML v9") + """Creates an XML document for the pain.001.001.09 schema. + + Args: + root (ElementTree.Element): The root element of the XML tree. + data (list): A list of dictionaries containing the data to be added + to the XML document. + mapping (dict): A dictionary mapping the Data column names to the XML + element names. + + Returns: + The root element of the XML tree. + """ # Create CstmrCdtTrfInitn element cstmr_cdt_trf_initn_element = ET.Element("CstmrCdtTrfInitn") @@ -337,7 +372,7 @@ def create_xml_v9(root, data, mapping): ) # Create new "InitgPty" element in the XML tree using data from the - # CSV file + # Data file InitgPty_element = ET.Element("InitgPty") create_xml_element( InitgPty_element, "Nm", data[0]["initiator_name"] @@ -346,7 +381,7 @@ def create_xml_v9(root, data, mapping): for row in data: # Create new "PmtInf" element in the XML tree using data from - # the CSV file + # the Data file PmtInf_element = ET.Element("PmtInf") cstmr_cdt_trf_initn_element.append(PmtInf_element) @@ -359,7 +394,7 @@ def create_xml_v9(root, data, mapping): PmtInf_element.append(Dbtr_element) # Create new "Dbtr" element in the XML tree using data from - # the CSV file + # the Data file Dbtr_element = ET.Element("Dbtr") child_element = ET.Element("Nm") child_element.text = row["debtor_name"] @@ -367,7 +402,7 @@ def create_xml_v9(root, data, mapping): PmtInf_element.append(Dbtr_element) # Create new "DbtrAcct" element in the XML tree using data - # from the CSV file + # from the Data file DbtrAcct_element = ET.Element("DbtrAcct") child_element = ET.Element("Id") child_element2 = ET.Element("IBAN") @@ -378,7 +413,7 @@ def create_xml_v9(root, data, mapping): PmtInf_element.append(DbtrAcct_element) # Create new "DbtrAgt" element in the XML tree using data - # from the CSV file + # from the Data file DbtrAgt_element = ET.Element("DbtrAgt") child_element = ET.Element("FinInstnId") child_element2 = ET.Element("BICFI") @@ -392,18 +427,18 @@ def create_xml_v9(root, data, mapping): PmtInf_element.append(DbtrAgt_element) # Create new "ChrgBr" element in the XML tree using data - # from the CSV file + # from the Data file child_element = ET.Element("ChrgBr") # replace with the appropriate value child_element.text = row["charge_bearer"] PmtInf_element.append(child_element) # Create new "CdtTrfTxInf" element in the XML tree using data - # from the CSV file + # from the Data file CdtTrfTxInf_element = ET.Element("CdtTrfTxInf") # Create new "PmtId" element in the XML tree using data - # from the CSV file + # from the Data file PmtId_element = ET.Element("PmtId") child_element = ET.Element("EndToEndId") child_element.text = row["payment_id"] @@ -411,7 +446,7 @@ def create_xml_v9(root, data, mapping): CdtTrfTxInf_element.append(PmtId_element) # Create new "Amt" element in the XML tree using data - # from the CSV file + # from the Data file Amt_element = ET.Element("Amt") child_element = ET.Element("InstdAmt") child_element.text = row["payment_amount"] @@ -420,7 +455,7 @@ def create_xml_v9(root, data, mapping): CdtTrfTxInf_element.append(Amt_element) # Create new "CdtrAgt" element in the XML tree using data - # from the CSV file + # from the Data file CdtrAgt_element = ET.Element("CdtrAgt") child_element = ET.Element("FinInstnId") child_element2 = ET.Element("BICFI") @@ -434,7 +469,7 @@ def create_xml_v9(root, data, mapping): CdtTrfTxInf_element.append(CdtrAgt_element) # Create new "Cdtr" element in the XML tree using data - # from the CSV file + # from the Data file Cdtr_element = ET.Element("Cdtr") child_element = ET.Element("Nm") child_element.text = row["creditor_name"] @@ -442,7 +477,7 @@ def create_xml_v9(root, data, mapping): CdtTrfTxInf_element.append(Cdtr_element) # Create new "RmtInf" element in the XML tree using data - # from the CSV file + # from the Data file RmtInf_element = ET.Element("RmtInf") child_element = ET.Element("Ustrd") child_element.text = row["remittance_information"] @@ -456,7 +491,7 @@ def create_xml_v9(root, data, mapping): cstmr_cdt_trf_initn_element.append(PmtInf_element) # Create new "SplmtryData" elements in the XML tree using data - # from the CSV file" + # from the Data file" # Create the main elements SplmtryData_element = ET.Element("SplmtryData") Envlp_element = ET.SubElement(SplmtryData_element, "Envlp") diff --git a/pyproject.toml b/pyproject.toml index f9545f0..665ea2a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,11 +9,11 @@ repository = "https://github.com/sebastienrousseau/pain001" homepage = "https://pain001.com" [tool.poetry.dependencies] -python = "^3.9" -xmlschema = "^2.3.0" click = "^8.1.3" defusedxml = "^0.7.1" +python = "^3.9" rich = "^13.4.2" +xmlschema = "^2.3.1" [build-system] requires = ["poetry-core"] diff --git a/setup.py b/setup.py index 6829816..8173568 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ ] TEST_DEPENDENCIES = [ - "pytest>=7.3.1", + "pytest>=7.3.2", ] NAME = "pain001" diff --git a/templates/pain.001.001.03/pain.001.001.03.xml b/templates/pain.001.001.03/pain.001.001.03.xml index 04f0b71..54b7ea9 100644 --- a/templates/pain.001.001.03/pain.001.001.03.xml +++ b/templates/pain.001.001.03/pain.001.001.03.xml @@ -1,7 +1,5 @@ - - + + 1 @@ -472,4 +470,4 @@ - \ No newline at end of file +