Esempio n. 1
0
def format_file(filename, args, standard_out):
    """Run format_code() on a file.

    Return True if the new formatting differs from the original.

    """
    encoding = autopep8.detect_encoding(filename)
    with autopep8.open_with_encoding(filename,
                                     encoding=encoding) as input_file:
        source = input_file.read()

    if not source:
        return False

    formatted_source = format_code(source,
                                   aggressive=args.aggressive)

    if source != formatted_source:
        if args.in_place:
            with autopep8.open_with_encoding(filename, mode='w',
                                             encoding=encoding) as output_file:
                output_file.write(formatted_source)
        else:
            diff = autopep8.get_diff_text(
                io.StringIO(source).readlines(),
                io.StringIO(formatted_source).readlines(),
                filename)
            standard_out.write(''.join(diff))

        return True

    return False
Esempio n. 2
0
def format_file(filename, args, standard_out):
    """Run format_code() on a file.

    Return True if the new formatting differs from the original.

    """
    encoding = autopep8.detect_encoding(filename)
    with autopep8.open_with_encoding(filename,
                                     encoding=encoding) as input_file:
        source = input_file.read()

    if not source:
        return False

    formatted_source = format_code(source,
                                   aggressive=args.aggressive,
                                   apply_config=args.config,
                                   filename=filename)

    if source != formatted_source:
        if args.in_place:
            with autopep8.open_with_encoding(filename,
                                             mode='w',
                                             encoding=encoding) as output_file:
                output_file.write(formatted_source)
        else:
            diff = autopep8.get_diff_text(
                io.StringIO(source).readlines(),
                io.StringIO(formatted_source).readlines(), filename)
            standard_out.write(''.join(diff))

        return True

    return False
Esempio n. 3
0
def check(expected_filename, input_filename, aggressive):
    """Test and compare output.

    Return True on success.

    """
    got = autopep8.fix_file(
        input_filename,
        options=autopep8.parse_args([''] + aggressive * ['--aggressive']))

    try:
        with autopep8.open_with_encoding(expected_filename) as expected_file:
            expected = expected_file.read()
    except IOError:
        expected = None

    if expected == got:
        return True
    else:
        got_filename = expected_filename + '.err'
        encoding = autopep8.detect_encoding(input_filename)

        with autopep8.open_with_encoding(got_filename,
                                         encoding=encoding,
                                         mode='w') as got_file:
            got_file.write(got)

        print('{begin}{got} does not match expected {expected}{end}'.format(
            begin=RED, got=got_filename, expected=expected_filename, end=END),
              file=sys.stdout)

        return False
Esempio n. 4
0
def check_syntax(filename, raise_error=False):
    """Return True if syntax is okay."""
    with autopep8.open_with_encoding(filename) as input_file:
        try:
            compile(input_file.read(), '<string>', 'exec', dont_inherit=True)
            return True
        except (SyntaxError, TypeError, UnicodeDecodeError):
            if raise_error:
                raise
            else:
                return False
Esempio n. 5
0
def check_syntax(filename, raise_error=False):
    """Return True if syntax is okay."""
    with autopep8.open_with_encoding(filename) as input_file:
        try:
            compile(input_file.read(), '<string>', 'exec', dont_inherit=True)
            return True
        except (SyntaxError, TypeError, UnicodeDecodeError):
            if raise_error:
                raise
            else:
                return False
Esempio n. 6
0
def check(expected_filename, input_filename):
    """Test and compare output.

    Return True on success.

    """
    got = autopep8.fix_file(
        input_filename,
        options=autopep8.parse_args(['', '--aggressive'])[0])

    try:
        with autopep8.open_with_encoding(expected_filename) as expected_file:
            expected = expected_file.read()
    except IOError:
        expected = None

    if expected == got:
        return True
    else:
        got_filename = expected_filename + '.err'
        encoding = autopep8.detect_encoding(input_filename)

        with autopep8.open_with_encoding(got_filename,
                                         encoding=encoding,
                                         mode='w') as got_file:
            got_file.write(got)

        print(
            '{begin}{got} does not match expected {expected}{end}'.format(
                begin=RED,
                got=got_filename,
                expected=expected_filename,
                end=END),
            file=sys.stdout)

        return False
Esempio n. 7
0
def disassemble(filename):
    """dis, but without line numbers."""
    with autopep8.open_with_encoding(filename) as f:
        code = compile(f.read(), '<string>', 'exec')

    return filter_disassembly('\n'.join(_disassemble(code)))
Esempio n. 8
0
def ast_dump(filename):
    with autopep8.open_with_encoding(filename) as f:
        return ast.dump(ast.parse(f.read(), '<string>', 'exec'))
Esempio n. 9
0
def readlines(filename):
    """Return contents of file as a list of lines."""
    with autopep8.open_with_encoding(
            filename,
            encoding=autopep8.detect_encoding(filename)) as f:
        return f.readlines()
Esempio n. 10
0
def readlines(filename):
    """Return contents of file as a list of lines."""
    with autopep8.open_with_encoding(
            filename, encoding=autopep8.detect_encoding(filename)) as f:
        return f.readlines()
Esempio n. 11
0
def disassemble(filename):
    """dis, but without line numbers."""
    with autopep8.open_with_encoding(filename) as f:
        code = compile(f.read(), '<string>', 'exec')

    return filter_disassembly('\n'.join(_disassemble(code)))
Esempio n. 12
0
def ast_dump(filename):
    with autopep8.open_with_encoding(filename) as f:
        return ast.dump(ast.parse(f.read(), '<string>', 'exec'))