Skip to content

Commit

Permalink
fix(excel2json): if gui_order is empty no warning is longer displayed (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Apr 9, 2024
1 parent 523d55b commit a278e3e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
34 changes: 22 additions & 12 deletions src/dsp_tools/commands/excel2json/resources.py
Expand Up @@ -118,14 +118,18 @@ def _row2resource(
"""

class_name = class_info_row["name"]
labels = {lang: class_info_row[f"label_{lang}"] for lang in languages if class_info_row.get(f"label_{lang}")}
labels = {
lang: class_info_row[f"label_{lang}"] for lang in languages if not pd.isna(class_info_row[f"label_{lang}"])
}
if not labels:
labels = {lang: class_info_row[lang] for lang in languages if class_info_row.get(lang)}
labels = {lang: class_info_row[lang] for lang in languages if not pd.isna(class_info_row[lang])}
supers = [s.strip() for s in class_info_row["super"].split(",")]

resource = {"name": class_name, "super": supers, "labels": labels}

comments = {lang: class_info_row[f"comment_{lang}"] for lang in languages if class_info_row.get(f"comment_{lang}")}
comments = {
lang: class_info_row[f"comment_{lang}"] for lang in languages if not pd.isna(class_info_row[f"comment_{lang}"])
}
if comments:
resource["comments"] = comments

Expand Down Expand Up @@ -167,12 +171,17 @@ def _create_all_cardinalities(class_name: str, class_df_with_cardinalities: pd.D

def _check_complete_gui_order(class_name: str, class_df_with_cardinalities: pd.DataFrame) -> pd.DataFrame:
detail_problem_msg = ""
attempt_conversion = False
if "gui_order" not in class_df_with_cardinalities:
detail_problem_msg = "the column 'gui_order' does not exist."
pass
elif class_df_with_cardinalities["gui_order"].isna().all():
pass
elif class_df_with_cardinalities["gui_order"].isna().any():
detail_problem_msg = "some rows in the column 'gui_order' are empty."
elif not class_df_with_cardinalities["gui_order"].isna().all():
attempt_conversion = True

if not detail_problem_msg:
if attempt_conversion:
try:
class_df_with_cardinalities["gui_order"] = [int(float(x)) for x in class_df_with_cardinalities["gui_order"]]
return class_df_with_cardinalities
Expand All @@ -184,13 +193,14 @@ def _check_complete_gui_order(class_name: str, class_df_with_cardinalities: pd.D

class_df_with_cardinalities["gui_order"] = list(range(1, len(class_df_with_cardinalities) + 1))

complete_msg = (
f"In the sheet '{class_name}' of the file 'resources.xlsx', "
f"{detail_problem_msg}\n"
f"Values have been filled in automatically, "
f"so that the gui-order reflects the order of the properties in the file."
)
warnings.warn(complete_msg)
if detail_problem_msg:
complete_msg = (
f"In the sheet '{class_name}' of the file 'resources.xlsx', "
f"{detail_problem_msg}\n"
f"Values have been filled in automatically, "
f"so that the gui-order reflects the order of the properties in the file."
)
warnings.warn(complete_msg)
return class_df_with_cardinalities


Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/excel2json/utils.py
Expand Up @@ -206,7 +206,7 @@ def get_comments(df_row: pd.Series[Any]) -> dict[str, str] | None:
Returns:
A dictionary with the language tag and the content of the cell
"""
comments = {lang: df_row[f"comment_{lang}"] for lang in languages if df_row[f"comment_{lang}"] is not pd.NA}
comments = {lang: df_row[f"comment_{lang}"] for lang in languages if not pd.isna(df_row[f"comment_{lang}"])}
return comments or None


Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/utils/shared.py
Expand Up @@ -42,7 +42,7 @@ def prepare_dataframe(
required_columns = [x.strip().lower() for x in required_columns]
# strip every cell, and insert "" if there is no valid word in it
new_df = new_df.map(
lambda x: str(x).strip() if pd.notna(x) and regex.search(r"[\w\p{L}]", str(x), flags=regex.U) else ""
lambda x: str(x).strip() if pd.notna(x) and regex.search(r"[\w\p{L}]", str(x), flags=regex.U) else pd.NA
)
# delete rows that don't have the required columns
for req in required_columns:
Expand Down
9 changes: 1 addition & 8 deletions test/unittests/commands/excel2json/test_resources.py
Expand Up @@ -10,15 +10,8 @@
class TestCheckCompleteGuiOrder:
def test_column_does_not_exist(self) -> None:
df = pd.DataFrame({"prop": [1, 2, 3]})
expected_msg = (
"In the sheet 'class_name' of the file 'resources.xlsx', "
"the column 'gui_order' does not exist.\n"
"Values have been filled in automatically, "
"so that the gui-order reflects the order of the properties in the file."
)
expected_df = pd.DataFrame({"prop": [1, 2, 3], "gui_order": [1, 2, 3]})
with pytest.warns(Warning, match=expected_msg):
res = _check_complete_gui_order("class_name", df)
res = _check_complete_gui_order("class_name", df)
assert_frame_equal(res, expected_df)

def test_na_in_row(self) -> None:
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/commands/excel2json/test_utils.py
Expand Up @@ -163,14 +163,14 @@ def test_get_comments(self) -> None:
"comment_en": ["text_en", pd.NA],
"comment_de": ["text_de", pd.NA],
"comment_fr": ["text_fr", pd.NA],
"comment_it": ["text_it", pd.NA],
"comment_it": [pd.NA, pd.NA],
"comment_rm": ["text_rm", pd.NA],
}
)
expected_dict = {"de": "text_de", "en": "text_en", "fr": "text_fr", "it": "text_it", "rm": "text_rm"}
expected_dict = {"de": "text_de", "en": "text_en", "fr": "text_fr", "rm": "text_rm"}
returned_dict = utl.get_comments(cast("pd.Series[Any]", original_df.loc[0, :]))
assert returned_dict
self.assertDictEqual(expected_dict, returned_dict)
assert expected_dict == returned_dict

assert not utl.get_comments(cast("pd.Series[Any]", original_df.loc[1, :]))

Expand Down
2 changes: 1 addition & 1 deletion test/unittests/utils/test_shared.py
Expand Up @@ -21,7 +21,7 @@ def test_prepare_dataframe() -> None:
{
"title of column 1": ["0-1", "1-n", "0-n"],
"title of column 2": ["1", "1", "text"],
"title of column 3": ["", "", ""],
"title of column 3": [pd.NA, pd.NA, pd.NA],
},
index=[1, 2, 7],
)
Expand Down

0 comments on commit a278e3e

Please sign in to comment.