Пример #1
0
    def validate(self, validators=None, error_handler=None, **kwargs):
        """
            Run the given validators on this string.

        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.
        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
        -------

        """
        if error_handler is None:
            error_handler = ErrorHandler()
        tag_ops = translate_ops(validators, **kwargs)

        error_handler.push_error_context(ErrorContext.HED_STRING,
                                         self,
                                         increment_depth_after=False)
        issues = self.apply_ops(tag_ops)
        error_handler.add_context_to_issues(issues)
        error_handler.pop_error_context()

        return issues
Пример #2
0
    def hed_string_iter(self, validators=None, error_handler=None, **kwargs):
        """
        Return iterator to loop over all hed strings in this column definition

        Parameters
        ----------
        validators : [func or validator like] or func or validator like
            A validator or list of validators to apply to the hed strings before returning
        error_handler : ErrorHandler
            The error handler to use for context, uses a default one if none.
        kwargs:
            See util.translate_ops or the specific validators for additional options
        Yields
        -------
        hed_string : HedString
            hed_string at a given column and key position
        position: str
            Indicates where hed_string was loaded from so it can be later set by the user
        issues: [{}]
            List of issues found applying validators
        """
        if error_handler is None:
            error_handler = ErrorHandler()

        if not isinstance(self._hed_dict, dict):
            return

        tag_ops = []
        if validators:
            tag_ops = translate_ops(validators,
                                    error_handler=error_handler,
                                    **kwargs)

        for hed_string_obj, key_name in self._hed_iter():
            new_col_issues = []
            error_handler.push_error_context(ErrorContext.SIDECAR_KEY_NAME,
                                             key_name)
            if not hed_string_obj:
                new_col_issues += ErrorHandler.format_error(
                    SidecarErrors.BLANK_HED_STRING)
                error_handler.add_context_to_issues(new_col_issues)
                yield hed_string_obj, key_name, new_col_issues
            else:
                error_handler.push_error_context(ErrorContext.HED_STRING,
                                                 hed_string_obj,
                                                 increment_depth_after=False)
                if tag_ops:
                    new_col_issues += hed_string_obj.apply_ops(tag_ops)

                error_handler.add_context_to_issues(new_col_issues)
                yield hed_string_obj, key_name, new_col_issues
                error_handler.pop_error_context()
            error_handler.pop_error_context()