Skip to content

Commit 456825d

Browse files
committed
use feature branch from pynxtools-xps
1 parent 75cf4e0 commit 456825d

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

.github/workflows/plugin_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
branch: main
4343
tests_to_run: tests/.
4444
- plugin: pynxtools-xps
45-
branch: main
45+
branch: custom-attr-for-open-enum
4646
tests_to_run: tests/.
4747
- plugin: pynxtools-xrd
4848
branch: main

src/pynxtools/dataconverter/helpers.py

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ class ValidationProblem(Enum):
8787
UnitWithoutDocumentation = auto()
8888
InvalidUnit = auto()
8989
InvalidEnum = auto()
90-
OpenEnumWithCorrectNewItem = auto()
91-
OpenEnumWithIncorrectNewItem = auto()
90+
OpenEnumWithCustom = auto()
91+
OpenEnumWithCustomFalse = auto()
92+
OpenEnumWithMissingCustom = auto()
9293
MissingRequiredGroup = auto()
9394
MissingRequiredField = auto()
9495
MissingRequiredAttribute = auto()
@@ -153,22 +154,22 @@ def _log(self, path: str, log_type: ValidationProblem, value: Optional[Any], *ar
153154

154155
elif log_type == ValidationProblem.InvalidEnum:
155156
logger.warning(
156-
f"The value at {path} should be one of the following: {value}."
157+
f"The value '{args[0]}' at {path} should be one of the following: {value}."
157158
)
158-
elif log_type == ValidationProblem.OpenEnumWithCorrectNewItem:
159+
elif log_type == ValidationProblem.OpenEnumWithCustom:
159160
logger.info(
160161
f"The value '{args[0]}' at {path} does not match with the enumerated items from the open enumeration: {value}."
161162
)
162-
elif log_type == ValidationProblem.OpenEnumWithIncorrectNewItem:
163-
actual_value, custom_attr = args
164-
165-
log_text = f"The value '{actual_value}' at {path} does not match with the enumerated items from the open enumeration: {value}."
166-
if custom_attr == "custom_missing":
167-
log_text += f" When a different value is used, a boolean 'custom' attribute must be added."
168-
logger.warning(log_text)
169-
elif custom_attr == "custom_false":
170-
log_text += f" When a different value is used, the boolean 'custom' attribute cannot be False."
171-
logger.warning(log_text)
163+
elif log_type == ValidationProblem.OpenEnumWithCustomFalse:
164+
logger.warning(
165+
f"The value '{args[0]}' at {path} does not match with the enumerated items from the open enumeration: {value}."
166+
"When a different value is used, the boolean 'custom' attribute cannot be False."
167+
)
168+
elif log_type == ValidationProblem.OpenEnumWithMissingCustom:
169+
logger.info(
170+
f"The value '{args[0]}' at {path} does not match with the enumerated items from the open enumeration: {value}. "
171+
"When a different value is used, a boolean 'custom=True' attribute must be added. It was added here automatically."
172+
)
172173

173174
elif log_type == ValidationProblem.MissingRequiredGroup:
174175
logger.warning(f"The required group {path} hasn't been supplied.")
@@ -301,7 +302,8 @@ def collect_and_log(
301302
ValidationProblem.UnitWithoutDocumentation,
302303
ValidationProblem.CompressionStrengthZero,
303304
ValidationProblem.MissingNXclass,
304-
ValidationProblem.OpenEnumWithCorrectNewItem,
305+
ValidationProblem.OpenEnumWithCustom,
306+
ValidationProblem.OpenEnumWithMissingCustom,
305307
):
306308
if self.logging and message not in self.data["info"]:
307309
self._log(path, log_type, value, *args, **kwargs)
@@ -819,10 +821,8 @@ def convert_int_to_float(value):
819821
def is_valid_data_field(value: Any, nxdl_type: str, path: str) -> Any:
820822
"""Checks whether a given value is valid according to the type defined in the NXDL."""
821823

822-
def validate_data_value(
823-
value: Any, nxdl_type: str, nxdl_enum: list, nxdl_enum_open: bool, path: str
824-
) -> Any:
825-
"""Validate and possibly convert a primitive value according to NXDL type/enum rules."""
824+
def validate_data_value(value: Any, nxdl_type: str, path: str) -> Any:
825+
"""Validate and possibly convert a primitive value according to NXDL type rules."""
826826
accepted_types = NEXUS_TO_PYTHON_DATA_TYPES[nxdl_type]
827827
original_value = value
828828

@@ -853,26 +853,6 @@ def validate_data_value(
853853
path, ValidationProblem.InvalidDatetime, value
854854
)
855855

856-
# if nxdl_enum is not None:
857-
# if (
858-
# isinstance(value, np.ndarray)
859-
# and isinstance(nxdl_enum, list)
860-
# and isinstance(nxdl_enum[0], list)
861-
# ):
862-
# enum_value = list(value)
863-
# else:
864-
# enum_value = value
865-
866-
# if enum_value not in nxdl_enum:
867-
# if nxdl_enum_open:
868-
# collector.collect_and_log(
869-
# path, ValidationProblem.OpenEnumWithNewItem, nxdl_enum
870-
# )
871-
# else:
872-
# collector.collect_and_log(
873-
# path, ValidationProblem.InvalidEnum, nxdl_enum
874-
# )
875-
876856
return value
877857

878858
if isinstance(value, dict) and set(value.keys()) == {"compress", "strength"}:
@@ -888,18 +868,14 @@ def validate_data_value(
888868
path, ValidationProblem.InvalidCompressionStrength, value
889869
)
890870
# In this case, we remove the compression.
891-
return validate_data_value(
892-
value["compress"], nxdl_type, nxdl_enum, nxdl_enum_open, path
893-
)
871+
return validate_data_value(value["compress"], nxdl_type, path)
894872

895873
# Apply standard validation to compressed value
896-
value["compress"] = validate_data_value(
897-
compressed_value, nxdl_type, nxdl_enum, nxdl_enum_open, path
898-
)
874+
value["compress"] = validate_data_value(compressed_value, nxdl_type, path)
899875

900876
return value
901877

902-
return validate_data_value(value, nxdl_type, nxdl_enum, nxdl_enum_open, path)
878+
return validate_data_value(value, nxdl_type, path)
903879

904880

905881
def split_class_and_name_of(name: str) -> tuple[Optional[str], str]:

tests/dataconverter/test_validation.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,32 @@ def format_error_message(msg: str) -> str:
894894
],
895895
id="open-enum-with-new-item",
896896
),
897+
pytest.param(
898+
alter_dict(
899+
alter_dict(
900+
alter_dict(
901+
alter_dict(
902+
TEMPLATE,
903+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/type2",
904+
{"compress": "a very different type", "strength": 2},
905+
),
906+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/type2/@custom",
907+
True,
908+
),
909+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/type2/@attribute_with_open_enum",
910+
{"compress": "3rd option", "strength": 4},
911+
),
912+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/type2/@attribute_with_open_enum_custom",
913+
True,
914+
),
915+
[
916+
"The value 'a very different type' at /ENTRY[my_entry]/NXODD_name[nxodd_name]/type2 does not match "
917+
"with the enumerated items from the open enumeration: ['1st type open', '2nd type open'].",
918+
"The value '3rd option' at /ENTRY[my_entry]/NXODD_name[nxodd_name]/type2/@attribute_with_open_enum "
919+
"does not match with the enumerated items from the open enumeration: ['1st option', '2nd option'].",
920+
],
921+
id="open-enum-with-new-item-compressed",
922+
),
897923
pytest.param(
898924
alter_dict(
899925
alter_dict(
@@ -2029,6 +2055,7 @@ def test_validate_data_dict(data_dict, error_messages, caplog, request):
20292055
"field-with-illegal-unit",
20302056
"baseclass-field-with-illegal-unit",
20312057
"open-enum-with-new-item",
2058+
"open-enum-with-new-item-compressed",
20322059
"baseclass-open-enum-with-new-item",
20332060
"appdef-compressed-strength-0",
20342061
):

0 commit comments

Comments
 (0)