Skip to content

Commit

Permalink
fix(setup): correct setup and templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 19, 2024
1 parent dc85db4 commit 38517bb
Show file tree
Hide file tree
Showing 50 changed files with 463 additions and 342 deletions.
49 changes: 24 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ Pain.001.001.03.
```sh
python -m pain001 \
-t pain.001.001.03 \
-m templates/pain.001.001.03/template.xml \
-s templates/pain.001.001.03/pain.001.001.03.xsd \
-d templates/pain.001.001.03/template.csv
-m pain001/templates/pain.001.001.03/template.xml \
-s pain001/templates/pain.001.001.03/pain.001.001.03.xsd \
-d pain001/templates/pain.001.001.03/template.csv
```

#### Pain.001.001.04
Expand All @@ -282,9 +282,9 @@ Pain.001.001.04.
```sh
python -m pain001 \
-t pain.001.001.04 \
-m templates/pain.001.001.04/template.xml \
-s templates/pain.001.001.04/pain.001.001.04.xsd \
-d templates/pain.001.001.04/template.csv
-m pain001/templates/pain.001.001.04/template.xml \
-s pain001/templates/pain.001.001.04/pain.001.001.04.xsd \
-d pain001/templates/pain.001.001.04/template.csv
```

#### Pain.001.001.05
Expand All @@ -295,9 +295,9 @@ Pain.001.001.05.
```sh
python -m pain001 \
-t pain.001.001.05 \
-m templates/pain.001.001.05/template.xml \
-s templates/pain.001.001.05/pain.001.001.05.xsd \
-d templates/pain.001.001.05/template.csv
-m pain001/templates/pain.001.001.05/template.xml \
-s pain001/templates/pain.001.001.05/pain.001.001.05.xsd \
-d pain001/templates/pain.001.001.05/template.csv
```

#### Pain.001.001.06
Expand All @@ -308,9 +308,9 @@ Pain.001.001.06.
```sh
python -m pain001 \
-t pain.001.001.06 \
-m templates/pain.001.001.06/template.xml \
-s templates/pain.001.001.06/pain.001.001.06.xsd \
-d templates/pain.001.001.06/template.csv
-m pain001/templates/pain.001.001.06/template.xml \
-s pain001/templates/pain.001.001.06/pain.001.001.06.xsd \
-d pain001/templates/pain.001.001.06/template.csv
```

#### Pain.001.001.07
Expand All @@ -321,9 +321,9 @@ Pain.001.001.07.
```sh
python -m pain001 \
-t pain.001.001.07 \
-m templates/pain.001.001.07/template.xml \
-s templates/pain.001.001.07/pain.001.001.07.xsd \
-d templates/pain.001.001.07/template.csv
-m pain001/templates/pain.001.001.07/template.xml \
-s pain001/templates/pain.001.001.07/pain.001.001.07.xsd \
-d pain001/templates/pain.001.001.07/template.csv
```

#### Pain.001.001.08
Expand All @@ -334,9 +334,9 @@ Pain.001.001.08.
```sh
python -m pain001 \
-t pain.001.001.08 \
-m templates/pain.001.001.08/template.xml \
-s templates/pain.001.001.08/pain.001.001.08.xsd \
-d templates/pain.001.001.08/template.csv
-m pain001/templates/pain.001.001.08/template.xml \
-s pain001/templates/pain.001.001.08/pain.001.001.08.xsd \
-d pain001/templates/pain.001.001.08/template.csv
```

#### Pain.001.001.09
Expand All @@ -347,19 +347,19 @@ Pain.001.001.09.
```sh
python -m pain001 \
-t pain.001.001.09 \
-m templates/pain.001.001.09/template.xml \
-s templates/pain.001.001.09/pain.001.001.09.xsd \
-d templates/pain.001.001.09/template.csv
-m pain001/templates/pain.001.001.09/template.xml \
-s pain001/templates/pain.001.001.09/pain.001.001.09.xsd \
-d pain001/templates/pain.001.001.09/template.csv
```

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

```sh
python3 -m pain001 \
-t pain.001.001.03 \
-m templates/pain.001.001.03/template.xml \
-s templates/pain.001.001.03/pain.001.001.03.xsd \
-d templates/pain.001.001.03/template.db
-m pain001/templates/pain.001.001.03/template.xml \
-s pain001/templates/pain.001.001.03/pain.001.001.03.xsd \
-d pain001/templates/pain.001.001.03/template.db
```

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


