Skip to content

Commit

Permalink
Add generation CLI Damdfe
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianoMafraJunior committed Nov 11, 2024
1 parent 71430d4 commit 1c74d4e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
57 changes: 49 additions & 8 deletions brazilfiscalreport/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ dependencies = [
dacte = [
"qrcode",
]
# DAMDFE specific dependencies
damdfe = [
"qrcode",
]
# Dependencies CLI
cli = [
"click",
"PyYAML",
Expand Down
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1c74d4e

Please sign in to comment.