Exemple #1
0
def fix_file(file_name,
             line_ranges,
             options=None,
             in_place=False,
             diff=False,
             verbose=0,
             cwd=None):
    """Calls fix_code on the source code from the passed in file over the given
    line_ranges.

    - If diff then this returns the udiff for the changes, otherwise
    returns the fixed code.
    - If in_place the changes are written to the file.

    """
    import codecs
    from os import getcwd
    from pep8radius.diff import get_diff
    from pep8radius.shell import from_dir

    if cwd is None:
        cwd = getcwd()

    with from_dir(cwd):
        try:
            with codecs.open(file_name, 'r', encoding='utf-8') as f:
                original = f.read()
        except IOError:
            # Most likely the file has been removed.
            # Note: it would be nice if we could raise here, specifically
            # for the case of passing in a diff when in the wrong directory.
            return ''

    fixed = fix_code(original, line_ranges, options, verbose=verbose)

    if in_place:
        with from_dir(cwd):
            with codecs.open(file_name, 'w', encoding='utf-8') as f:
                f.write(fixed)

    return get_diff(original, fixed, file_name) if diff else fixed
Exemple #2
0
def fix_file(file_name, line_ranges, options=None, in_place=False,
             diff=False, verbose=0, cwd=None):
    """Calls fix_code on the source code from the passed in file over the given
    line_ranges.

    - If diff then this returns the udiff for the changes, otherwise
    returns the fixed code.
    - If in_place the changes are written to the file.

    """
    import codecs
    from os import getcwd
    from pep8radius.diff import get_diff
    from pep8radius.shell import from_dir

    if cwd is None:
        cwd = getcwd()

    with from_dir(cwd):
        try:
            with codecs.open(file_name, 'r', encoding='utf-8') as f:
                original = f.read()
        except IOError:
            # Most likely the file has been removed.
            # Note: it would be nice if we could raise here, specifically
            # for the case of passing in a diff when in the wrong directory.
            return ''

    fixed = fix_code(original, line_ranges, options, verbose=verbose)

    if in_place:
        with from_dir(cwd):
            with codecs.open(file_name, 'w', encoding='utf-8') as f:
                f.write(fixed)

    return get_diff(original, fixed, file_name) if diff else fixed
Exemple #3
0
def get_diff_many(modified, expected, files):
    return ''.join(get_diff(*mef) for mef in zip(modified, expected, files))
Exemple #4
0
def get_diff_many(modified, expected, files):
    return ''.join(get_diff(*mef)
                   for mef in zip(modified, expected, files))