Exemple #1
0
def _main(filepaths, format='text', stream=sys.stdout, encoding=None):
    error_dicts = []
    error_occurred = False

    for filepath in filepaths:
        # Read and lint the file
        file_errors = lint_file(filepath, encoding=encoding)

        if not file_errors:
            if format == 'text':
                stream.write('INFO File {filepath} is clean.\n'.format(filepath=filepath))
        else:
            error_occurred = True
            if format == 'text':
                for err in file_errors:
                    # e.g. WARNING readme.rst:12 Title underline too short.
                    stream.write('{err.type} {err.source}:{err.line} {err.message}\n'.format(err=err))
            elif format == 'json':
                error_dicts.extend({
                    'line': error.line,
                    'source': error.source,
                    'level': error.level,
                    'type': error.type,
                    'message': error.message,
                    'full_message': error.full_message,
                } for error in file_errors)

    if format == 'json':
        stream.write(json.dumps(error_dicts))

    if error_occurred:
        sys.exit(1)
    else:
        sys.exit(0)
Exemple #2
0
def _main(paths, format=DEFAULT_FORMAT, stream=sys.stdout, encoding=None, level=LEVEL_MAP[DEFAULT_LEVEL_KEY],
          **kwargs):
    error_dicts = []
    error_occurred = False
    filepaths = []

    for path in paths:
        # Check if the given path is a file or a directory
        if os.path.isfile(path):
            filepaths.append(path)
        else:
            # Recurse over subdirectories to search for *.rst files
            for root, subdir, files in os.walk(path):
                for file in files:
                    if file.endswith('.rst'):
                        filepaths.append(os.path.join(root, file))

    for filepath in filepaths:
        # Read and lint the file
        unfiltered_file_errors = lint_file(filepath, encoding=encoding, **kwargs)
        file_errors = [err for err in unfiltered_file_errors if err.level >= level]

        if file_errors:
            error_occurred = True
            if format == 'text':
                for err in file_errors:
                    # e.g. WARNING readme.rst:12 Title underline too short.
                    stream.write('{err.type} {err.source}:{err.line} {err.message}\n'.format(err=err))
            elif format == 'json':
                error_dicts.extend({
                    'line': error.line,
                    'source': error.source,
                    'level': error.level,
                    'type': error.type,
                    'message': error.message,
                    'full_message': error.full_message,
                } for error in file_errors)

    if format == 'json':
        stream.write(json.dumps(error_dicts))

    if error_occurred:
        sys.exit(2)  # Using 2 for linting failure, 1 for internal error
    else:
        sys.exit(0)  # Success!
Exemple #3
0
def _main(filepaths,
          format=DEFAULT_FORMAT,
          stream=sys.stdout,
          encoding=None,
          level=LEVEL_MAP[DEFAULT_LEVEL_KEY],
          **kwargs):
    error_dicts = []
    error_occurred = False

    for filepath in filepaths:
        # Read and lint the file
        unfiltered_file_errors = lint_file(filepath,
                                           encoding=encoding,
                                           **kwargs)
        file_errors = [
            err for err in unfiltered_file_errors if err.level >= level
        ]

        if file_errors:
            error_occurred = True
            if format == 'text':
                for err in file_errors:
                    # e.g. WARNING readme.rst:12 Title underline too short.
                    stream.write(
                        '{err.type} {err.source}:{err.line} {err.message}\n'.
                        format(err=err))
            elif format == 'json':
                error_dicts.extend({
                    'line': error.line,
                    'source': error.source,
                    'level': error.level,
                    'type': error.type,
                    'message': error.message,
                    'full_message': error.full_message,
                } for error in file_errors)

    if format == 'json':
        stream.write(json.dumps(error_dicts))

    if error_occurred:
        sys.exit(2)  # Using 2 for linting failure, 1 for internal error
    else:
        sys.exit(0)  # Success!