コード例 #1
0
def _Run2to3Fixers(source, options):
    """Use lib2to3 to reformat the source.
  Args:
    source: (unicode) The source to reformat.
    options: dictionary of options to pass to lib2to3_refactor.RefactoringTool
  Returns:
    Reformatted source code.
  """
    fixer_names = [MODULE_NAME_PREFIX + fixer for fixer in options['fixers']]
    options['print_function'] = True
    try:
        try:
            tool = lib2to3_refactor.RefactoringTool(fixer_names=fixer_names,
                                                    explicit=fixer_names,
                                                    options=options)
            return '{}'.format(tool.refactor_string(source, name=''))
        except pgen2_parse.ParseError:
            options['print_function'] = False
            tool = lib2to3_refactor.RefactoringTool(fixer_names=fixer_names,
                                                    explicit=fixer_names,
                                                    options=options)
            return '{}'.format(tool.refactor_string(source, name=''))
    except (pgen2_tokenize.TokenError, pgen2_parse.ParseError, SyntaxError,
            UnicodeDecodeError, UnicodeEncodeError) as err:
        logging.error(err)
        raise
コード例 #2
0
 def test_print_function_option(self):
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always", DeprecationWarning)
         refactor.RefactoringTool(_DEFAULT_FIXERS, {"print_function": True})
     self.assertEqual(len(w), 1)
     msg, = w
     self.assertTrue(msg.category is DeprecationWarning)
コード例 #3
0
 def refactor(self):
     try:
         factory = refactor.RefactoringTool(refactor.get_fixers_from_package(lib2to3_fixer_packages[0]))
         return factory.refactor_string(self.code, "test code")
     except Exception as e:
         print(getattr(e, "message", repr(e)))
         return "print invalid"
コード例 #4
0
 def __init__(self):
     super().__init__()
     fixes = set(refactor.get_fixers_from_package("lib2to3.fixes"))
     self.refactorer_2to3 = refactor.RefactoringTool(fixes)
     self.lattice = TypeLatticeGenerator(
         os.path.join("processing", "typilus", "src", "data_preparation", "metadata", "typingRules.json")
     )
コード例 #5
0
ファイル: extract_ast.py プロジェクト: smartshark/coastSHARK
def convert_2to3(file_content, file_name):
    """Quick helper function to convert python2 to python3 so that we can keep the ast buildin."""

    # all default fixers
    avail_fixes = set(refactor.get_fixers_from_package("lib2to3.fixes"))

    # create default RefactoringTool, apply to passed file_content string and return fixed string
    rt = refactor.RefactoringTool(avail_fixes)
    tmp = rt.refactor_string(file_content, file_name)
    return str(tmp)
コード例 #6
0
ファイル: convert2to3.py プロジェクト: jameskress/visit
def ConvertPy2to3(py_script_text):
    """
    Converts contents of input string py_script_text to python 3 using lib2to3
    and returns the result as a string.
    """
    # As far as I can tell, lib2to3 isn't documented that well but once
    # you find the right recipe it's very easy to use for this case.
    # ref:
    # https://stackoverflow.com/questions/30340151/using-2to3-on-in-memory-scripts
    fixes = refactor.get_fixers_from_package('lib2to3.fixes')
    converter = refactor.RefactoringTool(fixes)
    ast = converter.refactor_string(py_script_text, '<script>')
    return str(ast)
コード例 #7
0
def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None):
    """
    A convenience function for creating a RefactoringTool for tests.

    fixers is a list of fixers for the RefactoringTool to use. By default
    "lib2to3.fixes.*" is used. options is an optional dictionary of options to
    be passed to the RefactoringTool.
    """
    if fixers is not None:
        fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers]
    else:
        fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes")
    options = options or {}
    return refactor.RefactoringTool(fixers, options, explicit=True)
コード例 #8
0
ファイル: subp_lib.py プロジェクト: uri-mog/dreampie
def build(log=simple_logger, force=False):
    dreampielib_dir = dirname(abspath(__file__))
    src_dir = dirname(dreampielib_dir)
    build_dir = join(dreampielib_dir, 'data')

    if py3_available:
        avail_fixes = refactor.get_fixers_from_package('lib2to3.fixes')
        rt = refactor.RefactoringTool(avail_fixes)

    for ver in lib_vers:
        lib_fn = join(build_dir, lib_fns[ver])

        # Make dirs if they don't exist yet
        if not os.path.exists(lib_fn):
            os.mkdir(lib_fn)
        for dir in dirs:
            dir_fn = join(lib_fn, dir)
            if not os.path.exists(dir_fn):
                os.mkdir(dir_fn)

        # Write files if not up to date
        for fn in files:
            src_fn = join(src_dir, fn)
            dst_fn = join(lib_fn, fn)
            if not force and not newer(src_fn, dst_fn):
                continue

            if ver == 3:
                log.info("Converting %s to Python 3..." % fn)
            else:
                log.info("Copying %s..." % fn)

            f = open(join(src_dir, fn), 'rb')
            src = f.read()
            f.close()

            if ver == 3:
                dst = str(rt.refactor_string(src + '\n', fn))[:-1]
            else:
                dst = src

            dst = """\
# This file was automatically generated from a file in the source DreamPie dir.
# DO NOT EDIT IT, as your changes will be gone when the file is created again.

""" + dst

            f = open(dst_fn, 'wb')
            f.write(dst)
            f.close()
