Subclass of list that will validate before __setitem__.
| 947 | |
| 948 | |
| 949 | class _HEDStrings(list): |
| 950 | """Subclass of list that will validate before __setitem__.""" |
| 951 | |
| 952 | def __init__(self, *args, hed_version, **kwargs): |
| 953 | self._hed = _soft_import("hed", "validation of HED tags in annotations") |
| 954 | self._schema = self._hed.load_schema_version(hed_version) |
| 955 | super().__init__(*args, **kwargs) |
| 956 | self._objs = [self._validate_hed_string(item, self._schema) for item in self] |
| 957 | |
| 958 | def __setitem__(self, key, value): |
| 959 | """Validate value first, before assigning.""" |
| 960 | if isinstance(key, slice): |
| 961 | hss = [self._validate_hed_string(v, self._schema) for v in value] |
| 962 | super().__setitem__(key, [hs.get_original_hed_string() for hs in hss]) |
| 963 | self._objs[key] = hss |
| 964 | return |
| 965 | hs = self._validate_hed_string(value, self._schema) |
| 966 | super().__setitem__(key, hs.get_original_hed_string()) |
| 967 | self._objs[key] = hs |
| 968 | |
| 969 | def _validate_hed_string(self, value, schema): |
| 970 | # create HedString object and validate it |
| 971 | hs = self._hed.HedString(value, schema) |
| 972 | # handle any errors |
| 973 | error_handler = self._hed.errors.ErrorHandler(check_for_warnings=False) |
| 974 | issues = hs.validate(allow_placeholders=False, error_handler=error_handler) |
| 975 | error_string = self._hed.get_printable_issue_string(issues) |
| 976 | if len(error_string): |
| 977 | raise ValueError(f"A HED string failed to validate:\n {error_string}") |
| 978 | hs.sort() |
| 979 | return hs |
| 980 | |
| 981 | def append(self, item): |
| 982 | """Append an item to the end of the HEDString list.""" |
| 983 | hs = self._validate_hed_string(item, self._schema) |
| 984 | super().append(hs.get_original_hed_string()) |
| 985 | self._objs.append(hs) |
| 986 | |
| 987 | |
| 988 | def _hed_extras_from_hed_annotations(annot): |
no outgoing calls
no test coverage detected