Beispiel #1
0
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format='text'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)

        score_groups = cs.run(ds, [] if skip_checks is None else skip_checks,
                              *checker_names)

        if not score_groups:
            raise ValueError("No checks found, please check the name of the checker(s) and that they are installed")

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_format == 'text':
            if output_filename == '-':
                groups = cls.stdout_output(cs, score_groups, verbose, limit)
            # need to redirect output from stdout since print functions are
            # presently used to generate the standard report output
            else:
                with io.open(output_filename, 'w', encoding='utf-8') as f:
                    with stdout_redirector(f):
                        groups = cls.stdout_output(cs, score_groups, verbose,
                                                   limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
Beispiel #2
0
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format='text'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)

        score_groups = cs.run(ds, [] if skip_checks is None else skip_checks,
                              *checker_names)

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_format == 'text':
            if output_filename == '-':
                groups = cls.stdout_output(cs, score_groups, verbose, limit)
            # need to redirect output from stdout since print functions are
            # presently used to generate the standard report output
            else:
                with io.open(output_filename, 'w', encoding='utf-8') as f:
                    with stdout_redirector(f):
                        groups = cls.stdout_output(cs, score_groups, verbose,
                                                   limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc, limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc, limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
Beispiel #3
0
    def run_checker(cls,
                    ds_loc,
                    checker_names,
                    verbose,
                    criteria,
                    output_filename='stdout',
                    output_format='stdout'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        retval = True

        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)
        score_groups = cs.run(ds, *checker_names)

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_filename == '-' and output_format == 'text':
            groups = cls.stdout_output(cs, score_groups, verbose, limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
Beispiel #4
0
    def run_checker(cls, ds_loc, checker_names, verbose, criteria, output_filename='stdout', output_format='stdout'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        retval = True

        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)
        score_groups = cs.run(ds, *checker_names)

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_filename == '-' and output_format == 'text':
            groups = cls.stdout_output(cs, score_groups, verbose, limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc, limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc, limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format=['text']):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names   List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output(s)

        @returns                If the tests failed (based on the criteria)
        """
        cs = CheckSuite()
        # using OrderedDict is important here to preserve the order
        # of multiple datasets which may be passed in
        score_dict = OrderedDict()
        if not isinstance(ds_loc, six.string_types):
            locs = ds_loc
        # if single dataset, put in list
        else:
            locs = [ds_loc]

        # Make sure output format is a list
        if isinstance(output_format, six.string_types):
            output_format = [output_format]

        for loc in locs:
            ds = cs.load_dataset(loc)

            score_groups = cs.run(ds, [] if skip_checks is None else skip_checks,
                                *checker_names)

            if not score_groups:
                raise ValueError("No checks found, please check the name of the checker(s) and that they are installed")
            else:
                score_dict[loc] = score_groups

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        for out_fmt in output_format:
            if out_fmt == 'text':
                if output_filename == '-':
                    groups = cls.stdout_output(cs, score_dict, verbose, limit)
                # need to redirect output from stdout since print functions are
                # presently used to generate the standard report output
                else:
                    if len(output_format) > 1:
                        # Update file name if needed
                        output_filename = '{}.txt'.format(os.path.splitext(output_filename)[0])
                    with io.open(output_filename, 'w', encoding='utf-8') as f:
                        with stdout_redirector(f):
                            groups = cls.stdout_output(cs, score_dict, verbose,
                                                       limit)

            elif out_fmt == 'html':
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.html'.format(os.path.splitext(output_filename)[0])
                groups = cls.html_output(cs, score_dict, output_filename, ds_loc,
                                         limit)

            elif out_fmt == 'json' or 'json_new':
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.json'.format(os.path.splitext(output_filename)[0])
                groups = cls.json_output(cs, score_dict, output_filename, ds_loc,
                                         limit, out_fmt)

            else:
                raise TypeError('Invalid format %s' % out_fmt)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
Beispiel #6
0
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format=['text']):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names   List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output(s)

        @returns                If the tests failed (based on the criteria)
        """
        all_groups = []
        cs = CheckSuite()
        # using OrderedDict is important here to preserve the order
        # of multiple datasets which may be passed in
        score_dict = OrderedDict()
        if not isinstance(ds_loc, six.string_types):
            locs = ds_loc
        # if single dataset, put in list
        else:
            locs = [ds_loc]

        # Make sure output format is a list
        if isinstance(output_format, six.string_types):
            output_format = [output_format]

        for loc in locs: # loop through each dataset and run specified checks
            ds = cs.load_dataset(loc)

            score_groups = cs.run(ds, skip_checks, *checker_names)
            for group in score_groups.values():
                all_groups.append(group[0])
            # TODO: consider wrapping in a proper context manager instead
            if hasattr(ds, 'close'):
                ds.close()

            if not score_groups:
                raise ValueError("No checks found, please check the name of the checker(s) and that they are installed")
            else:
                score_dict[loc] = score_groups

        # define a score limit to truncate the ouput to the strictness level
        # specified by the user
        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        for out_fmt in output_format:
            if out_fmt == 'text':
                if output_filename == '-':
                    cls.stdout_output(cs, score_dict, verbose, limit)
                # need to redirect output from stdout since print functions are
                # presently used to generate the standard report output
                else:
                    if len(output_format) > 1:
                        # Update file name if needed
                        output_filename = '{}.txt'.format(os.path.splitext(output_filename)[0])
                    with io.open(output_filename, 'w', encoding='utf-8') as f:
                        with stdout_redirector(f):
                            cls.stdout_output(cs, score_dict, verbose, limit)

            elif out_fmt == 'html':
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.html'.format(os.path.splitext(output_filename)[0])
                cls.html_output(cs, score_dict, output_filename, ds_loc, limit)

            elif out_fmt in {'json', 'json_new'}:
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.json'.format(os.path.splitext(output_filename)[0])
                cls.json_output(cs, score_dict, output_filename, ds_loc, limit,
                                out_fmt)

            else:
                raise TypeError('Invalid format %s' % out_fmt)

            errors_occurred = cls.check_errors(score_groups, verbose)

        return (all(cs.passtree(groups, limit) for groups in all_groups),
                errors_occurred)
Beispiel #7
0
    def run_checker(
        cls,
        ds_loc,
        checker_names,
        verbose,
        criteria,
        skip_checks=None,
        output_filename="-",
        output_format=["text"],
        options=None,
    ):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names   List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output(s)

        @returns                If the tests failed (based on the criteria)
        """
        all_groups = []
        cs = CheckSuite(options=options or {})
        # using OrderedDict is important here to preserve the order
        # of multiple datasets which may be passed in
        score_dict = OrderedDict()
        if not isinstance(ds_loc, str):
            locs = ds_loc
        # if single dataset, put in list
        else:
            locs = [ds_loc]

        # Make sure output format is a list
        if isinstance(output_format, str):
            output_format = [output_format]

        for loc in locs:  # loop through each dataset and run specified checks
            ds = cs.load_dataset(loc)

            score_groups = cs.run(ds, skip_checks, *checker_names)
            for group in score_groups.values():
                all_groups.append(group[0])
            # TODO: consider wrapping in a proper context manager instead
            if hasattr(ds, "close"):
                ds.close()

            if not score_groups:
                raise ValueError(
                    "No checks found, please check the name of the checker(s) and that they are installed"
                )
            else:
                score_dict[loc] = score_groups

        # define a score limit to truncate the ouput to the strictness level
        # specified by the user
        if criteria == "normal":
            limit = 2
        elif criteria == "strict":
            limit = 1
        elif criteria == "lenient":
            limit = 3

        for out_fmt in output_format:
            if out_fmt == "text":
                if output_filename == "-":
                    cls.stdout_output(cs, score_dict, verbose, limit)
                # need to redirect output from stdout since print functions are
                # presently used to generate the standard report output
                else:
                    if len(output_format) > 1:
                        # Update file name if needed
                        output_filename = "{}.txt".format(
                            os.path.splitext(output_filename)[0])
                    with io.open(output_filename, "w", encoding="utf-8") as f:
                        with stdout_redirector(f):
                            cls.stdout_output(cs, score_dict, verbose, limit)

            elif out_fmt == "html":
                # Update file name if needed
                if len(output_format) > 1 and output_filename != "-":
                    output_filename = "{}.html".format(
                        os.path.splitext(output_filename)[0])
                cls.html_output(cs, score_dict, output_filename, ds_loc, limit)

            elif out_fmt in {"json", "json_new"}:
                # Update file name if needed
                if len(output_format) > 1 and output_filename != "-":
                    output_filename = "{}.json".format(
                        os.path.splitext(output_filename)[0])
                cls.json_output(cs, score_dict, output_filename, ds_loc, limit,
                                out_fmt)

            else:
                raise TypeError("Invalid format %s" % out_fmt)

            errors_occurred = cls.check_errors(score_groups, verbose)

        return (
            all(cs.passtree(groups, limit) for groups in all_groups),
            errors_occurred,
        )