Skip to content

Commit 38517bb

Browse files
fix(setup): correct setup and templates
1 parent dc85db4 commit 38517bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+463
-342
lines changed

README.md

+24-25
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ Pain.001.001.03.
269269
```sh
270270
python -m pain001 \
271271
-t pain.001.001.03 \
272-
-m templates/pain.001.001.03/template.xml \
273-
-s templates/pain.001.001.03/pain.001.001.03.xsd \
274-
-d templates/pain.001.001.03/template.csv
272+
-m pain001/templates/pain.001.001.03/template.xml \
273+
-s pain001/templates/pain.001.001.03/pain.001.001.03.xsd \
274+
-d pain001/templates/pain.001.001.03/template.csv
275275
```
276276

277277
#### Pain.001.001.04
@@ -282,9 +282,9 @@ Pain.001.001.04.
282282
```sh
283283
python -m pain001 \
284284
-t pain.001.001.04 \
285-
-m templates/pain.001.001.04/template.xml \
286-
-s templates/pain.001.001.04/pain.001.001.04.xsd \
287-
-d templates/pain.001.001.04/template.csv
285+
-m pain001/templates/pain.001.001.04/template.xml \
286+
-s pain001/templates/pain.001.001.04/pain.001.001.04.xsd \
287+
-d pain001/templates/pain.001.001.04/template.csv
288288
```
289289

290290
#### Pain.001.001.05
@@ -295,9 +295,9 @@ Pain.001.001.05.
295295
```sh
296296
python -m pain001 \
297297
-t pain.001.001.05 \
298-
-m templates/pain.001.001.05/template.xml \
299-
-s templates/pain.001.001.05/pain.001.001.05.xsd \
300-
-d templates/pain.001.001.05/template.csv
298+
-m pain001/templates/pain.001.001.05/template.xml \
299+
-s pain001/templates/pain.001.001.05/pain.001.001.05.xsd \
300+
-d pain001/templates/pain.001.001.05/template.csv
301301
```
302302

303303
#### Pain.001.001.06
@@ -308,9 +308,9 @@ Pain.001.001.06.
308308
```sh
309309
python -m pain001 \
310310
-t pain.001.001.06 \
311-
-m templates/pain.001.001.06/template.xml \
312-
-s templates/pain.001.001.06/pain.001.001.06.xsd \
313-
-d templates/pain.001.001.06/template.csv
311+
-m pain001/templates/pain.001.001.06/template.xml \
312+
-s pain001/templates/pain.001.001.06/pain.001.001.06.xsd \
313+
-d pain001/templates/pain.001.001.06/template.csv
314314
```
315315

316316
#### Pain.001.001.07
@@ -321,9 +321,9 @@ Pain.001.001.07.
321321
```sh
322322
python -m pain001 \
323323
-t pain.001.001.07 \
324-
-m templates/pain.001.001.07/template.xml \
325-
-s templates/pain.001.001.07/pain.001.001.07.xsd \
326-
-d templates/pain.001.001.07/template.csv
324+
-m pain001/templates/pain.001.001.07/template.xml \
325+
-s pain001/templates/pain.001.001.07/pain.001.001.07.xsd \
326+
-d pain001/templates/pain.001.001.07/template.csv
327327
```
328328

329329
#### Pain.001.001.08
@@ -334,9 +334,9 @@ Pain.001.001.08.
334334
```sh
335335
python -m pain001 \
336336
-t pain.001.001.08 \
337-
-m templates/pain.001.001.08/template.xml \
338-
-s templates/pain.001.001.08/pain.001.001.08.xsd \
339-
-d templates/pain.001.001.08/template.csv
337+
-m pain001/templates/pain.001.001.08/template.xml \
338+
-s pain001/templates/pain.001.001.08/pain.001.001.08.xsd \
339+
-d pain001/templates/pain.001.001.08/template.csv
340340
```
341341

