-
Notifications
You must be signed in to change notification settings - Fork 4
/
resources.py
278 lines (230 loc) · 11.2 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import importlib.resources
import json
import warnings
from typing import Any
from typing import Optional
import jsonpath_ng.ext
import jsonschema
import pandas as pd
import regex
from dsp_tools.commands.excel2json.models.input_error import JsonValidationResourceProblem
from dsp_tools.commands.excel2json.models.input_error import MissingValuesInRowProblem
from dsp_tools.commands.excel2json.models.input_error import PositionInExcel
from dsp_tools.commands.excel2json.models.input_error import Problem
from dsp_tools.commands.excel2json.models.input_error import ResourcesSheetsNotAsExpected
from dsp_tools.commands.excel2json.utils import check_column_for_duplicate
from dsp_tools.commands.excel2json.utils import read_and_clean_all_sheets
from dsp_tools.models.exceptions import InputError
from dsp_tools.utils.shared import check_notna
from dsp_tools.utils.shared import prepare_dataframe
languages = ["en", "de", "fr", "it", "rm"]
def _validate_resources(resources_list: list[dict[str, Any]]) -> None:
"""
This function checks if the "resources" section of a JSON project file is valid according to the JSON schema,
and if the resource names are unique.
Args:
resources_list: the "resources" section of a JSON project as a list of dicts
Raises:
InputError: if the validation fails
"""
with importlib.resources.files("dsp_tools").joinpath("resources/schema/resources-only.json").open(
encoding="utf-8"
) as schema_file:
resources_schema = json.load(schema_file)
try:
jsonschema.validate(instance=resources_list, schema=resources_schema)
except jsonschema.ValidationError as err:
validation_problem = _find_validation_problem(
validation_error=err,
resources_list=resources_list,
)
msg = "\nThe Excel file 'resources.xlsx' did not pass validation." + validation_problem.execute_error_protocol()
raise InputError(msg) from None
def _find_validation_problem(
validation_error: jsonschema.ValidationError, resources_list: list[dict[str, Any]]
) -> JsonValidationResourceProblem:
if json_path_to_resource := regex.search(r"^\$\[(\d+)\]", validation_error.json_path):
# fmt: off
wrong_res_name = (
jsonpath_ng.ext.parse(json_path_to_resource.group(0))
.find(resources_list)[0]
.value["name"]
)
# fmt: on
if affected_field := regex.search(
r"name|labels|comments|super|cardinalities\[(\d+)\]", validation_error.json_path
):
affected_value = affected_field.group(0)
problematic_resource, excel_sheet, excel_row, excel_column = "", None, None, None
if affected_value in ["name", "labels", "comments", "super"]:
excel_sheet = "classes"
problematic_resource = wrong_res_name
excel_row = int(json_path_to_resource.group(1)) + 2
excel_column = affected_value
elif "cardinalities" in affected_value:
excel_row = int(affected_field.group(1)) + 2
excel_sheet = wrong_res_name
if validation_error.json_path.endswith("cardinality"):
excel_column = "Cardinality"
elif validation_error.json_path.endswith("propname"):
excel_column = "Property"
return JsonValidationResourceProblem(
problematic_resource=problematic_resource,
excel_position=PositionInExcel(sheet=excel_sheet, column=excel_column, row=excel_row),
original_msg=validation_error.message,
)
return JsonValidationResourceProblem(
original_msg=validation_error.message,
message_path=validation_error.json_path,
)
def _row2resource(
class_info_row: pd.Series,
class_df_with_cardinalities: pd.DataFrame,
) -> dict[str, Any]:
"""
Method that reads one row from the "classes" DataFrame,
opens the corresponding details DataFrame,
and builds a dict object of the resource.
Args:
class_info_row: row from the "classes" DataFrame
class_df_with_cardinalities: Excel sheet of the individual class
Raises:
UserError: if the row or the details sheet contains invalid data
Returns:
dict object of the resource
"""
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}")}
if not labels:
labels = {lang: class_info_row[lang] for lang in languages if class_info_row.get(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}")}
if comments:
resource["comments"] = comments
cards = _make_cardinality_section(class_name, class_df_with_cardinalities)
if cards:
resource["cardinalities"] = cards
return resource
def _make_cardinality_section(class_name: str, class_df_with_cardinalities: pd.DataFrame) -> list[dict[str, str | int]]:
class_df_with_cardinalities = prepare_dataframe(
df=class_df_with_cardinalities,
required_columns=["Property", "Cardinality"],
location_of_sheet=f"Sheet '{class_name}' in file 'resources.xlsx'",
)
if len(class_df_with_cardinalities) == 0:
warnings.warn(
f"Sheet '{class_name}' in file 'resources.xlsx' does not have any properties listed.\n"
f"Creation of the resource class continues without 'cardinalities' section."
)
return []
cards = _create_all_cardinalities(class_name, class_df_with_cardinalities)
return cards
def _create_all_cardinalities(class_name: str, class_df_with_cardinalities: pd.DataFrame) -> list[dict[str, str | int]]:
class_df_with_cardinalities = _check_complete_gui_order(class_name, class_df_with_cardinalities)
cards = []
for i, detail_row in class_df_with_cardinalities.iterrows():
property_ = {
"propname": ":" + detail_row["property"],
"cardinality": detail_row["cardinality"].lower(),
"gui_order": detail_row["gui_order"],
}
cards.append(property_)
return cards
def _check_complete_gui_order(class_name: str, class_df_with_cardinalities: pd.DataFrame) -> pd.DataFrame:
detail_problem_msg = ""
if "gui_order" not in class_df_with_cardinalities:
detail_problem_msg = "the column 'gui_order' does not exist."
elif class_df_with_cardinalities["gui_order"].isna().any():
detail_problem_msg = "some rows in the column 'gui_order' are empty."
if not detail_problem_msg:
try:
class_df_with_cardinalities["gui_order"] = [int(float(x)) for x in class_df_with_cardinalities["gui_order"]]
return class_df_with_cardinalities
except ValueError:
detail_problem_msg = (
"some rows in the column 'gui_order' contain invalid characters "
"that could not be converted to an integer."
)
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)
return class_df_with_cardinalities
def excel2resources(
excelfile: str,
path_to_output_file: Optional[str] = None,
) -> tuple[list[dict[str, Any]], bool]:
"""
Converts resources described in an Excel file into a "resources" section which can be inserted into a JSON
project file.
Args:
excelfile: path to the Excel file containing the resources
path_to_output_file: if provided, the output is written into this JSON file
(otherwise, it's only returned as return value)
Raises:
UserError: if something went wrong
InputError: is something went wrong
Returns:
a tuple consisting of the "resources" section as Python list,
and the success status (True if everything went well)
"""
all_dfs = read_and_clean_all_sheets(excelfile)
classes_df, resource_dfs = _prepare_classes_df(all_dfs)
if validation_problems := _validate_excel_file(classes_df, resource_dfs):
msg = "The excel file 'resources.xlsx', sheet 'classes' has a problem.\n" + "\n\n".join(
(x.execute_error_protocol() for x in validation_problems)
)
raise InputError(msg)
# transform every row into a resource
resources = [_row2resource(row, resource_dfs[row["name"]]) for i, row in classes_df.iterrows()]
# write final "resources" section into a JSON file
_validate_resources(resources_list=resources)
if path_to_output_file:
with open(file=path_to_output_file, mode="w", encoding="utf-8") as file:
json.dump(resources, file, indent=4, ensure_ascii=False)
print(f"resources section was created successfully and written to file '{path_to_output_file}'")
return resources, True
def _prepare_classes_df(resource_dfs: dict[str, pd.DataFrame]) -> tuple[pd.DataFrame, dict[str, pd.DataFrame]]:
resource_dfs = {k.strip(): v for k, v in resource_dfs.items()}
sheet_name_list = list(resource_dfs)
cls_sheet_name = [
ok.group(0) for x in sheet_name_list if (ok := regex.search(r"classes", flags=regex.IGNORECASE, string=x))
]
if not cls_sheet_name:
msg = ResourcesSheetsNotAsExpected(set(), names_sheets={"classes"}).execute_error_protocol()
raise InputError(msg)
elif len(cls_sheet_name) == 1:
classes_df = resource_dfs.pop(cls_sheet_name[0])
else:
msg = (
"The excel file 'resources.xlsx' has some problems.\n"
"There is more than one excel sheet called 'classes'.\n"
"This is a protected name and cannot be used for other sheets."
)
raise InputError(msg)
classes_df = prepare_dataframe(
df=classes_df,
required_columns=["name"],
location_of_sheet="Sheet 'classes' in file 'resources.xlsx'",
)
return classes_df, resource_dfs
def _validate_excel_file(classes_df: pd.DataFrame, df_dict: dict[str, pd.DataFrame]) -> list[Problem]:
if any(classes_df.get(lang) is not None for lang in languages):
warnings.warn(
f"The file 'resources.xlsx' uses {languages} as column titles, which is deprecated. "
f"Please use {[f'label_{lang}' for lang in languages]}"
)
problems: list[Problem] = []
if missing_super_rows := [int(index) + 2 for index, row in classes_df.iterrows() if not check_notna(row["super"])]:
problems.append(MissingValuesInRowProblem(column="super", row_numbers=missing_super_rows))
if duplicate_check := check_column_for_duplicate(classes_df, "name"):
problems.append(duplicate_check)
# check that all the sheets have an entry in the names column and vice versa
if (all_names := set(classes_df["name"].tolist())) != (all_sheets := set(df_dict)):
problems.append(ResourcesSheetsNotAsExpected(all_names, all_sheets))
return problems