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

MAINT: refactor function for better code readability

parent caceab21
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
......@@ -113,30 +113,20 @@ class TableTemplateGenerator(ABC):
sheets, [RTname], foreign_keys)
return sheets
def _get_foreign_keys(self, foreign_keys: dict, path: list) -> list:
def _get_foreign_keys(self, keys: dict, path: list) -> list:
""" returns the foreign keys that are needed at the location to which path points """
cpath = list(path)
keys = foreign_keys
selected_keys = None
while cpath:
if keys is None:
raise ValueError(f"A foreign key definition is missing for path:"
f"\n{path}\n{foreign_keys}")
if cpath[0] not in keys:
raise ValueError(f"A foreign key definition is missing for path: \n{path}\n{keys}")
keys = keys[cpath[0]]
if isinstance(keys, list):
selected_keys, keys = keys, None
else:
selected_keys, keys = None, keys
cpath = cpath[1:]
msg = (f"A foreign key definition is missing for path:\n{path}\n{keys}")
while path:
if keys is None or path[0] not in keys:
raise ValueError(msg)
keys = keys[path[0]]
path = path[1:]
if isinstance(keys, dict) and "__this__" in keys:
selected_keys = keys["__this__"]
if selected_keys is None:
raise ValueError(f"A foreign key definition is missing for path:"
f"\n{path}\n{foreign_keys}")
return selected_keys
return keys["__this__"]
elif isinstance(keys, list):
return keys
else:
raise ValueError(msg)
def _treat_schema_element(self, schema: dict, sheets: dict = None, path: list = None,
foreign_keys: dict = None, level_in_sheet_name: int = 1,
......
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