def check_if_tag_is_valid(self, original_tag, formatted_tag, previous_original_tag='', previous_formatted_tag=''): """Reports a validation error if the tag provided is not a valid tag or doesn't take a value. Parameters ---------- original_tag: string The original tag that is used to report the error. formatted_tag: string The tag that is used to do the validation. previous_original_tag: string The previous original tag that is used to report the error. previous_formatted_tag: string The previous tag that is used to do the validation. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; is_extension_tag = self.is_extension_allowed_tag(formatted_tag); if self._hed_dictionary_dictionaries[TagValidator.TAG_DICTIONARY_KEY].get(formatted_tag) or \ self.tag_takes_value(formatted_tag) or formatted_tag == TagValidator.TILDE: pass; elif is_extension_tag: pass; elif not is_extension_tag and self.tag_takes_value(previous_formatted_tag) and TagValidator.COMMA in previous_formatted_tag: validation_error = error_reporter.report_error_type(TagValidator.COMMA_VALID_ERROR_TYPE, tag=original_tag, previous_tag=previous_original_tag); self._increment_issue_count(); elif not is_extension_tag: validation_error = error_reporter.report_error_type(TagValidator.VALID_ERROR_TYPE, tag=original_tag); self._increment_issue_count(); return validation_error;
def check_if_duplicate_tags_exist(self, original_tag_list, formatted_tag_list): """Reports a validation error if two or more tags are the same. Parameters ---------- original_tag_list: list A list containing tags that are used to report the error. formatted_tag_list: list A list containing tags that are used to do the validation. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; duplicate_indices = set([]); for tag_index, tag in enumerate(formatted_tag_list): all_indices = range(len(formatted_tag_list)); for duplicate_index in all_indices: if tag_index == duplicate_index: continue; if formatted_tag_list[tag_index] != TagValidator.TILDE and \ formatted_tag_list[tag_index] == formatted_tag_list[duplicate_index] and \ tag_index not in duplicate_indices and duplicate_index not in duplicate_indices: duplicate_indices.add(tag_index); duplicate_indices.add(duplicate_index); validation_error += error_reporter.report_error_type(TagValidator.DUPLICATE_ERROR_TYPE, tag=original_tag_list[tag_index]); self._increment_issue_count(); return validation_error;
def check_if_multiple_unique_tags_exist(self, original_tag_list, formatted_tag_list): """Reports a validation error if two or more tags start with a tag prefix that has the 'unique' attribute. Parameters ---------- original_tag_list: list A list containing tags that are used to report the error. formatted_tag_list: list A list containing tags that are used to do the validation. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; unique_tag_prefixes = self._hed_dictionary_dictionaries[TagValidator.UNIQUE_ERROR_TYPE]; for unique_tag_prefix in unique_tag_prefixes: unique_tag_prefix_boolean_mask = [x.startswith(unique_tag_prefix) for x in formatted_tag_list]; if sum(unique_tag_prefix_boolean_mask) > 1: validation_error += error_reporter.report_error_type( TagValidator.UNIQUE_ERROR_TYPE, tag_prefix=self._hed_dictionary_dictionaries[TagValidator.UNIQUE_ERROR_TYPE][unique_tag_prefix]); self._increment_issue_count(); return validation_error;
def check_if_tag_unit_class_units_are_valid(self, original_tag, formatted_tag): """Reports a validation error if the tag provided has a unit class and the units are incorrect. Parameters ---------- original_tag: string The original tag that is used to report the error. formatted_tag: string The tag that is used to do the validation. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; if not self.tag_is_valid(formatted_tag) and self.is_unit_class_tag(formatted_tag): tag_unit_classes = self.get_tag_unit_classes(formatted_tag); tag_unit_values = self.get_tag_name(formatted_tag); tag_unit_class_units = tuple(self.get_tag_unit_class_units(formatted_tag)); if TagValidator.TIME_UNIT_CLASS in tag_unit_classes and TagValidator.is_hh_mm_time(tag_unit_values): pass; elif re.search(TagValidator.DIGIT_EXPRESSION, TagValidator.strip_off_units_if_valid(tag_unit_values, tag_unit_class_units)): pass else: validation_error = error_reporter.report_error_type('unitClass', tag=original_tag, unit_class_units=','.join(tag_unit_class_units)); self._increment_issue_count(); return validation_error;
def report_invalid_character_error(error_tag): """Reports a error that is related with an invalid character Parameters ---------- error_tag: string The tag that caused the error. Returns ------- string The error message associated with a missing comma. """ error_tag = error_tag.strip(); return error_reporter.report_error_type(TagValidator.CHARACTER_ERROR_TYPE, tag=error_tag);
def report_missing_comma_error(error_tag): """Reports a error that is related with a missing comma. Parameters ---------- error_tag: string The tag that caused the error. Returns ------- string The error message associated with a missing comma. """ error_tag = error_tag[:-1].strip(); return error_reporter.report_error_type(TagValidator.COMMA_ERROR_TYPE, tag=error_tag);
def generate_row_issue_message(row_number, has_headers=True): """Generates a row issue message that is associated with a particular row in a spreadsheet. Parameters ---------- row_number: integer The row number that the issue is associated with. Returns ------- string The row issue message. """ if has_headers: row_number += 1; return error_reporter.report_error_type('row', error_row=row_number);
def check_number_of_group_tildes(self, tag_group): """Reports a validation error if the tag group has too many tildes. Parameters ---------- tag_group: list A list containing the tags in a group. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; if tag_group.count('~') > 2: validation_error = error_reporter.report_error_type(TagValidator.TILDE_ERROR_TYPE, tag_group); self._increment_issue_count(); return validation_error;
def find_comma_issues_in_hed_string(self, hed_string): """Reports a validation error if there are missing commas or commas in tags that take values. Parameters ---------- hed_string: string A hed string. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; current_tag = ''; last_non_empty_character = ''; character_indices_iterator = iter(range(len(hed_string))); for character_index in character_indices_iterator: character = hed_string[character_index]; current_tag += character; if not character.isspace(): if TagValidator.character_is_delimiter(character): current_tag = ''; if character == TagValidator.OPENING_GROUP_BRACKET: # If we have an opening group bracket by itself without a tag, it's actually starting a new group. if current_tag.strip() == TagValidator.OPENING_GROUP_BRACKET: current_tag = '' else: validation_error = error_reporter.report_error_type(TagValidator.VALID_ERROR_TYPE, tag=current_tag) self._increment_issue_count() break elif TagValidator.comma_is_missing_before_opening_bracket(last_non_empty_character, character): validation_error = TagValidator.report_missing_comma_error(current_tag); self._increment_issue_count(); break; elif TagValidator.comma_is_missing_after_closing_bracket(last_non_empty_character, character): validation_error = TagValidator.report_missing_comma_error(current_tag); self._increment_issue_count(); break; last_non_empty_character = character; return validation_error;
def check_if_tag_requires_child(self, original_tag, formatted_tag): """Reports a validation error if the tag provided has the 'requireChild' attribute. Parameters ---------- original_tag: string The original tag that is used to report the error. formatted_tag: string The tag that is used to do the validation. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; if self._hed_dictionary_dictionaries[TagValidator.REQUIRE_CHILD_ERROR_TYPE].get(formatted_tag): validation_error = error_reporter.report_error_type(TagValidator.REQUIRE_CHILD_ERROR_TYPE, tag=original_tag); self._increment_issue_count(); return validation_error;
def count_tag_group_brackets(self, hed_string): """Reports a validation error if there are an unequal number of opening or closing parentheses. This is the first check before the tags are parsed. Parameters ---------- hed_string: string A hed string. Returns ------- string A validation error string. If no errors are found then an empty string is returned. """ validation_error = ''; number_of_opening_brackets = hed_string.count('('); number_of_closing_brackets = hed_string.count(')'); if number_of_opening_brackets != number_of_closing_brackets: validation_error = error_reporter.report_error_type(TagValidator.BRACKET_ERROR_TYPE, opening_bracket_count=number_of_opening_brackets, closing_bracket_count=number_of_closing_brackets); self._increment_issue_count(); return validation_error;
def test_report_error_type(self): for error_type in self.error_types: error_report = error_reporter.report_error_type(error_type) self.assertIsInstance(error_report, str)