From e93984c0613743e28ea444a91557a2d14580d904 Mon Sep 17 00:00:00 2001 From: Daniel Hornung <d.hornung@indiscale.com> Date: Thu, 18 Apr 2024 09:02:53 +0200 Subject: [PATCH] WIP: XLSX reader --- .../table_json_conversion/convert.py | 37 +++++++++++++++++++ .../table_json_conversion/fill_xlsx.py | 6 +-- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/caosadvancedtools/table_json_conversion/convert.py diff --git a/src/caosadvancedtools/table_json_conversion/convert.py b/src/caosadvancedtools/table_json_conversion/convert.py new file mode 100644 index 00000000..344c7ad1 --- /dev/null +++ b/src/caosadvancedtools/table_json_conversion/convert.py @@ -0,0 +1,37 @@ +#!/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) diff --git a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py index 585bc6bf..e9a410db 100644 --- a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py +++ b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py @@ -118,7 +118,7 @@ def _next_row_index(sheet: Worksheet) -> int: 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 isinstance(data, dict): pass @@ -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 XLSX template. """ - data = _read_or_dict(data) + data = read_or_dict(data) assert isinstance(data, dict) # Validation if validation_schema is not None: - validation_schema = _read_or_dict(validation_schema) + validation_schema = read_or_dict(validation_schema) try: validate(data, validation_schema, format_checker=FormatChecker()) except ValidationError as ve: -- GitLab