[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.'
[codecov-badge]: https://img.shields.io/codecov/c/github/sebastienrousseau/pain001?style=for-the-badge&token=AaUxKfRiou 'Codecov badge'
[license-badge]: https://img.shields.io/pypi/l/pain001?style=for-the-badge 'License badge'
Expand Down
12 changes: 7 additions & 5 deletions pain001/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,26 @@ def main(
try:
# Check that the required arguments are provided
if not xml_message_type:
console.print("The XML message type is required.")
console.print(
"The XML message type is required. Use -h for help.\n"
)
sys.exit(1)

if not xml_template_file_path:
console.print("The XML template file path is required.")
console.print("The XML template file path is required.\n")
sys.exit(1)

if not xsd_schema_file_path:
console.print("The XSD schema file path is required.")
console.print("The XSD schema file path is required.\n")
sys.exit(1)

if not data_file_path:
console.print("The data file path is required.")
console.print("The data file path is required.\n")
sys.exit(1)

logger = Context.get_instance().get_logger()

logger.info("Parsing command line arguments.")
logger.info("Parsing command line arguments.\n")

# Check that the XML message type is valid
if xml_message_type not in valid_xml_types:
Expand Down
161 changes: 161 additions & 0 deletions pain001/cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Copyright (C) 2023 Sebastien Rousseau.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import click
import configparser

from pain001.constants.constants import valid_xml_types
from pain001.context.context import Context
from pain001.core.core import process_files
from pain001.xml.validate_via_xsd import validate_via_xsd

from rich.console import Console
from rich.table import Table
from rich import box

console = Console()

description = """
A powerful Python library that enables you to create
ISO 20022-compliant payment files directly from CSV or SQLite Data files.\n
https://pain001.com
"""
title = "Pain001"

table = Table(box=box.ROUNDED, safe_box=True, show_header=False, title=title)

table.add_column(justify="center", no_wrap=False, vertical="middle")
table.add_row(description)
table.width = 80
console.print(table)


@click.command(
help=("To use Pain001, you must specify the following options:\n\n"),
context_settings=dict(help_option_names=["-h", "--help"]),
)
@click.option(
"-t",
"--xml_message_type",
default=None,
help="Type of XML message (required)",
)
@click.option(
"-m",
"--xml_template_file_path",
default=None,
type=click.Path(),
help="Path to XML template file (required)",
)
@click.option(
"-s",
"--xsd_schema_file_path",
default=None,
type=click.Path(),
help="Path to XSD template file (required)",
)
@click.option(
"-d",
"--data_file_path",
default=None,
type=click.Path(),
help="Path to data file (CSV or SQLite) (required)",
)
@click.option(
"-c",
"--config_file",
default=None,
type=click.Path(),
help="Path to configuration file (optional)",
)
def main(
xml_message_type,
xml_template_file_path,
xsd_schema_file_path,
data_file_path,
config_file,
):
# Expand user-friendly paths
xml_template_file_path = os.path.expanduser(xml_template_file_path)
xsd_schema_file_path = os.path.expanduser(xsd_schema_file_path)
data_file_path = os.path.expanduser(data_file_path)

# Load configuration file if provided
if config_file:
config = configparser.ConfigParser()
config.read(config_file)
if "Paths" in config:
xml_template_file_path = config["Paths"].get(
"xml_template_file_path", xml_template_file_path
)
xsd_schema_file_path = config["Paths"].get(
"xsd_schema_file_path", xsd_schema_file_path
)
data_file_path = config["Paths"].get(
"data_file_path", data_file_path
)

# Check that the required arguments are provided
if not xml_message_type:
print("The XML message type is required.")
sys.exit(1)

# Check file existence
for file_path in [
xml_template_file_path,
xsd_schema_file_path,
data_file_path,
]:
if not os.path.isfile(file_path):
print(f"The file '{file_path}' does not exist.")
sys.exit(1)

logger = Context.get_instance().get_logger()

logger.info("Parsing command line arguments.")

# Check that the XML message type is valid
if xml_message_type not in valid_xml_types:
logger.info(f"Invalid XML message type: {xml_message_type}.")
print(
f"""
Invalid XML message type: {xml_message_type}.
Valid types are: {', '.join(valid_xml_types)}.
"""
)
sys.exit(1)

# Validate XML and XSD schemas
try:
validate_via_xsd(xml_template_file_path, xsd_schema_file_path)
except Exception as e:
logger.error(f"Schema validation failed: {e}")
print(f"Schema validation failed: {e}")
sys.exit(1)

process_files(
xml_message_type,
xml_template_file_path,
xsd_schema_file_path,
data_file_path,
)


if __name__ == "__main__":
# pylint: disable=no-value-for-parameter
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion pain001/xml/create_xml_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def create_xml_v3(root, data):
env = Environment(loader=FileSystemLoader("."), autoescape=True)

# Load the Jinja2 template for the pain.001.001.03 schema
template = env.get_template("templates/pain.001.001.03/template.xml")
template = env.get_template(
"pain001/templates/pain.001.001.03/template.xml"
)

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

0 comments on commit 38517bb

Please sign in to comment.