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