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
from collections import OrderedDict
from types import SimpleNamespace
from typing import Any, Dict, List, Optional, TextIO, Union
from warnings import warn
from jsonschema import FormatChecker, validate
from jsonschema.exceptions import ValidationError
......@@ -134,8 +135,9 @@ def _read_or_dict(data: Union[dict, str, TextIO]) -> dict:
class TemplateFiller:
"""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._graceful = graceful
self._create_index()
@property
......@@ -305,6 +307,7 @@ out: union[dict, None]
# collecting the data
assert isinstance(content, list)
content = [str(x) for x in content]
value = ";".join(content) # TODO we need escaping of values
path_str = p2s(path)
assert path_str not in insertables
......@@ -318,6 +321,9 @@ out: union[dict, None]
insert_row = None
sheet = None
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]
if sheet is None:
sheet = sheet_meta.sheet
......@@ -359,7 +365,8 @@ result: str
Path for the result XLSX.
validation_schema: dict, optional
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)
assert isinstance(data, dict)
......@@ -377,7 +384,7 @@ validation_schema: dict, optional
# Filling the data
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)
parentpath = pathlib.Path(result).parent
......
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