Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(json): output json for ebd section with no table #96

Merged
merged 13 commits into from
Nov 7, 2024
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ python-docx==1.1.2
# via ebdamame
python-dotenv==1.0.1
# via pydantic-settings
rebdhuhn==0.5.2
rebdhuhn==0.5.3
# via
# ebd-toolchain (pyproject.toml)
# ebdamame
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