Skip to content
Snippets Groups Projects
Commit 4ef559b2 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

WIP for fill_xlsx: More graceful handling of incomplete data.

parent a504c310
No related branches found
No related tags found
2 merge requests!100WIP: Filling XLSX: Seems to be working.,!93Filling XLSX: Everything except multiple choice.
Pipeline #48831 failed
...@@ -27,6 +27,7 @@ import pathlib ...@@ -27,6 +27,7 @@ import pathlib
from collections import OrderedDict from collections import OrderedDict
from types import SimpleNamespace from types import SimpleNamespace
from typing import Any, Dict, List, Optional, TextIO, Union from typing import Any, Dict, List, Optional, TextIO, Union
from warnings import warn
from jsonschema import FormatChecker, validate from jsonschema import FormatChecker, validate
from jsonschema.exceptions import ValidationError from jsonschema.exceptions import ValidationError
...@@ -134,8 +135,9 @@ def _read_or_dict(data: Union[dict, str, TextIO]) -> dict: ...@@ -134,8 +135,9 @@ def _read_or_dict(data: Union[dict, str, TextIO]) -> dict:
class TemplateFiller: class TemplateFiller:
"""Class to fill XLSX templates. Has an index for all relevant columns.""" """Class to fill XLSX templates. Has an index for all relevant columns."""
def __init__(self, workbook: Workbook): def __init__(self, workbook: Workbook, graceful: bool = False):
self._workbook = workbook self._workbook = workbook
self._graceful = graceful
self._create_index() self._create_index()
@property @property
...@@ -305,6 +307,7 @@ out: union[dict, None] ...@@ -305,6 +307,7 @@ out: union[dict, None]
# collecting the data # collecting the data
assert isinstance(content, list) assert isinstance(content, list)
content = [str(x) for x in content]
value = ";".join(content) # TODO we need escaping of values value = ";".join(content) # TODO we need escaping of values
path_str = p2s(path) path_str = p2s(path)
assert path_str not in insertables assert path_str not in insertables
...@@ -318,6 +321,9 @@ out: union[dict, None] ...@@ -318,6 +321,9 @@ out: union[dict, None]
insert_row = None insert_row = None
sheet = None sheet = None
for path_str, value in insertables.items(): for path_str, value in insertables.items():
if self._graceful and path_str not in self._sheet_index:
warn(f"Ignoring path with missing sheet index: {path_str}")
continue
sheet_meta = self._sheet_index[path_str] sheet_meta = self._sheet_index[path_str]
if sheet is None: if sheet is None:
sheet = sheet_meta.sheet sheet = sheet_meta.sheet
...@@ -359,7 +365,8 @@ result: str ...@@ -359,7 +365,8 @@ result: str
Path for the result XLSX. Path for the result XLSX.
validation_schema: dict, optional validation_schema: dict, optional
If given, validate the date against this schema first. This raises an exception if the validation If given, validate the date against this schema first. This raises an exception if the validation
fails. fails. If no validation schema is given, try to ignore more errors in the data when filling the
XLSX template.
""" """
data = _read_or_dict(data) data = _read_or_dict(data)
assert isinstance(data, dict) assert isinstance(data, dict)
...@@ -377,7 +384,7 @@ validation_schema: dict, optional ...@@ -377,7 +384,7 @@ validation_schema: dict, optional
# Filling the data # Filling the data
result_wb = load_workbook(template) result_wb = load_workbook(template)
template_filler = TemplateFiller(result_wb) template_filler = TemplateFiller(result_wb, graceful=(validation_schema is None))
template_filler.fill_data(data=data) template_filler.fill_data(data=data)
parentpath = pathlib.Path(result).parent parentpath = pathlib.Path(result).parent
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment