diff --git a/main.py b/main.py index dc112b4..6490b29 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,13 @@ from ebdtable2graph import convert_graph_to_plantuml, convert_table_to_graph from ebdtable2graph.graphviz import convert_dot_to_svg_kroki, convert_graph_to_dot from ebdtable2graph.models import EbdGraph, EbdTable -from ebdtable2graph.plantuml import GraphToComplexForPlantumlError +from ebdtable2graph.models.errors import ( + EbdCrossReferenceNotSupportedError, + EndeInWrongColumnError, + NotExactlyTwoOutgoingEdgesError, + PathsNotGreaterThanOneError, +) +from ebdtable2graph.plantuml import GraphTooComplexForPlantumlError # pylint:disable=import-error from ebddocx2table import TableNotFoundError, get_all_ebd_keys, get_ebd_docx_tables # type:ignore[import] @@ -72,7 +78,7 @@ def _dump_json(json_path: Path, ebd_table: EbdTable) -> None: multiple=True, help="Choose which file you'd like to create", ) -# pylint:disable=too-many-locals, too-many-branches +# pylint:disable=too-many-locals, too-many-branches, too-many-statements, def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", "dot", "json", "svg"]]): """ A program to get a machine-readable version of the AHBs docx files published by edi@energy. @@ -81,14 +87,22 @@ def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", click.secho(f"The output directory '{output_path}' exists already.", fg="yellow") else: output_path.mkdir(parents=True) - click.secho(f"Created a new directory at {output_path}", fg="yellow") + click.secho(f"Created a new directory at {output_path}", fg="green") all_ebd_keys = get_all_ebd_keys(input_path) + error_sources: dict[type, list[str]] = {} + + def handle_known_error(error: Exception, ebd_key: str) -> None: + click.secho(f"Error while processing EBD {ebd_key}: {error}", fg="yellow") + if type(error) not in error_sources: + error_sources[type(error)] = [] + error_sources[type(error)].append(ebd_key) + for ebd_key, (ebd_title, ebd_kapitel) in all_ebd_keys.items(): click.secho(f"Processing EBD {ebd_kapitel} '{ebd_key}' ({ebd_title})") try: docx_tables = get_ebd_docx_tables(docx_file_path=input_path, ebd_key=ebd_key) except TableNotFoundError as table_not_found_error: - click.secho(f"Table not found: {ebd_key}: {str(table_not_found_error)}; Skip!", fg="red") + click.secho(f"Table not found: {ebd_key}: {str(table_not_found_error)}; Skip!", fg="yellow") continue assert ebd_kapitel is not None try: @@ -108,8 +122,11 @@ def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", click.secho(f"💾 Successfully exported '{ebd_key}.json'") try: ebd_graph = convert_table_to_graph(ebd_table) - except Exception as graphing_error: # pylint:disable=broad-except - click.secho(f"Error while graphing {ebd_key}: {str(graphing_error)}; Skip!", fg="yellow") + except (EbdCrossReferenceNotSupportedError, EndeInWrongColumnError) as known_issue: + handle_known_error(known_issue, ebd_key) + continue + except Exception as unknown_error: # pylint:disable=broad-except + click.secho(f"Error while graphing {ebd_key}: {str(unknown_error)}; Skip!", fg="red") continue if "puml" in export_types: try: @@ -118,8 +135,11 @@ def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", except AssertionError as assertion_error: # https://github.com/Hochfrequenz/ebdtable2graph/issues/35 click.secho(str(assertion_error), fg="red") - except GraphToComplexForPlantumlError as too_complex_error: - click.secho(str(too_complex_error), fg="red") + except (NotExactlyTwoOutgoingEdgesError, GraphTooComplexForPlantumlError) as known_issue: + handle_known_error(known_issue, ebd_key) + except Exception as general_error: # pylint:disable=broad-exception-caught + click.secho(f"Error while exporting {ebd_key} as UML: {str(general_error)}; Skip!", fg="yellow") + try: if "dot" in export_types: _dump_dot(output_path / Path(f"{ebd_key}.dot"), ebd_graph) @@ -127,11 +147,13 @@ def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", if "svg" in export_types: _dump_svg(output_path / Path(f"{ebd_key}.svg"), ebd_graph) click.secho(f"💾 Successfully exported '{ebd_key}.svg'") + except PathsNotGreaterThanOneError as known_issue: + handle_known_error(known_issue, ebd_key) except AssertionError as assertion_error: # e.g. AssertionError: If indegree > 1, the number of paths should always be greater than 1 too. click.secho(str(assertion_error), fg="red") # both the SVG and dot path require graphviz to work, hence the common error handling block - + click.secho(json.dumps({str(k): v for k, v in error_sources.items()}, indent=4)) click.secho("🏁Finished") diff --git a/pyproject.toml b/pyproject.toml index 588b555..92e9e90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ classifiers = [ "Programming Language :: Python :: 3.11", ] dependencies = [ - "ebdtable2graph>=0.1.9", + "ebdtable2graph>=0.1.16", "python-docx", "more_itertools", "attrs", diff --git a/requirements.in b/requirements.in index 7f44708..bedd2e5 100644 --- a/requirements.in +++ b/requirements.in @@ -1,4 +1,4 @@ -ebdtable2graph>=0.1.9 +ebdtable2graph>=0.1.16 python-docx more_itertools attrs diff --git a/requirements.txt b/requirements.txt index 4a36cd7..bc0b66c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,7 +17,7 @@ charset-normalizer==2.1.1 # via requests click==8.1.6 # via -r requirements.in -ebdtable2graph==0.1.14 +ebdtable2graph==0.1.16 # via -r requirements.in idna==3.4 # via requests