From 1c74d4e6fddd4a30dd585619345e6277a6a22b84 Mon Sep 17 00:00:00 2001 From: CristianoMafraJunior Date: Mon, 11 Nov 2024 10:01:03 -0300 Subject: [PATCH] Add generation CLI Damdfe --- .github/workflows/tests.yml | 2 +- brazilfiscalreport/cli.py | 57 +++++++++++++++++++++++++++++++------ pyproject.toml | 5 ++++ tests/test_cli.py | 33 +++++++++++++++++++++ 4 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 tests/test_cli.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8165714..1f8c8d6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,7 @@ jobs: run: sudo apt-get update --allow-releaseinfo-change && sudo apt-get install qpdf - name: Install dependencies run: | - python -m pip install --upgrade pip setuptools fpdf2 phonenumbers python-barcode pytest qrcode pytest-cov + python -m pip install --upgrade pip setuptools fpdf2 phonenumbers python-barcode pytest qrcode pytest-cov click PyYAML - name: Test run: | pytest --cov=./brazilfiscalreport --cov-report=xml --cov-branch --doctest-glob="docs/*.md" diff --git a/brazilfiscalreport/cli.py b/brazilfiscalreport/cli.py index ba69337..4db2841 100644 --- a/brazilfiscalreport/cli.py +++ b/brazilfiscalreport/cli.py @@ -73,10 +73,10 @@ def generate_danfe(xml): config_data = load_config() logo = config_data.get("LOGO") - top = config_data.get("TOP_MARGIN", 5) - right = config_data.get("RIGHT_MARGIN", 5) - bottom = config_data.get("BOTTOM_MARGIN", 5) - left = config_data.get("LEFT_MARGIN", 5) + top = config_data.get("TOP_MARGIN", danfe.Margins.top) + right = config_data.get("RIGHT_MARGIN", danfe.Margins.right) + bottom = config_data.get("BOTTOM_MARGIN", danfe.Margins.bottom) + left = config_data.get("LEFT_MARGIN", danfe.Margins.left) xml_path = Path(xml).resolve() output_path = Path.cwd() / xml_path.stem @@ -114,10 +114,10 @@ def generate_dacte(xml): config_data = load_config() logo = config_data.get("LOGO") - top = config_data.get("TOP_MARGIN", 5) - right = config_data.get("RIGHT_MARGIN", 5) - bottom = config_data.get("BOTTOM_MARGIN", 5) - left = config_data.get("LEFT_MARGIN", 5) + top = config_data.get("TOP_MARGIN", dacte.Margins.top) + right = config_data.get("RIGHT_MARGIN", dacte.Margins.right) + bottom = config_data.get("BOTTOM_MARGIN", dacte.Margins.bottom) + left = config_data.get("LEFT_MARGIN", dacte.Margins.left) xml_path = Path(xml).resolve() output_path = Path.cwd() / xml_path.stem @@ -141,5 +141,46 @@ def generate_dacte(xml): click.echo(f"DACTE generated successfully: {output_path}") +@cli.command("damdfe") +@click.argument("xml", type=click.Path(exists=True)) +def generate_damdfe(xml): + try: + from brazilfiscalreport import damdfe + except ImportError: + click.echo( + "Error: The brazilfiscalreport package " + "or its damdfe module is not installed." + ) + return + + config_data = load_config() + logo = config_data.get("LOGO") + top = config_data.get("TOP_MARGIN", damdfe.Margins.top) + right = config_data.get("RIGHT_MARGIN", damdfe.Margins.right) + bottom = config_data.get("BOTTOM_MARGIN", damdfe.Margins.bottom) + left = config_data.get("LEFT_MARGIN", damdfe.Margins.left) + + xml_path = Path(xml).resolve() + output_path = Path.cwd() / xml_path.stem + output_path = output_path.with_suffix(".pdf") + logo_path = Path(logo).resolve() if logo else None + + if logo_path and not logo_path.exists(): + click.echo("Logo file not found, proceeding without logo.") + logo_path = None + + with open(xml_path, encoding="utf-8") as xml_file: + xml_content = xml_file.read() + + config = damdfe.DamdfeConfig( + margins=damdfe.Margins(top=top, right=right, bottom=bottom, left=left), + logo=logo_path, + ) + + damdfe_instance = damdfe.Damdfe(xml=xml_content, config=config) + damdfe_instance.output(output_path) + click.echo(f"DAMDFE generated successfully: {output_path}") + + if __name__ == "__main__": cli() diff --git a/pyproject.toml b/pyproject.toml index 0524ae8..eea5335 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,6 +42,11 @@ dependencies = [ dacte = [ "qrcode", ] +# DAMDFE specific dependencies +damdfe = [ + "qrcode", +] +# Dependencies CLI cli = [ "click", "PyYAML", diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..7fa0300 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,33 @@ +import pytest +from click.testing import CliRunner + +from brazilfiscalreport.cli import cli + + +@pytest.fixture +def runner(): + return CliRunner() + + +def test_generate_dacce(runner): + xml_path = "tests/fixtures/xml_cce_1.xml" + result = runner.invoke(cli, ["dacce", xml_path]) + assert result.exit_code == 0, result.output + + +def test_generate_danfe(runner): + xml_path = "tests/fixtures/nfe_test_1.xml" + result = runner.invoke(cli, ["danfe", xml_path]) + assert result.exit_code == 0, result.output + + +def test_generate_dacte(runner): + xml_path = "tests/fixtures/dacte_test_1.xml" + result = runner.invoke(cli, ["dacte", xml_path]) + assert result.exit_code == 0, result.output + + +def test_generate_damdfe(runner): + xml_path = "tests/fixtures/mdf-e_test_1.xml" + result = runner.invoke(cli, ["damdfe", xml_path]) + assert result.exit_code == 0, result.output