caosadvancedtools.table_json_conversion package
Submodules
caosadvancedtools.table_json_conversion.fill_xlsx module
- class caosadvancedtools.table_json_conversion.fill_xlsx.TemplateFiller(workbook: Workbook, graceful: bool = False)
Bases:
object
Class to fill XLSX templates. Has an index for all relevant columns.
- class Context(current_path: List[str] | None = None, props: Dict[str, Any] | None = None)
Bases:
object
Context for an entry: simple properties of all ancestors, organized in a dict.
This is similar to a dictionary with all scalar element properties at the tree nodes up to the root. Siblings in lists and dicts are ignored. Additionally the context knows where its current position is.
Lookup of elements can easily be achieved by giving the path (as
list[str]
or stringified path).
- property workbook
- caosadvancedtools.table_json_conversion.fill_xlsx.fill_template(data: dict | str | TextIO, template: str, result: str, validation_schema: dict | str | TextIO | None = None) None
Insert json data into an xlsx file, according to a template.
This function fills the json data into the template stored at
template
and stores the result asresult
.- Parameters:
data (Union[dict, str, TextIO]) – The data, given as Python dict, path to a file or a file-like object.
template (str) – Path to the XLSX template.
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. If no validation schema is given, try to ignore more errors in the data when filling the XLSX template.
caosadvancedtools.table_json_conversion.table_generator module
This module allows to generate template tables from JSON schemas.
- class caosadvancedtools.table_json_conversion.table_generator.ColumnType(value)
Bases:
Enum
column types enum
- FOREIGN = 3
- IGNORE = 4
- LIST = 2
- SCALAR = 1
- class caosadvancedtools.table_json_conversion.table_generator.RowType(value)
Bases:
Enum
row types enum
- COL_TYPE = 1
- IGNORE = 3
- PATH = 2
- class caosadvancedtools.table_json_conversion.table_generator.TableTemplateGenerator
Bases:
ABC
base class for generating tables from json schema
- abstract generate(schema: dict, foreign_keys: dict, filepath: str)
Generate a sheet definition from a given JSON schema.
Parameters:
- schema: dict
Given JSON schema.
- foreign_keys: dict
A tree-like configuration (nested dict) that defines which attributes shall be used to create additional columns when a list of references exists. The nested dict is structured like the data model, its innermost elements are leaves of the path trees within the JSON, they define the required keys.
Suppose we want to distinguish Persons that are referenced by Trainings, thenforeign_keys
must at least contain the following:{"Training": {"Person": ["name", "email"]}}
.Values within the dicts can be either a list representing the keys (as in the example above) or a dict that allows to set additional foreign keys at higher depths. In the latter case (dict instead of list) if foreign keys exist at that level (e.g. in the above example there might be further levels below “Person”), then the foreign keys can be set using the special
__this__
key.Example:
{"Training": {"__this__": ["date"], "Person": ["name", "email"]}}
Here,date
is the sole foreign key for Training.
- class caosadvancedtools.table_json_conversion.table_generator.XLSXTemplateGenerator
Bases:
TableTemplateGenerator
Class for generating XLSX tables from json schema definitions.
- generate(schema: dict, foreign_keys: dict, filepath: str) None
Generate a sheet definition from a given JSON schema.
Parameters:
- schema: dict
Given JSON schema
- foreign_keys: dict
A configuration that defines which attributes shall be used to create additional columns when a list of references exists. See
foreign_keys
argument ofTableTemplateGenerator.generate()
.- filepath: str
The XLSX file will be stored under this path.