Skip to content

Commit

Permalink
fixed linting, testing, etc, issues :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDaniel committed Mar 5, 2024
1 parent 5178c4f commit 171c193
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
46 changes: 22 additions & 24 deletions src/migmose/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from typing import Generator, Union

import click
import docx
from docx.document import Document
from docx.oxml import CT_P, CT_Tbl
from docx.table import Table, _Cell
from docx.text.paragraph import Paragraph
import docx # type: ignore[import]
from docx.document import Document # type: ignore[import]
from docx.oxml import CT_Tbl # type: ignore[import]
from docx.table import Table, _Cell # type: ignore[import]
from docx.text.paragraph import Paragraph # type: ignore[import]
from loguru import logger


Expand Down Expand Up @@ -40,6 +40,9 @@
help="Set path to directory which contains the output files. If the directory does not exist, it will be created.",
)
def main(input_dir: Path, output_dir, message_type: list[str]) -> None:
"""
Main function. Uses CLI input.
"""
dict_files = find_file_to_type(message_type, input_dir)
for m_type, file in dict_files.items():
mig_table = parse_raw_nachrichtenstrukturzeile(file)
Expand All @@ -48,9 +51,6 @@ def main(input_dir: Path, output_dir, message_type: list[str]) -> None:
preliminary_output_as_json(mig_table, m_type, output_dir)


# ad


def find_file_to_type(message_types: list[str], input_dir: Path) -> dict[str, Path]:
"""
finds the file with the message type in the input directory
Expand All @@ -68,9 +68,8 @@ def find_file_to_type(message_types: list[str], input_dir: Path) -> dict[str, Pa
logger.warning(f"⚠️ No file found for {message_type}.", fg="red")
if file_dict:
return file_dict
else:
logger.error("⚠️ No files found in the input directory.", fg="red")
raise click.Abort()
logger.error("⚠️ No files found in the input directory.", fg="red")
raise click.Abort()


def preliminary_output_as_json(table: list[str], message_type: str, output_dir: Path) -> None:
Expand All @@ -81,13 +80,14 @@ def preliminary_output_as_json(table: list[str], message_type: str, output_dir:
output_dir.mkdir(parents=True, exist_ok=True)
file_path = output_dir.joinpath(f"{message_type}_preliminary_output.json")
structured_json = {line: None for line in table}
with open(file_path, "w") as json_file:
with open(file_path, "w", encoding="utf-8") as json_file:
json.dump(structured_json, json_file, indent=4, encoding="utf-8")
logger.info(f"Created and wrote to {file_path}")


def get_paragraphs_up_to_diagram(parent: Union[Document, _Cell]) -> Generator[Union[Paragraph, Table], None, None]:
"""Goes through paragraphs and tables until a diagram is found"""
"""Goes through paragraphs and tables"""
# pylint: disable=protected-access
if isinstance(parent, Document):
parent_elm = parent.element.body
elif isinstance(parent, _Cell):
Expand All @@ -96,29 +96,27 @@ def get_paragraphs_up_to_diagram(parent: Union[Document, _Cell]) -> Generator[Un
raise ValueError("Passed parent argument must be of type Document or _Cell")

for child in parent_elm.iterchildren():
if isinstance(child, CT_P):
yield Paragraph(child, parent)
elif isinstance(child, CT_Tbl):
if isinstance(child, CT_Tbl):
yield Table(child, parent)


def parse_raw_nachrichtenstrukturzeile(input_path: Path) -> list[str]:
"""
parses raw nachrichtenstrukturzeile from a table . returns list of raw lines
"""
# pylint: disable=protected-access
doc = docx.Document(input_path)
docx_objects = get_paragraphs_up_to_diagram(doc)
mig_tables = []
nachrichtenstruktur_header = "Status\tMaxWdh\n\tZähler\tNr\tBez\tSta\tBDEW\tSta\tBDEW\tEbene\tInhalt"
for object in docx_objects:
if isinstance(object, Table):
for ind, line in enumerate(object._cells):
# marks the beginning of the complete nachrichtentruktur table
if line.text == nachrichtenstruktur_header:
mig_tables.extend([row.text for row in object._cells[ind + 1 :]])
break
for docx_object in docx_objects:
for ind, line in enumerate(docx_object._cells):
# marks the beginning of the complete nachrichtentruktur table
if line.text == nachrichtenstruktur_header:
mig_tables.extend([row.text for row in docx_object._cells[ind + 1 :]])
break
# filter empty rows and headers
mig_tables = [row for row in mig_tables if row != "\n" and row != nachrichtenstruktur_header]
mig_tables = [row for row in mig_tables if row not in ("\n", nachrichtenstruktur_header)]
return mig_tables


Expand Down
16 changes: 8 additions & 8 deletions src/migmose/mig/nachrichtenstrukturzeile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class for lines in mig tables, e.g. (ORDCHG):
"""

zaehler: str
nr: str
bezeichnung: str
standard_status: str
bdew_status: str
standard_maximale_wiederholungen: int
bdew_maximale_wiederholungen: int
ebene: int
inhalt: str
nr: str | None = None
bezeichnung: str | None = None
standard_status: str | None = None
bdew_status: str | None = None
standard_maximale_wiederholungen: int | None = None
bdew_maximale_wiederholungen: int | None = None
ebene: int | None = None
inhalt: str | None = None
11 changes: 0 additions & 11 deletions unittests/test_myclass.py

This file was deleted.

11 changes: 11 additions & 0 deletions unittests/test_nachrichtenstrukturzeile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from migmose.mig.nachrichtenstrukturzeile import NachrichtenstrukturZeile


class TestNachrichtenstrukturZeile:
"""
A class with pytest unit tests.
"""

def test_something(self):
my_class = NachrichtenstrukturZeile(zaehler="1")
assert my_class.zaehler == "1"

0 comments on commit 171c193

Please sign in to comment.