Skip to content

Commit

Permalink
feat(json): output json for ebd section with no table (#96)
Browse files Browse the repository at this point in the history
* 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 <konstantin.klein@hochfrequenz.de>

* typo

---------

Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de>
Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent 4174550 commit a2c763d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers = [
]
dependencies = [
"ebdamame>=0.4.0",
"rebdhuhn>=0.5.2",
"rebdhuhn>=0.5.3",
"cattrs",
"click",
"pydantic-settings"
Expand Down
32 changes: 28 additions & 4 deletions src/ebd_toolchain/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a2c763d

Please sign in to comment.