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

FIX: no explode for top level; clean title string

parent 8b2db58d
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 #47854 failed
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
""" """
This module allows to generate template tables from JSON schemas. This module allows to generate template tables from JSON schemas.
""" """
import argparse import argparse
import re
import sys import sys
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from argparse import RawTextHelpFormatter from argparse import RawTextHelpFormatter
...@@ -31,6 +31,7 @@ from enum import Enum ...@@ -31,6 +31,7 @@ from enum import Enum
from typing import Union from typing import Union
from openpyxl import Workbook from openpyxl import Workbook
from openpyxl.workbook.child import INVALID_TITLE_REGEX
class ColumnType(Enum): class ColumnType(Enum):
...@@ -113,23 +114,23 @@ class TableTemplateGenerator(ABC): ...@@ -113,23 +114,23 @@ class TableTemplateGenerator(ABC):
def _get_foreign_keys(self, foreign_keys: dict, path: list) -> list: def _get_foreign_keys(self, foreign_keys: dict, path: list) -> list:
""" returns the foreign keys that are needed at the location to which path points """ """ returns the foreign keys that are needed at the location to which path points """
path = list(path) cpath = list(path)
keys = foreign_keys keys = foreign_keys
selected_keys = None selected_keys = None
while path: while cpath:
if keys is None: if keys is None:
raise ValueError(f"A foreign key definition is missing for path:" raise ValueError(f"A foreign key definition is missing for path:"
f"\n{path}\n{foreign_keys}") f"\n{path}\n{foreign_keys}")
if path[0] not in keys: if cpath[0] not in keys:
raise ValueError(f"A foreign key definition is missing for path: \n{path}\n{keys}") raise ValueError(f"A foreign key definition is missing for path: \n{path}\n{keys}")
keys = keys[path[0]] keys = keys[cpath[0]]
if isinstance(keys, tuple): if isinstance(keys, tuple):
selected_keys, keys = keys selected_keys, keys = keys
elif isinstance(keys, list): elif isinstance(keys, list):
selected_keys, keys = keys, None selected_keys, keys = keys, None
else: else:
selected_keys, keys = None, keys selected_keys, keys = None, keys
path = path[1:] cpath = cpath[1:]
if selected_keys is None: if selected_keys is None:
raise ValueError(f"A foreign key definition is missing for path:" raise ValueError(f"A foreign key definition is missing for path:"
f"\n{path}\n{foreign_keys}") f"\n{path}\n{foreign_keys}")
...@@ -147,7 +148,7 @@ class TableTemplateGenerator(ABC): ...@@ -147,7 +148,7 @@ class TableTemplateGenerator(ABC):
# if it is an array, value defs are in 'items' # if it is an array, value defs are in 'items'
if 'type' in schema and schema['type'] == 'array': if 'type' in schema and schema['type'] == 'array':
if 'type' in schema['items'] and schema['items']['type'] == 'object': # list of references; special treatment if 'type' in schema['items'] and schema['items']['type'] == 'object' and len(path) > 1: # list of references; special treatment
# we add a new sheet # we add a new sheet
sheets[".".join(path)] = self._treat_schema_element(schema['items'], sheets, path, foreign_keys) sheets[".".join(path)] = self._treat_schema_element(schema['items'], sheets, path, foreign_keys)
for c in self._get_foreign_keys(foreign_keys, path[:-1]): for c in self._get_foreign_keys(foreign_keys, path[:-1]):
...@@ -245,7 +246,7 @@ class XLSXTemplateGenerator(TableTemplateGenerator): ...@@ -245,7 +246,7 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
wb = Workbook() wb = Workbook()
assert wb.sheetnames == ["Sheet"] assert wb.sheetnames == ["Sheet"]
for sn, sheetdef in sheets.items(): for sn, sheetdef in sheets.items():
ws = wb.create_sheet(sn) ws = wb.create_sheet(re.sub(INVALID_TITLE_REGEX, '_', sn))
# first row will by the COL_TYPE row # first row will by the COL_TYPE row
# first column will be the indicator row with values COL_TYPE, PATH, IGNORE # first column will be the indicator row with values COL_TYPE, PATH, IGNORE
# the COL_TYPE row will be followed by as many PATH rows as needed # the COL_TYPE row will be followed by as many PATH rows as needed
......
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