342342
#### Pain.001.001.09
@@ -347,19 +347,19 @@ Pain.001.001.09.
347347
```sh
348348
python -m pain001 \
349349
-t pain.001.001.09 \
350-
-m templates/pain.001.001.09/template.xml \
351-
-s templates/pain.001.001.09/pain.001.001.09.xsd \
352-
-d templates/pain.001.001.09/template.csv
350+
-m pain001/templates/pain.001.001.09/template.xml \
351+
-s pain001/templates/pain.001.001.09/pain.001.001.09.xsd \
352+
-d pain001/templates/pain.001.001.09/template.csv
353353
```
354354

355355
You can do the same with the sample SQLite Data file:
356356

357357
```sh
358358
python3 -m pain001 \
359359
-t pain.001.001.03 \
360-
-m templates/pain.001.001.03/template.xml \
361-
-s templates/pain.001.001.03/pain.001.001.03.xsd \
362-
-d templates/pain.001.001.03/template.db
360+
-m pain001/templates/pain.001.001.03/template.xml \
361+
-s pain001/templates/pain.001.001.03/pain.001.001.03.xsd \
362+
-d pain001/templates/pain.001.001.03/template.db
363363
```
364364

365365
> **Note:** The XML file that **Pain001** generates will automatically be
@@ -497,7 +497,6 @@ We would like to extend a big thank you to all the awesome contributors of
497497
[06]: https://codecov.io/github/sebastienrousseau/pain001?branch=main
498498
[07]: https://pypi.org/project/pain001/
499499

500-
501500
[banner]: https://kura.pro/pain001/images/banners/banner-pain001.svg 'Pain001, A Python Library for Automating ISO 20022-Compliant Payment Files Using CSV Or SQlite Data Files.'
502501
[codecov-badge]: https://img.shields.io/codecov/c/github/sebastienrousseau/pain001?style=for-the-badge&token=AaUxKfRiou 'Codecov badge'
503502
[license-badge]: https://img.shields.io/pypi/l/pain001?style=for-the-badge 'License badge'

pain001/__main__.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,26 @@ def main(
7979
try:
8080
# Check that the required arguments are provided
8181
if not xml_message_type:
82-
console.print("The XML message type is required.")
82+
console.print(
83+
"The XML message type is required. Use -h for help.\n"
84+
)
8385
sys.exit(1)
8486

8587
if not xml_template_file_path:
86-
console.print("The XML template file path is required.")
88+
console.print("The XML template file path is required.\n")
8789
sys.exit(1)
8890

8991
if not xsd_schema_file_path:
90-
console.print("The XSD schema file path is required.")
92+
console.print("The XSD schema file path is required.\n")
9193
sys.exit(1)
9294

9395
if not data_file_path:
94-
console.print("The data file path is required.")
96+
console.print("The data file path is required.\n")
9597
sys.exit(1)
9698

9799
logger = Context.get_instance().get_logger()
98100

99-
logger.info("Parsing command line arguments.")
101+
logger.info("Parsing command line arguments.\n")
100102

101103
# Check that the XML message type is valid
102104
if xml_message_type not in valid_xml_types:

pain001/cli/cli.py

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright (C) 2023 Sebastien Rousseau.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
# implied.
13+
#
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import os
18+
import sys
19+
import click
20+
import configparser
21+
22+
from pain001.constants.constants import valid_xml_types
23+
from pain001.context.context import Context
24+
from pain001.core.core import process_files
25+
from pain001.xml.validate_via_xsd import validate_via_xsd
26+
27+
from rich.console import Console
28+
from rich.table import Table
29+
from rich import box
30+
31+
console = Console()
32+
33+
description = """
34+
A powerful Python library that enables you to create
35+
ISO 20022-compliant payment files directly from CSV or SQLite Data files.\n
36+
https://pain001.com
37+
"""
38+
title = "Pain001"
39+
40+
table = Table(box=box.ROUNDED, safe_box=True, show_header=False, title=title)
41+
42+
table.add_column(justify="center", no_wrap=False, vertical="middle")
43+
table.add_row(description)
44+
table.width = 80
45+
console.print(table)
46+
47+
48+
@click.command(
49+
help=("To use Pain001, you must specify the following options:\n\n"),
50+
context_settings=dict(help_option_names=["-h", "--help"]),
51+
)
52+
@click.option(
53+
"-t",
54+
"--xml_message_type",
55+
default=None,
56+
help="Type of XML message (required)",
57+
)
58+
@click.option(
59+
"-m",
60+
"--xml_template_file_path",
61+
default=None,
62+
type=click.Path(),
63+
help="Path to XML template file (required)",
64+
)
65+
@click.option(
66+
"-s",
67+
"--xsd_schema_file_path",
68+
default=None,
69+
type=click.Path(),
70+
help="Path to XSD template file (required)",
71+
)
72+
@click.option(
73+
"-d",
74+
"--data_file_path",
75+
default=None,
76+
type=click.Path(),
77+
help="Path to data file (CSV or SQLite) (required)",
78+
)
79+
@click.option(
80+
"-c",
81+
"--config_file",
82+
default=None,
83+
type=click.Path(),
84+
help="Path to configuration file (optional)",
85+
)
86+
def main(
87+
xml_message_type,
88+
xml_template_file_path,
89+
xsd_schema_file_path,
90+
data_file_path,
91+
config_file,
92+
):
93+
# Expand user-friendly paths
94+
xml_template_file_path = os.path.expanduser(xml_template_file_path)
95+
xsd_schema_file_path = os.path.expanduser(xsd_schema_file_path)
96+
data_file_path = os.path.expanduser(data_file_path)
97+
98+
# Load configuration file if provided
99+
if config_file:
100+
config = configparser.ConfigParser()
101+
config.read(config_file)
102+
if "Paths" in config:
103+
xml_template_file_path = config["Paths"].get(
104+
"xml_template_file_path", xml_template_file_path
105+
)
106+
xsd_schema_file_path = config["Paths"].get(
107+
"xsd_schema_file_path", xsd_schema_file_path
108+
)
109+
data_file_path = config["Paths"].get(
110+
"data_file_path", data_file_path
111+
)
112+
113+
# Check that the required arguments are provided
114+
if not xml_message_type:
115+
print("The XML message type is required.")
116+
sys.exit(1)
117+
118+
# Check file existence
119+
for file_path in [
120+
xml_template_file_path,
121+
xsd_schema_file_path,
122+
data_file_path,
123+
]:
124+
if not os.path.isfile(file_path):
125+
print(f"The file '{file_path}' does not exist.")
126+
sys.exit(1)
127+
128+
logger = Context.get_instance().get_logger()
129+
130+
logger.info("Parsing command line arguments.")
131+
132+
# Check that the XML message type is valid
133+
if xml_message_type not in valid_xml_types:
134+
logger.info(f"Invalid XML message type: {xml_message_type}.")
135+
print(
136+
f"""
137+
Invalid XML message type: {xml_message_type}.
138+
Valid types are: {', '.join(valid_xml_types)}.
139+
"""
140+
)
141+
sys.exit(1)
142+
143+
# Validate XML and XSD schemas
144+
try:
145+
validate_via_xsd(xml_template_file_path, xsd_schema_file_path)
146+
except Exception as e:
147+
logger.error(f"Schema validation failed: {e}")
148+
print(f"Schema validation failed: {e}")
149+
sys.exit(1)
150+
151+
process_files(
152+
xml_message_type,
153+
xml_template_file_path,
154+
xsd_schema_file_path,
155+
data_file_path,
156+
)
157+
158+
159+
if __name__ == "__main__":
160+
# pylint: disable=no-value-for-parameter
161+
main()

pain001/xml/create_xml_v3.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def create_xml_v3(root, data):
5858
env = Environment(loader=FileSystemLoader("."), autoescape=True)
5959

6060
# Load the Jinja2 template for the pain.001.001.03 schema
61-
template = env.get_template("templates/pain.001.001.03/template.xml")
61+
template = env.get_template(
62+
"pain001/templates/pain.001.001.03/template.xml"
63+
)
6264

6365
# Prepare the data dictionary for rendering through the Jinja2 template
6466
# This dictionary is a reformatted version of the `data` parameter, made to

0 commit comments

Comments
 (0)