def validate_column(self, validators, error_handler, **kwargs): """ Run the given validators on this column Parameters ---------- validators : [func or validator like] or func or validator like A validator or list of validators to apply to the hed strings in the columns. error_handler : ErrorHandler or None Used to report errors. Uses a default one if none passed in. kwargs: See util.translate_ops or the specific validators for additional options Returns ------- col_issues: [{}] A list of issues found by the given validators. """ if error_handler is None: error_handler = error_reporter.ErrorHandler() if not isinstance(validators, list): validators = [validators] validators = validators.copy() validators.append(self._validate_pound_sign_count) error_handler.push_error_context(ErrorContext.SIDECAR_COLUMN_NAME, self.column_name) col_validation_issues = self._run_ops(validators, allow_placeholders=True, error_handler=error_handler, **kwargs) col_validation_issues += self._validate_column_structure(error_handler) col_validation_issues += self.get_definition_issues() error_handler.pop_error_context() return col_validation_issues
def setUpClass(cls): if cls.schema_file: hed_xml = os.path.join(os.path.dirname(os.path.abspath(__file__)), cls.schema_file) cls.hed_schema = schema.load_schema(hed_xml) elif not cls.hed_schema: raise ValueError("No schema set for test case") cls.error_handler = error_reporter.ErrorHandler()
def setUpClass(cls): super().setUpClass() cls.error_handler = error_reporter.ErrorHandler() cls.syntactic_hed_input_reader = HedValidator( hed_schema=None, run_semantic_validation=False) cls.syntactic_tag_validator = cls.syntactic_hed_input_reader._tag_validator cls.semantic_hed_input_reader = HedValidator( hed_schema=cls.hed_schema, run_semantic_validation=True) cls.semantic_tag_validator = cls.semantic_hed_input_reader._tag_validator
def setUpClass(cls): schema_file = '../data/hed_pairs/HED8.0.0t.xml' hed_xml = os.path.join(os.path.dirname(os.path.abspath(__file__)), schema_file) hed_schema1 = schema.load_schema(hed_xml) hed_schema2 = schema.load_schema(hed_xml, library_prefix="tl:") cls.hed_schema = HedSchemaGroup([hed_schema1, hed_schema2]) cls.error_handler = error_reporter.ErrorHandler() super().setUpClass()
def validate_entries(self, validators=None, name=None, extra_def_dicts=None, error_handler=None, **kwargs): """Run the given validators on all columns in this sidecar Parameters ---------- validators : [func or validator like] or func or validator like A validator or list of validators to apply to the hed strings in this sidecar. name: str If present, will use this as the filename for context, rather than using the actual filename Useful for temp filenames. extra_def_dicts: [DefDict] or DefDict or None If present, also use these in addition to the sidecars def dicts. error_handler : ErrorHandler or None Used to report errors. Uses a default one if none passed in. kwargs: See util.translate_ops or the specific validators for additional options Returns ------- validation_issues: [{}] The list of validation issues found """ if error_handler is None: error_handler = error_reporter.ErrorHandler() if not name: name = self.name if name: error_handler.push_error_context(ErrorContext.FILE_NAME, name, False) validators = self._add_definition_mapper(validators, extra_def_dicts) all_validation_issues = [] for column_data in self: all_validation_issues += column_data.validate_column(validators, error_handler=error_handler, **kwargs) if name: error_handler.pop_error_context() return all_validation_issues
def check_compliance(hed_schema, also_check_for_warnings=True, name=None, error_handler=None): """ Checks for hed3 compliance of a schema object. Parameters ---------- hed_schema : HedSchema HedSchema object to check for hed3 compliance also_check_for_warnings : bool, default True If True, also checks for formatting issues like invalid characters, capitalization, etc. name: str If present, will use this as the filename for context, rather than using the actual filename Useful for temp filenames. error_handler : ErrorHandler or None Used to report errors. Uses a default one if none passed in. Returns ------- issue_list : [{}] A list of all warnings and errors found in the file. """ if not isinstance(hed_schema, HedSchema): raise ValueError( "To check compliance of a HedGroupSchema, call self.check_compliance on the schema itself." ) if error_handler is None: error_handler = error_reporter.ErrorHandler() issues_list = [] if not name: name = hed_schema.filename error_handler.push_error_context(ErrorContext.FILE_NAME, name) unknown_attributes = hed_schema.get_all_unknown_attributes() if unknown_attributes: for attribute_name, source_tags in unknown_attributes.items(): for tag in source_tags: issues_list += error_handler.format_error_with_context( SchemaErrors.HED_SCHEMA_ATTRIBUTE_INVALID, attribute_name, source_tag=tag) schema_attribute_validators = { HedKey.SuggestedTag: tag_exists_check, HedKey.RelatedTag: tag_exists_check, HedKey.UnitClass: tag_is_placeholder_check, HedKey.ValueClass: tag_is_placeholder_check, } # Check attributes for section_key in hed_schema._sections: error_handler.push_error_context(ErrorContext.SCHEMA_SECTION, section_key) # Check attributes for tag_entry in hed_schema[section_key].values(): error_handler.push_error_context(ErrorContext.SCHEMA_TAG, tag_entry.name) for attribute_name in tag_entry.attributes: validator = schema_attribute_validators.get(attribute_name) if validator: error_handler.push_error_context( ErrorContext.SCHEMA_ATTRIBUTE, attribute_name, False) new_issues = validator( hed_schema, tag_entry, tag_entry.attributes[attribute_name]) error_handler.add_context_to_issues(new_issues) issues_list += new_issues error_handler.pop_error_context() error_handler.pop_error_context() # Check duplicate names for name, duplicate_entries in hed_schema[ section_key].duplicate_names.items(): issues_list += error_handler.format_error_with_context( SchemaErrors.HED_SCHEMA_DUPLICATE_NODE, name, duplicate_tag_list=[entry.name for entry in duplicate_entries], section=section_key) error_handler.pop_error_context() if also_check_for_warnings: hed_terms = hed_schema.get_all_schema_tags(True) for hed_term in hed_terms: issues_list += validate_schema_term(hed_term) for tag_name, desc in hed_schema.get_desc_iter(): issues_list += validate_schema_description(tag_name, desc) error_handler.pop_error_context() return issues_list