'Refactors a file.'

def refactor_file(self, filename, write=False, doctests_only=False):

    (input, encoding) = self._read_python_source(filename)
    if (input is None):
        return
    input += u'\n'
    if doctests_only:
        self.log_debug('Refactoring    doctests    in    %s', filename)
        output = self.refactor_docstring(input, filename)
        if (self.write_unchanged_files or (output != input)):
            self.processed_file(output, filename, input, write, encoding)
        else:
            self.log_debug('No    doctest    changes    in    %s', filename)
    else:
        tree = self.refactor_string(input, filename)
        if (self.write_unchanged_files or (tree and tree.was_changed)):
            self.processed_file(unicode(tree)[:(-1)], filename, write=write, encoding=encoding)
        else:
            self.log_debug('No    changes    in    %s', filename)
