Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
a small click based script to extract all EBDs from a given file.
"""
# invoke like this:
# main.py -i unittests/test_data/ebd20221128.docx -o foo -t json -t svg -t puml
import json
from pathlib import Path
from typing import Literal

import cattrs
import click
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

# pylint:disable=import-error
from ebddocx2table import TableNotFoundError, get_all_ebd_keys, get_ebd_docx_tables # type:ignore[import]
from ebddocx2table.docxtableconverter import DocxTableConverter # type:ignore[import]


def _dump_puml(puml_path: Path, ebd_graph: EbdGraph) -> None:
plantuml_code = convert_graph_to_plantuml(ebd_graph)
with open(puml_path, "w+", encoding="utf-8") as uml_file:
uml_file.write(plantuml_code)


Comment thread
hf-kklein marked this conversation as resolved.
def _dump_svg(svg_path: Path, ebd_graph: EbdGraph) -> None:
dot_code = convert_graph_to_dot(ebd_graph)
svg_code = convert_dot_to_svg_kroki(dot_code)
with open(svg_path, "w+", encoding="utf-8") as svg_file:
svg_file.write(svg_code)


def _dump_json(json_path: Path, ebd_table: EbdTable) -> 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)


@click.command()
@click.option(
"-i",
"--input_path",
type=click.Path(exists=True, dir_okay=False, file_okay=True, path_type=Path),
prompt="Input DOCX File",
help="Path of a .docx file from which the EBDs shall be extracted",
)
@click.option(
"-o",
"--output_path",
type=click.Path(exists=False, dir_okay=True, file_okay=False, path_type=Path),
default="output",
prompt="Output directory",
help="Define the path where you want to save the generated files",
)
@click.option(
"-t",
"--export_types",
type=click.Choice(["puml", "json", "svg"], case_sensitive=False),
Comment thread
hf-kklein marked this conversation as resolved.
Outdated
multiple=True,
help="Choose which file you'd like to create",
)
def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", "json", "svg"]]):
Comment thread
hf-kklein marked this conversation as resolved.
Outdated
"""
A program to get a machine-readable version of the AHBs docx files published by edi@energy.
"""
if output_path.exists():
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")
all_ebd_keys = get_all_ebd_keys(input_path)
for ebd_key in all_ebd_keys:
click.secho(f"Processing EBD '{ebd_key}'")
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"Error while scraping {ebd_key}: {str(table_not_found_error)}; Skip!", fg="red")
continue
try:
converter = DocxTableConverter(
docx_tables, ebd_key=ebd_key, chapter="Dummy Chapter", sub_chapter="Dummy Subchapter"
)
ebd_table = converter.convert_docx_tables_to_ebd_table()
ebd_graph = convert_table_to_graph(ebd_table)
except Exception as error: # pylint:disable=broad-except
click.secho(f"Error while converting {ebd_key}: {str(error)}; Skip!", fg="red")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pokemon exception by design

continue
if "puml" in export_types:
try:
_dump_puml(output_path / Path(f"{ebd_key}.puml"), ebd_graph)
click.secho(f"💾 Successfully exported '{ebd_key}.puml'")
except AssertionError as assertion_error:
Comment thread
hf-kklein marked this conversation as resolved.
# https://github.com/Hochfrequenz/ebdtable2graph/issues/35
click.secho(str(assertion_error), fg="red")
Comment thread
hf-kklein marked this conversation as resolved.
if "svg" in export_types:
_dump_svg(output_path / Path(f"{ebd_key}.svg"), ebd_graph)
click.secho(f"💾 Successfully exported '{ebd_key}.svg'")
if "json" in export_types:
_dump_json(output_path / Path(f"{ebd_key}.json"), ebd_table)
click.secho(f"💾 Successfully exported '{ebd_key}.json'")

click.secho("🏁Finished")


if __name__ == "__main__":
# the parameter arguments gets provided over the CLI
main() # pylint:disable=no-value-for-parameter
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ebdtable2graph>=0.1.5
ebdtable2graph>=0.1.7
python-docx
more_itertools
attrs
click
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ certifi==2022.12.7
# via requests
charset-normalizer==2.1.1
# via requests
click==8.1.3
# via -r requirements.in
colorama==0.4.6
# via click
ebdtable2graph==0.1.7
# via -r requirements.in
idna==3.4
Expand Down
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ zip_safe = False
include_package_data = True
python_requires = >=3.11
install_requires =
ebdtable2graph
ebdtable2graph>=0.1.7
python-docx
more_itertools
attrs
click
# write here line by line the dependencies for your package

[options.packages.find]
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ deps =
setenv = PYTHONPATH = {toxinidir}/src
commands =
pylint ebddocx2table
pylint main.py
# add single files (ending with .py) or packages here

[testenv:type_check]
Expand All @@ -38,6 +39,7 @@ deps =
commands =
mypy --show-error-codes src/ebddocx2table
mypy --show-error-codes unittests
mypy --show-error-codes main.py
# add single files (ending with .py) or packages here

[testenv:coverage]
Expand Down