Fix various deprecated code (via lib2to3).
196437
def fix_2to3(source,
             aggressive=True, select=None, ignore=None, filename=''):
    """"""
    if not aggressive:
        return source

    select = select or []
    ignore = ignore or []

    return refactor(source,
                    code_to_2to3(select=select,
                                 ignore=ignore),
                    filename=filename)
234481
def fix_examples_dir(lib_dir):
    """"""
    for x in lib_dir.dirs():
        if x.name.lower() == EXAMPLES:
            return
    for x in lib_dir.dirs():
        if x.name.lower() == EXAMPLES:
            _fix_dir(x)
            return
    for x in lib_dir.dirs():
        if 'example' in x.name.lower():
            _fix_dir(x)
            return
    for x in lib_dir.dirs():
        if len(files_multi_pattern(x, INO_PATTERNS)):
            _fix_dir(x)
            return
874970
def deprecated (func):
    """"""
    def newfunc (*args, **kwargs):
        """Print deprecated warning and execute original function."""
        warnings.warn("Call to deprecated function %s." % func.__name__,
                      category=DeprecationWarning)
        return func(*args, **kwargs)
    return update_func_meta(newfunc, func)
1010056
def deprecated(use_instead=None):
    """"""

    def wrapped(func):
        @wraps(func)
        def new_func(*args, **kwargs):
            message = "Call to deprecated function %s." % func.__name__
            if use_instead:
                message += " Use %s instead." % use_instead
            if not DISABLE_WARNINGS:
                warn(message, stacklevel=3)
            return func(*args, **kwargs)
        return new_func
    return wrapped
71697
def fix_code(code, directory):
    
    if not autopep8:
        raise Fault('autopep8 not installed, cannot fix code.',
                    code=400)
    old_dir = os.getcwd()
    try:
        os.chdir(directory)
        return autopep8.fix_code(code, apply_config=True)
    finally:
        os.chdir(old_dir)
468004
def deprecated(func):
    ''''''

    import warnings

    @functools.wraps(func)
    def new_func(*args, **kwargs):
        if is_python_3:
            code = func.__code__
        else:
            code = func.func_code
        warnings.warn_explicit(
            "Call to deprecated function {}.".format(func.__name__),
            category=Warning,
            filename=code.co_filename,
            lineno=code.co_firstlineno + 1
        )
        return func(*args, **kwargs)
    return new_func
369972
def fix_e702(self, result, logical):
        """"""
        if not logical:
            return []  # pragma: no cover
        logical_lines = logical[2]

        # Avoid applying this when indented.
        # https://docs.python.org/reference/compound_stmts.html
        for line in logical_lines:
            if (result['id'] == 'E702' and ':' in line
                    and STARTSWITH_DEF_REGEX.match(line)):
                return []

        line_index = result['line'] - 1
        target = self.source[line_index]

        if target.rstrip().endswith('\\'):
            # Normalize '1; \\\n2' into '1; 2'.
            self.source[line_index] = target.rstrip('\n \r\t\\')
            self.source[line_index + 1] = self.source[line_index + 1].lstrip()
            return [line_index + 1, line_index + 2]

        if target.rstrip().endswith(';'):
            self.source[line_index] = target.rstrip('\n \r\t;') + '\n'
            return [line_index + 1]

        offset = result['column'] - 1
        first = target[:offset].rstrip(';').rstrip()
        second = (_get_indentation(logical_lines[0]) +
                  target[offset:].lstrip(';').lstrip())

        # Find inline comment.
        inline_comment = None
        if target[offset:].lstrip(';').lstrip()[:2] == '# ':
            inline_comment = target[offset:].lstrip(';')

        if inline_comment:
            self.source[line_index] = first + inline_comment
        else:
            self.source[line_index] = first + '\n' + second
        return [line_index + 1]
443368
def deprecated(func):
    """"""
    def newFunc(*args, **kwargs):
        print("Call to deprecated function %s." % func.__name__)
        return func(*args, **kwargs)
    newFunc.__name__ = func.__name__
    newFunc.__doc__ = func.__doc__
    newFunc.__dict__.update(func.__dict__)
    return newFunc
833354
def fix(self):
        """"""
        pep8_options = {
            'ignore': self.options.ignore,
            'select': self.options.select,
            'max_line_length': self.options.max_line_length,
            'hang_closing': self.options.hang_closing,
        }
        results = _execute_pep8(pep8_options, self.source)

        if self.options.verbose:
            progress = {}
            for r in results:
                if r['id'] not in progress:
                    progress[r['id']] = set()
                progress[r['id']].add(r['line'])
            print('--->  {n} issue(s) to fix {progress}'.format(
                n=len(results), progress=progress), file=sys.stderr)

        if self.options.line_range:
            start, end = self.options.line_range
            results = [r for r in results
                       if start <= r['line'] <= end]

        self._fix_source(filter_results(source=''.join(self.source),
                                        results=results,
                                        aggressive=self.options.aggressive))

        if self.options.line_range:
            # If number of lines has changed then change line_range.
            count = sum(sline.count('\n')
                        for sline in self.source[start - 1:end])
            self.options.line_range[1] = start + count - 1

        return ''.join(self.source)
162147
def _depr(fn, usage, stacklevel=3):
    """"""
    warn('{0} is deprecated. Use {1} instead'.format(fn, usage),
         stacklevel=stacklevel, category=DeprecationWarning)
1356450
def fix_e262(self, result):
        """"""
        target = self.source[result['line'] - 1]
        offset = result['column']

        code = target[:offset].rstrip(' \t#')
        comment = target[offset:].lstrip(' \t#')

        fixed = code + ('  # ' + comment if comment.strip() else '\n')

        self.source[result['line'] - 1] = fixed
1019317
def fix_e113(self, result):
        """"""
        line_index = result['line'] - 1
        target = self.source[line_index]
        indent = _get_indentation(target)
        stripped = target.lstrip()
        self.source[line_index] = indent[1:] + stripped
799560
def deprecated(function, instead):
    """"""

    # skip for classes, breaks doc generation
    if not isinstance(function, types.FunctionType):
        return function

    @wraps(function)
    def wrap(*args, **kwargs):
        warnings.warn("Deprecated, use %s instead" % instead,
                      PyGIDeprecationWarning)
        return function(*args, **kwargs)

    return wrap
1039385
def clean_python(ctx, dry_run=False):
    """"""
    # MAYBE NOT: "**/__pycache__"
    cleanup_dirs(["build", "dist", "*.egg-info", "**/__pycache__"],
                 dry_run=dry_run)
    if not dry_run:
        ctx.run("py.cleanup")
    cleanup_files(["**/*.pyc", "**/*.pyo", "**/*$py.class"], dry_run=dry_run)
285919
def fix_ofiles(self):
        
        filepaths = self.outdir.list_filepaths()
        #logger.info("in fix_ofiles with filepaths %s" % list(filepaths))

        old2new = FilepathFixer().fix_paths(filepaths)

        for old, new in old2new.items():
            self.history.info("will rename old %s to new %s" % (old, new))
            os.rename(old, new)
335729
def rewrite_file_imports(item, vendored_libs):
    """"""
    text = item.read_text(encoding='utf-8')
    for lib in vendored_libs:
        text = re.sub(
            r'(\n\s*)import %s(\n\s*)' % lib,
            r'\1from pythonfinder._vendor import %s\2' % lib,
            text,
        )
        text = re.sub(
            r'(\n\s*)from %s' % lib,
            r'\1from pythonfinder._vendor.%s' % lib,
            text,
        )
    item.write_text(text, encoding='utf-8')
1258010
def command_py2to3(args):
    
    from lib2to3.main import main
    sys.exit(main("lib2to3.fixes", args=args.sources))