Skip to content
Snippets Groups Projects
Commit 3912400d authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: fixing typhints

parent 6ffe6a89
No related branches found
No related tags found
4 merge requests!100WIP: Filling XLSX: Seems to be working.,!94ENH: add framework for converting json schema into table templates,!93Filling XLSX: Everything except multiple choice.,!92ENH: xlsx template generator
Pipeline #48128 passed
This commit is part of merge request !94. Comments created here will be created in the context of that merge request.
...@@ -31,7 +31,7 @@ def _fill_leaves(json_doc: dict, workbook): ...@@ -31,7 +31,7 @@ def _fill_leaves(json_doc: dict, workbook):
for el in value: for el in value:
if isinstance(el, dict): if isinstance(el, dict):
_fill_leaves(el, workbook) _fill_leaves(el, workbook)
wb.cell(1, 2, el) workbook.cell(1, 2, el)
def _get_row_type_column(worksheet): def _get_row_type_column(worksheet):
......
...@@ -22,13 +22,11 @@ ...@@ -22,13 +22,11 @@
""" """
This module allows to generate template tables from JSON schemas. This module allows to generate template tables from JSON schemas.
""" """
import argparse
import re import re
import sys import sys
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from argparse import RawTextHelpFormatter
from enum import Enum from enum import Enum
from typing import Dict, List, Tuple, Union from typing import Dict, List, Optional, Tuple, Union
from openpyxl import Workbook from openpyxl import Workbook
from openpyxl.workbook.child import INVALID_TITLE_REGEX from openpyxl.workbook.child import INVALID_TITLE_REGEX
...@@ -52,7 +50,7 @@ class TableTemplateGenerator(ABC): ...@@ -52,7 +50,7 @@ class TableTemplateGenerator(ABC):
pass pass
@abstractmethod @abstractmethod
def generate(self, schema: dict, foreign_keys: dict): def generate(self, schema: dict, foreign_keys: dict, filepath: str):
""" generates a sheet definition from a given JSON schema """ generates a sheet definition from a given JSON schema
Parameters: Parameters:
...@@ -73,8 +71,8 @@ class TableTemplateGenerator(ABC): ...@@ -73,8 +71,8 @@ class TableTemplateGenerator(ABC):
""" """
pass pass
def _generate_sheets_from_schema(self, schema: dict, foreign_keys: dict = None def _generate_sheets_from_schema(self, schema: dict, foreign_keys: Optional[dict] = None
) -> Dict[str, Dict[str, list]]: ) -> Dict[str, Dict[str, Tuple[ColumnType, Optional[str], list]]]:
""" generates a sheet definition from a given JSON schema """ generates a sheet definition from a given JSON schema
Parameters Parameters
...@@ -104,7 +102,7 @@ class TableTemplateGenerator(ABC): ...@@ -104,7 +102,7 @@ class TableTemplateGenerator(ABC):
foreign_keys = {} foreign_keys = {}
# here, we treat the top level # here, we treat the top level
# sheets[sheetname][colname]= (COL_TYPE, description, [path]) # sheets[sheetname][colname]= (COL_TYPE, description, [path])
sheets: Dict[str, dict[str, Tuple[str, list]]] = {} sheets: Dict[str, Dict[str, Tuple[ColumnType, Optional[str], list]]] = {}
if "properties" not in schema: if "properties" not in schema:
raise ValueError("Inappropriate JSON schema: The following part should contain " raise ValueError("Inappropriate JSON schema: The following part should contain "
f"the 'properties' key:\n{schema}\n") f"the 'properties' key:\n{schema}\n")
...@@ -128,10 +126,10 @@ class TableTemplateGenerator(ABC): ...@@ -128,10 +126,10 @@ class TableTemplateGenerator(ABC):
else: else:
raise ValueError(msg) raise ValueError(msg)
def _treat_schema_element(self, schema: dict, sheets: dict = None, path: list = None, def _treat_schema_element(self, schema: dict, sheets: dict, path: list,
foreign_keys: dict = None, level_in_sheet_name: int = 1, foreign_keys: Optional[dict] = None, level_in_sheet_name: int = 1,
array_paths: list = None array_paths: Optional[list] = None
) -> Dict[str, Tuple[str, str, list]]: ) -> Dict[str, Tuple[ColumnType, Optional[str], list]]:
""" recursively transforms elements from the schema into column definitions """ recursively transforms elements from the schema into column definitions
sheets is modified in place. sheets is modified in place.
...@@ -157,6 +155,8 @@ class TableTemplateGenerator(ABC): ...@@ -157,6 +155,8 @@ class TableTemplateGenerator(ABC):
# if this is not set, we are at top level and the top level element may always be an # if this is not set, we are at top level and the top level element may always be an
# array # array
array_paths = [path] array_paths = [path]
if foreign_keys is None:
foreign_keys = {}
ctype = ColumnType.SCALAR ctype = ColumnType.SCALAR
...@@ -272,7 +272,8 @@ class XLSXTemplateGenerator(TableTemplateGenerator): ...@@ -272,7 +272,8 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
return ordered_cols return ordered_cols
def _create_workbook_from_sheets_def(self, sheets: Dict[str, Dict[str, Tuple[str, list]]]): def _create_workbook_from_sheets_def(
self, sheets: Dict[str, Dict[str, Tuple[ColumnType, Optional[str], list]]]):
wb = Workbook() wb = Workbook()
assert wb.sheetnames == ["Sheet"] assert wb.sheetnames == ["Sheet"]
for sn, sheetdef in sheets.items(): for sn, sheetdef in sheets.items():
...@@ -322,18 +323,3 @@ class XLSXTemplateGenerator(TableTemplateGenerator): ...@@ -322,18 +323,3 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
wb.move_sheet(sn, index-wb.index(wb[sn])) wb.move_sheet(sn, index-wb.index(wb[sn]))
return wb return wb
def parse_args():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=RawTextHelpFormatter)
parser.add_argument("path",
help="the subtree of files below the given path will "
"be considered. Use '/' for everything.")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
sys.exit(main(args))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment