Skip to content

Commit

Permalink
refactor(excel2json-new-lists): change order of functions (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann authored Aug 13, 2024
1 parent e5a19eb commit 0b9a429
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/dsp_tools/commands/excel2json/new_lists/make_new_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def new_excel2lists(
a tuple consisting of the "lists" section as Python list, and the success status (True if everything went well)
"""
sheet_list = _parse_files(excelfolder)
sheet_list = _prepare_dfs(sheet_list)

finished_lists = _make_serialised_lists(sheet_list)
validate_lists_section_with_schema(lists_section=finished_lists)
Expand Down Expand Up @@ -77,29 +78,6 @@ def _non_hidden(path: Path) -> bool:
return not regex.search(r"^(\.|~\$).+", path.name)


def _make_serialised_lists(sheet_list: list[ExcelSheet]) -> list[dict[str, Any]]:
sheet_list = _prepare_dfs(sheet_list)
all_lists = _make_all_lists(sheet_list)
if isinstance(all_lists, ListCreationProblem):
msg = all_lists.execute_error_protocol()
logger.error(msg)
raise InputError(msg)
return [list_.to_dict() for list_ in all_lists]


def _make_all_lists(sheet_list: list[ExcelSheet]) -> list[ListRoot] | ListCreationProblem:
good_lists = []
problem_lists: list[SheetProblem] = []
for sheet in sheet_list:
if isinstance(new_list := _make_one_list(sheet), ListRoot):
good_lists.append(new_list)
else:
problem_lists.append(new_list)
if problem_lists:
return ListCreationProblem([CollectedSheetProblems(problem_lists)])
return good_lists


def _prepare_dfs(sheet_list: list[ExcelSheet]) -> list[ExcelSheet]:
sheet_list = _add_id_optional_column_if_not_exists(sheet_list)
make_all_formal_excel_compliance_checks(sheet_list)
Expand Down Expand Up @@ -224,6 +202,28 @@ def _construct_non_duplicate_id_string(row: pd.Series[Any], preferred_language:
return ":".join(id_cols)


def _make_serialised_lists(sheet_list: list[ExcelSheet]) -> list[dict[str, Any]]:
all_lists = _make_all_lists(sheet_list)
if isinstance(all_lists, ListCreationProblem):
msg = all_lists.execute_error_protocol()
logger.error(msg)
raise InputError(msg)
return [list_.to_dict() for list_ in all_lists]


def _make_all_lists(sheet_list: list[ExcelSheet]) -> list[ListRoot] | ListCreationProblem:
good_lists = []
problem_lists: list[SheetProblem] = []
for sheet in sheet_list:
if isinstance(new_list := _make_one_list(sheet), ListRoot):
good_lists.append(new_list)
else:
problem_lists.append(new_list)
if problem_lists:
return ListCreationProblem([CollectedSheetProblems(problem_lists)])
return good_lists


def _make_one_list(sheet: ExcelSheet) -> ListRoot | ListSheetProblem:
node_dict, node_problems = _make_list_nodes_from_df(sheet.df)
nodes_for_root = _add_nodes_to_parent(node_dict, sheet.df.at[0, "id"]) if node_dict else []
Expand Down

0 comments on commit 0b9a429

Please sign in to comment.