|
1 | | -""" |
2 | | -a small click based script to extract all EBDs from a given file. |
3 | | -""" |
4 | | - |
5 | | -# invoke like this: |
6 | | -# main.py -i unittests/test_data/ebd20230619_v33.docx |
7 | | -# -o ../machine-readable_entscheidungsbaumdiagramme/FV2304 |
8 | | -# -t json -t dot -t svg -t puml |
9 | | -# or |
10 | | -# main.py -i unittests/test_data/ebd20230629_v34.docx |
11 | | -# -o ../machine-readable_entscheidungsbaumdiagramme/FV2310 |
12 | | -# -t json -t dot -t svg -t puml |
13 | | -import json |
14 | | -from pathlib import Path |
15 | | -from typing import Literal |
16 | | - |
17 | | -import cattrs |
18 | | -import click |
19 | | -from ebdtable2graph import convert_graph_to_plantuml, convert_table_to_graph |
20 | | -from ebdtable2graph.graphviz import convert_dot_to_svg_kroki, convert_graph_to_dot |
21 | | -from ebdtable2graph.models import EbdGraph, EbdTable |
22 | | -from ebdtable2graph.models.errors import ( |
23 | | - EbdCrossReferenceNotSupportedError, |
24 | | - EndeInWrongColumnError, |
25 | | - NotExactlyTwoOutgoingEdgesError, |
26 | | - OutcomeCodeAmbiguousError, |
27 | | - PathsNotGreaterThanOneError, |
28 | | -) |
29 | | -from ebdtable2graph.plantuml import GraphTooComplexForPlantumlError |
30 | | - |
31 | | -# pylint:disable=import-error |
32 | | -from ebdamame import TableNotFoundError, get_all_ebd_keys, get_ebd_docx_tables # type:ignore[import] |
33 | | -from ebdamame.docxtableconverter import DocxTableConverter # type:ignore[import] |
34 | | - |
35 | | - |
36 | | -def _dump_puml(puml_path: Path, ebd_graph: EbdGraph) -> None: |
37 | | - plantuml_code = convert_graph_to_plantuml(ebd_graph) |
38 | | - with open(puml_path, "w+", encoding="utf-8") as uml_file: |
39 | | - uml_file.write(plantuml_code) |
40 | | - |
41 | | - |
42 | | -def _dump_dot(dot_path: Path, ebd_graph: EbdGraph) -> None: |
43 | | - dot_code = convert_graph_to_dot(ebd_graph) |
44 | | - with open(dot_path, "w+", encoding="utf-8") as uml_file: |
45 | | - uml_file.write(dot_code) |
46 | | - |
47 | | - |
48 | | -def _dump_svg(svg_path: Path, ebd_graph: EbdGraph) -> None: |
49 | | - dot_code = convert_graph_to_dot(ebd_graph) |
50 | | - svg_code = convert_dot_to_svg_kroki(dot_code) |
51 | | - with open(svg_path, "w+", encoding="utf-8") as svg_file: |
52 | | - svg_file.write(svg_code) |
53 | | - |
54 | | - |
55 | | -def _dump_json(json_path: Path, ebd_table: EbdTable) -> None: |
56 | | - with open(json_path, "w+", encoding="utf-8") as json_file: |
57 | | - json.dump(cattrs.unstructure(ebd_table), json_file, ensure_ascii=False, indent=2, sort_keys=True) |
58 | | - |
59 | | - |
60 | | -@click.command() |
61 | | -@click.option( |
62 | | - "-i", |
63 | | - "--input_path", |
64 | | - type=click.Path(exists=True, dir_okay=False, file_okay=True, path_type=Path), |
65 | | - prompt="Input DOCX File", |
66 | | - help="Path of a .docx file from which the EBDs shall be extracted", |
67 | | -) |
68 | | -@click.option( |
69 | | - "-o", |
70 | | - "--output_path", |
71 | | - type=click.Path(exists=False, dir_okay=True, file_okay=False, path_type=Path), |
72 | | - default="output", |
73 | | - prompt="Output directory", |
74 | | - help="Define the path where you want to save the generated files", |
75 | | -) |
76 | | -@click.option( |
77 | | - "-t", |
78 | | - "--export_types", |
79 | | - type=click.Choice(["puml", "dot", "json", "svg"], case_sensitive=False), |
80 | | - multiple=True, |
81 | | - help="Choose which file you'd like to create", |
82 | | -) |
83 | | -# pylint:disable=too-many-locals, too-many-branches, too-many-statements, |
84 | | -def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", "dot", "json", "svg"]]): |
85 | | - """ |
86 | | - A program to get a machine-readable version of the AHBs docx files published by edi@energy. |
87 | | - """ |
88 | | - if output_path.exists(): |
89 | | - click.secho(f"The output directory '{output_path}' exists already.", fg="yellow") |
90 | | - else: |
91 | | - output_path.mkdir(parents=True) |
92 | | - click.secho(f"Created a new directory at {output_path}", fg="green") |
93 | | - all_ebd_keys = get_all_ebd_keys(input_path) |
94 | | - error_sources: dict[type, list[str]] = {} |
95 | | - |
96 | | - def handle_known_error(error: Exception, ebd_key: str) -> None: |
97 | | - click.secho(f"Error while processing EBD {ebd_key}: {error}", fg="yellow") |
98 | | - if type(error) not in error_sources: |
99 | | - error_sources[type(error)] = [] |
100 | | - error_sources[type(error)].append(ebd_key) |
101 | | - |
102 | | - for ebd_key, (ebd_title, ebd_kapitel) in all_ebd_keys.items(): |
103 | | - click.secho(f"Processing EBD {ebd_kapitel} '{ebd_key}' ({ebd_title})") |
104 | | - try: |
105 | | - docx_tables = get_ebd_docx_tables(docx_file_path=input_path, ebd_key=ebd_key) |
106 | | - except TableNotFoundError as table_not_found_error: |
107 | | - click.secho(f"Table not found: {ebd_key}: {str(table_not_found_error)}; Skip!", fg="yellow") |
108 | | - continue |
109 | | - assert ebd_kapitel is not None |
110 | | - try: |
111 | | - converter = DocxTableConverter( |
112 | | - docx_tables, |
113 | | - ebd_key=ebd_key, |
114 | | - chapter=ebd_kapitel.chapter_title, # type:ignore[arg-type] |
115 | | - # pylint:disable=line-too-long |
116 | | - sub_chapter=f"{ebd_kapitel.chapter}.{ebd_kapitel.section}.{ebd_kapitel.subsection}: {ebd_kapitel.section_title}", |
117 | | - ) |
118 | | - ebd_table = converter.convert_docx_tables_to_ebd_table() |
119 | | - except Exception as scraping_error: # pylint:disable=broad-except |
120 | | - click.secho(f"Error while scraping {ebd_key}: {str(scraping_error)}; Skip!", fg="red") |
121 | | - continue |
122 | | - if "json" in export_types: |
123 | | - _dump_json(output_path / Path(f"{ebd_key}.json"), ebd_table) |
124 | | - click.secho(f"💾 Successfully exported '{ebd_key}.json'") |
125 | | - try: |
126 | | - ebd_graph = convert_table_to_graph(ebd_table) |
127 | | - except (EbdCrossReferenceNotSupportedError, EndeInWrongColumnError, OutcomeCodeAmbiguousError) as known_issue: |
128 | | - handle_known_error(known_issue, ebd_key) |
129 | | - continue |
130 | | - except Exception as unknown_error: # pylint:disable=broad-except |
131 | | - click.secho(f"Error while graphing {ebd_key}: {str(unknown_error)}; Skip!", fg="red") |
132 | | - continue |
133 | | - if "puml" in export_types: |
134 | | - try: |
135 | | - _dump_puml(output_path / Path(f"{ebd_key}.puml"), ebd_graph) |
136 | | - click.secho(f"💾 Successfully exported '{ebd_key}.puml'") |
137 | | - except AssertionError as assertion_error: |
138 | | - # https://github.com/Hochfrequenz/ebdtable2graph/issues/35 |
139 | | - click.secho(str(assertion_error), fg="red") |
140 | | - except (NotExactlyTwoOutgoingEdgesError, GraphTooComplexForPlantumlError) as known_issue: |
141 | | - handle_known_error(known_issue, ebd_key) |
142 | | - except Exception as general_error: # pylint:disable=broad-exception-caught |
143 | | - click.secho(f"Error while exporting {ebd_key} as UML: {str(general_error)}; Skip!", fg="yellow") |
144 | | - |
145 | | - try: |
146 | | - if "dot" in export_types: |
147 | | - _dump_dot(output_path / Path(f"{ebd_key}.dot"), ebd_graph) |
148 | | - click.secho(f"💾 Successfully exported '{ebd_key}.dot'") |
149 | | - if "svg" in export_types: |
150 | | - _dump_svg(output_path / Path(f"{ebd_key}.svg"), ebd_graph) |
151 | | - click.secho(f"💾 Successfully exported '{ebd_key}.svg'") |
152 | | - except PathsNotGreaterThanOneError as known_issue: |
153 | | - handle_known_error(known_issue, ebd_key) |
154 | | - except AssertionError as assertion_error: |
155 | | - # e.g. AssertionError: If indegree > 1, the number of paths should always be greater than 1 too. |
156 | | - click.secho(str(assertion_error), fg="red") |
157 | | - # both the SVG and dot path require graphviz to work, hence the common error handling block |
158 | | - click.secho(json.dumps({str(k): v for k, v in error_sources.items()}, indent=4)) |
159 | | - click.secho("🏁Finished") |
160 | | - |
161 | | - |
162 | | -if __name__ == "__main__": |
163 | | - # the parameter arguments gets provided over the CLI |
164 | | - main() # pylint:disable=no-value-for-parameter |
| 1 | +""" |
| 2 | +the script to convert EBDs from .docx to SVGs visualizations using both the ebdamame and rebdhuhn libraries |
| 3 | +was relocated to https://github.com/Hochfrequenz/ebd_toolchain |
| 4 | +""" |
0 commit comments