コード例 #9
0
from Bio._py3k import _universal_read_mode

import unittest
import doctest
import os
import sys
import warnings
from Bio import BiopythonExperimentalWarning

warnings.simplefilter('ignore', BiopythonExperimentalWarning)

if sys.version_info[0] >= 3:
    from lib2to3 import refactor
    fixers = refactor.get_fixers_from_package("lib2to3.fixes")
    fixers.remove("lib2to3.fixes.fix_print")  # Already using print function
    rt = refactor.RefactoringTool(fixers)
    assert rt.refactor_docstring(">>> print(2+2)\n4\n", "example1") == \
                                 ">>> print(2+2)\n4\n"
    assert rt.refactor_docstring('>>> print("Two plus two is", 2+2)\n'
                                 'Two plus two is 4\n', "example2") == \
                                 '>>> print("Two plus two is", 2+2)\nTwo plus two is 4\n'

# Cache this to restore the cwd at the end of the tests
original_path = os.path.abspath(".")

if os.path.basename(sys.argv[0]) == "test_Tutorial.py":
    # sys.argv[0] will be (relative) path to test_Turorial.py - use this to allow, e.g.
    # [base]$ python Tests/test_Tutorial.py
    # [Tests/]$ python test_Tutorial.py
    tutorial_base = os.path.abspath(
        os.path.join(os.path.dirname(sys.argv[0]), "../Doc/"))
コード例 #10
0
 def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):
     return refactor.RefactoringTool(fixers, options, explicit)
コード例 #11
0
def fix_python2(code):
    tool = refactor.RefactoringTool(fixers, {}, explicit=True)

    return str(tool.refactor_string(code + '\n', 'test'))
コード例 #12
0
ファイル: 2to3.py プロジェクト: niksaz/semantic-code-search
from lib2to3 import refactor

code = 'print "old-fashioned print"'
fixes = set(refactor.get_fixers_from_package("lib2to3.fixes"))
conversion_tool = refactor.RefactoringTool(fixes)
converted_code = str(conversion_tool.refactor_string(code + '\n', 'some_name'))
print(converted_code)  # print("old-fashioned print")
コード例 #13
0
import unittest
import doctest
import os
import sys

if sys.version_info[0] >= 3:
    from lib2to3 import refactor
    rt = refactor.RefactoringTool(
        refactor.get_fixers_from_package("lib2to3.fixes"))
    assert rt.refactor_docstring(">>> print 2+2\n4\n", "example") == \
           ">>> print(2+2)\n4\n"

tutorial = os.path.join(os.path.dirname(sys.argv[0]), "../Doc/Tutorial.tex")
tutorial_base = os.path.abspath(
    os.path.join(os.path.dirname(sys.argv[0]), "../Doc/"))
original_path = os.path.abspath(".")


def _extract(handle):
    line = handle.readline()
    if line != "\\begin{verbatim}\n":
        raise ValueError(
            "Any '%doctest' or '%cont-doctest' line should be followed by '\\begin{verbatim}'"
        )
    lines = []
    while True:
        line = handle.readline()
        if not line:
            if lines:
                print "".join(lines[:30])
                raise ValueError("Didn't find end of test starting: %r",
コード例 #14
0
import ast, _ast
import typing

from stackrunner._meta import errors

# Never thought I'd import this... What a world we live in
from lib2to3 import refactor

fixers = refactor.get_fixers_from_package(
    'lib2to3.fixes')  # Why is this package like this?
refactoring_tool = refactor.RefactoringTool(fixers)


class RemovePrints(ast.NodeTransformer):
    def visit_Expr(self, node):
        if isinstance(node, _ast.Expr) \
                and isinstance(node.value, _ast.Call) \
                and isinstance(node.value.func, _ast.Name) \
                and node.value.func.id == 'print':
            return ast.Pass()  # replace prints with noop
        return node


print_remover = RemovePrints()


def remove_prints(tree: ast.AST) -> ast.AST:
    return print_remover.visit(tree)


def refactor2to3(code: str) -> str:
コード例 #15
0
 def create_refactoring_tool(self, fixer_names):
     """Create the refactoring tool from a list of fixer names."""
     return refactor.RefactoringTool(fixer_names)
コード例 #16
0
import sys
from lib2to3 import refactor

infile = sys.argv[1]
fixers = ['fixer_generic']
tool = refactor.RefactoringTool(fixers, {}, explicit=True)
class_code = open(infile).read()
print tool.refactor_string(class_code, 'test')