caosadvancedtools.table_json_conversion.convert module#

Convert XLSX files to JSON dictionaries.

exception caosadvancedtools.table_json_conversion.convert.ForeignError(*args, definitions: list, message: str = '')#

Bases: KeyError

class caosadvancedtools.table_json_conversion.convert.XLSXConverter(xlsx: str | BinaryIO, schema: dict | str | TextIO, strict: bool = False)#

Bases: object

Class for conversion from XLSX to JSON.

For a detailed description of the required formatting of the XLSX files, see specs.md in the documentation.

PARSER: dict[str, Callable] = {'boolean': <function _strict_bool>, 'integer': <class 'int'>, 'number': <class 'float'>, 'string': <class 'str'>}#
get_error_str() str#

Return a beautiful string with the collected errors.

get_errors() dict#

Return a dict with collected errors.

to_dict(validate: bool = False, collect_errors: bool = True) dict#

Convert the xlsx contents to a dict.

Parameters:
  • validate (bool, default=False) – If True, validate the result against the schema.

  • collect_errors (bool, default=True) – If True, do not fail at the first error, but try to collect as many errors as possible. After an exception is raised, the errors can be collected with get_errors() and printed with get_error_str().

Returns:

out – A dict representing the JSON with the extracted data.

Return type:

dict

caosadvancedtools.table_json_conversion.convert.deep_record_from_json(recdata: dict, schema: dict, parent: str, container: Container | None = None) Container#

Create a single Record (and linked references recursivley) from json data.

Parameters:
  • recdata (dict) – The data dictionary for the single Record.

  • schema (dict) – The json schema for this data.

  • parent (str) – The name of the RecordType for this Record.

  • container (db.Container, optional) – If given, use this to collect the generates entities. Not generally necessary when calling this function from the outside.

Returns:

out – The Record and all its references (recursively). The first element is guaranteed to be the top-level element described by data.

Return type:

db.Container

caosadvancedtools.table_json_conversion.convert.records_from_json(data: dict, schema: dict | str | TextIO) Container#

Create Records from json data.

Parameters:
  • data (dict) – The data dictionary, as returned by to_dict.

  • schema (dict) – The json schema for this data.

caosadvancedtools.table_json_conversion.convert.to_dict(xlsx: str | BinaryIO, schema: dict | str | TextIO, validate: bool = False, strict: bool = False, collect_errors: bool = True) dict#

Convert the xlsx contents to a dict, it must follow a schema.

Parameters:
  • xlsx (Union[str, BinaryIO]) – Path to the XLSX file or opened file object.

  • schema (Union[dict, str, TextIO]) – Schema for validation of XLSX content.

  • validate (bool, default=False) – If True, validate the result against the schema.

  • strict (bool, default=False) – If True, fail faster.

  • collect_errors (bool, default=True) – See XLSXConverter.to_dict() for details.

Returns:

out – A dict representing the JSON with the extracted data.

Return type:

dict