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

WIP: XLSX reader

parent 381ba467
No related branches found
No related tags found
2 merge requests!107Release v0.11.0,!102ENH: XLSX reader
Checking pipeline status
#!/usr/bin/env python3
# encoding: utf-8
#
# This file is a part of the LinkAhead Project.
#
# Copyright (C) 2024 Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2024 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2024 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from fill_xlsx import read_or_dict
class XLSXConverter:
def __init__(self, schema: Union[dict, str, TextIO]):
"""
Parameters
----------
schema: Union[dict, str, TextIO]
If given, validate the date against this schema first. This raises an exception if the validation
fails. May be given as dict with the schema, path to a json file as string or as a readable file
like object.
"""
self._schema = read_or_dict(schema)
...@@ -118,7 +118,7 @@ def _next_row_index(sheet: Worksheet) -> int: ...@@ -118,7 +118,7 @@ def _next_row_index(sheet: Worksheet) -> int:
return sheet.max_row return sheet.max_row
def _read_or_dict(data: Union[dict, str, TextIO]) -> dict: def read_or_dict(data: Union[dict, str, TextIO]) -> dict:
"""If data is a json file name or input stream, read data from there.""" """If data is a json file name or input stream, read data from there."""
if isinstance(data, dict): if isinstance(data, dict):
pass pass
...@@ -414,12 +414,12 @@ validation_schema: dict, optional ...@@ -414,12 +414,12 @@ validation_schema: dict, optional
fails. If no validation schema is given, try to ignore more errors in the data when filling the fails. If no validation schema is given, try to ignore more errors in the data when filling the
XLSX template. XLSX template.
""" """
data = _read_or_dict(data) data = read_or_dict(data)
assert isinstance(data, dict) assert isinstance(data, dict)
# Validation # Validation
if validation_schema is not None: if validation_schema is not None:
validation_schema = _read_or_dict(validation_schema) validation_schema = read_or_dict(validation_schema)
try: try:
validate(data, validation_schema, format_checker=FormatChecker()) validate(data, validation_schema, format_checker=FormatChecker())
except ValidationError as ve: except ValidationError as ve:
......
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