Skip to content
Snippets Groups Projects
Commit 51728602 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: comments and sanity checks

parent b8ab0b16
No related branches found
No related tags found
4 merge requests!100WIP: Filling XLSX: Seems to be working.,!94ENH: add framework for converting json schema into table templates,!93Filling XLSX: Everything except multiple choice.,!92ENH: xlsx template generator
Pipeline #48100 failed
...@@ -139,6 +139,9 @@ class TableTemplateGenerator(ABC): ...@@ -139,6 +139,9 @@ class TableTemplateGenerator(ABC):
---------- ----------
array_paths: list array_paths: list
a list of path along the way to the current object, where the json contains arrays a list of path along the way to the current object, where the json contains arrays
schema: dict
part of the json schema; it must be the level that contains the type definition
(e.g. 'type' or 'oneOf' key)
Returns Returns
------- -------
...@@ -161,20 +164,26 @@ class TableTemplateGenerator(ABC): ...@@ -161,20 +164,26 @@ class TableTemplateGenerator(ABC):
if 'type' in schema and schema['type'] == 'array': if 'type' in schema and schema['type'] == 'array':
if ('type' in schema['items'] and schema['items']['type'] == 'object' if ('type' in schema['items'] and schema['items']['type'] == 'object'
and len(path) > 1): # list of references; special treatment and len(path) > 1): # list of references; special treatment
# we add a new sheet # we add a new sheet with columns generated from the subtree of the schema
sheetname = ".".join(path) sheetname = ".".join(path)
if sheetname in sheets:
raise ValueError(f"The shema would lead to two sheets with the same name which"
f" is forbidden:{sheetname}")
sheets[sheetname] = self._treat_schema_element( sheets[sheetname] = self._treat_schema_element(
schema['items'], sheets, path, foreign_keys, len(path), schema['items'], sheets, path, foreign_keys, len(path),
array_paths=array_paths+[path] # since this level is an array, we extend the list array_paths=array_paths+[path] # since this level is an array, we extend the list
) )
# and add the foreign keys that are necessary up to this point
for p in array_paths: for p in array_paths:
keys = self._get_foreign_keys(foreign_keys, p) keys = self._get_foreign_keys(foreign_keys, p)
for k in keys: for k in keys:
sheets[sheetname].update({k: (ColumnType.FOREIGN, f"see sheet '{path[0]}'", p+[k])}) if k in sheets[sheetname]:
raise ValueError(f"The shema would lead to two columns with the same "
f"name which is forbidden:{k}")
sheets[sheetname][k] = (ColumnType.FOREIGN, f"see sheet '{path[0]}'", p+[k])
# columns are added to the new sheet, thus we do not return columns # columns are added to the new sheet, thus we do not return columns
return {} return {}
else: else: # it is a list of primitive types -> semi colon separated list
# it is a list of primitive types -> semi colon separated list
schema = schema['items'] schema = schema['items']
ctype = ColumnType.LIST ctype = ColumnType.LIST
...@@ -183,31 +192,34 @@ class TableTemplateGenerator(ABC): ...@@ -183,31 +192,34 @@ class TableTemplateGenerator(ABC):
if 'type' in el: if 'type' in el:
schema = el schema = el
if "properties" in schema: if "properties" in schema: # recurse for each property
# recurse for each property
cols = {} cols = {}
for pname in schema["properties"].keys(): for pname in schema["properties"].keys():
cols.update(self._treat_schema_element( col_defs = self._treat_schema_element(
schema["properties"][pname], sheets, path+[pname], foreign_keys, schema["properties"][pname], sheets, path+[pname], foreign_keys,
level_in_sheet_name, array_paths=array_paths)) level_in_sheet_name, array_paths=array_paths)
for k in col_defs.keys():
if k in cols:
raise ValueError(f"The shema would lead to two columns with the same "
f"name which is forbidden:{k}")
cols.update(col_defs)
return cols return cols
else: else: # those are the leaves
description = schema['description'] if 'description' in schema else None description = schema['description'] if 'description' in schema else None
# definition of a single column
# those are the leaves default_return = {".".join(path[level_in_sheet_name:]): (ctype, description, path)}
if 'type' not in schema: if 'type' not in schema and 'enum' in schema:
if 'enum' in schema: return default_return
return {".".join(path[level_in_sheet_name:]): (ctype, description, path)} elif 'type' not in schema and 'anyOf' in schema:
if 'anyOf' in schema: for d in schema['anyOf']:
for d in schema['anyOf']: # currently the only case where this occurs is date formats
# currently the only case where this occurs is date formats assert d['type'] == 'string'
assert d['type'] == 'string' assert d['format'] == 'date' or d['format'] == 'date-time'
assert d['format'] == 'date' or d['format'] == 'date-time' return default_return
return {".".join(path[level_in_sheet_name:]): (ctype, description, path)}
elif schema["type"] in ['string', 'number', 'integer', 'boolean']: elif schema["type"] in ['string', 'number', 'integer', 'boolean']:
if 'format' in schema and schema['format'] == 'data-url': if 'format' in schema and schema['format'] == 'data-url':
return {} # file; ignore for now return {} # file; ignore for now
return {".".join(path[level_in_sheet_name:]): (ctype, description, path)} return default_return
else: else:
raise ValueError("Inappropriate JSON schema: The following part should define an" raise ValueError("Inappropriate JSON schema: The following part should define an"
f" object with properties or a primitive type:\n{schema}\n") f" object with properties or a primitive type:\n{schema}\n")
......
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