Skip to content
Snippets Groups Projects
Verified Commit 8cde9de2 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

WIP: Filling XLSX

parent 0dadcca6
No related branches found
No related tags found
2 merge requests!100WIP: Filling XLSX: Seems to be working.,!93Filling XLSX: Everything except multiple choice.
Showing
with 24 additions and 53 deletions
......@@ -57,6 +57,14 @@ def _get_column_types(sheet: Worksheet) -> OrderedDict:
return result
def _get_deep_value(data: Dict[str, Any], path: List[str]):
"""Return the value at ``path`` inside the dict ``data``.
"""
if len(path) > 1:
return _get_deep_value(data[path[0]], path[1:])
return data[path[0]]
def _get_foreign_key_columns(sheet: Worksheet) -> Dict[str, SimpleNamespace]:
"""Return the foreign keys of the worksheet.
......@@ -84,14 +92,6 @@ out: dict[str, SimpleNamespace]
return result
def _get_deep_value(data: Dict[str, Any], path: List[str]):
"""Return the value at ``path`` inside the dict ``data``.
"""
if len(path) > 1:
return _get_deep_value(data[path.pop(0)], path)
return data[path[0]]
def _get_row_type_column_index(sheet: Worksheet):
"""Return the column index (0-indexed) of the column which defines the row types.
"""
......@@ -135,7 +135,7 @@ class TemplateFiller:
def fill_data(self, data: dict):
"""Fill the data into the workbook."""
self._context = data
self._handle_data(data=data, current_path=[])
self._handle_data(data=data)
self._context = None
def _create_index(self):
......
......@@ -63,10 +63,10 @@ def _parse_arguments():
def main():
"""The main function of this script."""
_ = _parse_arguments()
prepare_datamodel("model_simple.yml", ["Training", "Person"], "model_schema.json",
prepare_datamodel("data/simple_model.yml", ["Training", "Person"], "data/simple_schema.json",
do_not_create=["Organisation"])
prepare_datamodel("model_multiple_refs.yml", ["Training", "Person"],
"schema_multiple_refs.json")
prepare_datamodel("data/multiple_refs_model.yml", ["Training", "Person"],
"data/multiple_refs_schema.json")
if __name__ == "__main__":
......
{
"Training": {
"date": "2023-01-01",
"url": "www.indiscale.com",
"coach": [
{
"family_name": "Sky",
"given_name": "Max",
"Organisation": "ECB"
},
{
"family_name": "Sky",
"given_name": "Min",
"Organisation": "ECB"
}
],
"supervisor": {
"family_name": "Steve",
"given_name": "Stevie",
"Organisation": "IMF"
},
"duration": 1.0,
"participants": 1,
"subjects": ["Math", "Physics"],
"remote": false
},
"Person": {
"family_name": "Steve",
"given_name": "Stevie",
"Organisation": "IMF"
}
}
......@@ -56,18 +56,20 @@ custom_output: str, optional
assert os.path.exists(outfile)
generated = load_workbook(outfile) # workbook can be read
known_good_wb = load_workbook(known_good)
if custom_output is not None:
from IPython import embed
embed()
# if custom_output is not None:
# from IPython import embed
# embed()
compare_workbooks(generated, known_good_wb)
def test_detect():
example = load_workbook(rfp("example_template.xlsx"))
example = load_workbook(rfp("data/simple_template.xlsx"))
assert 0 == _get_row_type_column_index(example['Person'])
assert [1, 2] == _get_path_rows(example['Person'])
def test_fill_xlsx():
fill_and_compare(json_file="example_single.json", template_file="example_template.xlsx",
known_good="example_single_data.xlsx")
fill_and_compare(json_file="data/simple_data.json", template_file="data/simple_template.xlsx",
known_good="data/simple_data.xlsx")
# fill_and_compare(json_file="data/example.json", template_file="data/example_template.xlsx",
# known_good="data/example_single_data.xlsx")
......@@ -140,7 +140,7 @@ def test_generate_sheets_from_schema():
assert tdef['name'] == (ColumnType.SCALAR, "The name of the Record to be created", ["Training", 'name'])
# example case
with open(rfp("model_schema.json")) as sfi:
with open(rfp("data/simple_schema.json")) as sfi:
schema = json.load(sfi)
with pytest.raises(ValueError, match="A foreign key definition is missing.*"):
generator._generate_sheets_from_schema(schema)
......@@ -197,7 +197,7 @@ def test_get_max_path_length():
def test_template_generator():
generated, _ = _compare_generated_to_known_good(
schema_file=rfp("model_schema.json"), known_good=rfp("example_template.xlsx"),
schema_file=rfp("data/simple_schema.json"), known_good=rfp("data/simple_template.xlsx"),
foreign_keys={'Training': {"__this__": ['date', 'url']}},
outfile=None)
# test some hidden
......@@ -230,7 +230,8 @@ def test_template_generator():
def test_model_with_multiple_refs():
_compare_generated_to_known_good(
schema_file=rfp("schema_multiple_refs.json"), known_good=rfp("multiple_refs.xlsx"),
schema_file=rfp("data/multiple_refs_schema.json"),
known_good=rfp("data/multiple_refs_template.xlsx"),
foreign_keys={'Training': {"__this__": ['date', 'url'],
"Organisation": ["name"]}},
outfile=None)
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