From a2c763d8548d649b29bb12b796030c7171c3dde7 Mon Sep 17 00:00:00 2001 From: Daniel <139119540+DeltaDaniel@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:37:14 +0100 Subject: [PATCH] feat(json): output json for ebd section with no table (#96) * output json for ebd section with no table * fix import error * linter * like this? * WIP * linting * bump rebdhuhn 0.5.2 -> 0.5.3 * Update src/ebd_toolchain/main.py Co-authored-by: konstantin * typo --------- Co-authored-by: Konstantin Co-authored-by: konstantin --- pyproject.toml | 2 +- src/ebd_toolchain/main.py | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 298a312..12f4feb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ classifiers = [ ] dependencies = [ "ebdamame>=0.4.0", - "rebdhuhn>=0.5.2", + "rebdhuhn>=0.5.3", "cattrs", "click", "pydantic-settings" diff --git a/src/ebd_toolchain/main.py b/src/ebd_toolchain/main.py index 7979af4..18f5e0a 100644 --- a/src/ebd_toolchain/main.py +++ b/src/ebd_toolchain/main.py @@ -38,7 +38,7 @@ from rebdhuhn.graphviz import convert_dot_to_svg_kroki, convert_graph_to_dot from rebdhuhn.kroki import DotToSvgConverter, Kroki, KrokiDotBadRequestError, KrokiPlantUmlBadRequestError from rebdhuhn.models.ebd_graph import EbdGraph -from rebdhuhn.models.ebd_table import EbdTable +from rebdhuhn.models.ebd_table import EbdTable, EbdTableMetaData from rebdhuhn.models.errors import ( EbdCrossReferenceNotSupportedError, EndeInWrongColumnError, @@ -80,9 +80,19 @@ def _dump_svg(svg_path: Path, ebd_graph: EbdGraph, converter: DotToSvgConverter) svg_file.write(svg_code) -def _dump_json(json_path: Path, ebd_table: EbdTable) -> None: +def _dump_json(json_path: Path, ebd_table: EbdTable | EbdTableMetaData) -> None: with open(json_path, "w+", encoding="utf-8") as json_file: json.dump(cattrs.unstructure(ebd_table), json_file, ensure_ascii=False, indent=2, sort_keys=True) + if isinstance(ebd_table, EbdTableMetaData): + json.dump( + cattrs.unstructure(EbdTable(metadata=ebd_table, rows=[])), + json_file, + ensure_ascii=False, + indent=2, + sort_keys=True, + ) + else: + json.dump(cattrs.unstructure(ebd_table), json_file, ensure_ascii=False, indent=2, sort_keys=True) @click.command() @@ -143,10 +153,24 @@ def handle_known_error(error: Exception, ebd_key: str) -> None: click.secho(f"Table not found: {ebd_key}: {str(table_not_found_error)}; Skip!", fg="yellow") continue assert ebd_kapitel is not None + assert ebd_kapitel.subsection_title is not None if isinstance(docx_tables, EbdNoTableSection): - _logger.warning("The EBD has no table: %s", ebd_key) - continue + if "json" in export_types: + ebd_meta_data = EbdTableMetaData( + ebd_code=ebd_key, + ebd_name=f"{ebd_kapitel.subsection_title}", + chapter=ebd_kapitel.chapter_title, # type:ignore[arg-type] + # pylint:disable=line-too-long + section=f"{ebd_kapitel.chapter}.{ebd_kapitel.section}.{ebd_kapitel.subsection}: {ebd_kapitel.section_title}", + role="N/A", + remark=docx_tables.remark, + ) + json_path = output_path / Path(f"{ebd_key}.json") + _dump_json(json_path, ebd_meta_data) + click.secho(f"💾 Successfully exported '{ebd_key}.json' to {json_path.absolute()}") + continue try: + assert not isinstance(docx_tables, EbdNoTableSection) converter = DocxTableConverter( docx_tables, ebd_key=ebd_key,