def processFunction( func_node, in_class=False ):

    fixIndents( func_node )

    func_gath = FunctionConverter( in_class )
    func_matches = func_gath.gather( func_node )
    func_gath.processAll( func_matches )

    gath_classes = [
        AssignmentConverter,
        BoolOpsConverter,
        ComparisonConverter,
        DecoratorConverter,
        DictComprehensionConverter,
        ExceptionConverter,
        ForLoopConverter,
        IfElifElseConverter,
        KeyWordCallConverter,
        ListComprehensionConverter,
        ListSliceConverter,
        NegateConverter,
        SelfConverter,
        StringInterpolationConverter,
        SuperConverter,
        TryExceptConverter,
        TupleConverter,
        WhileLoopConverter,
    ]

    ConverterChain( gath_classes ).convertAll( func_node )

    fixComments( func_node )
    fixPassStatements( func_node )
    fixSemicolons( func_node )
    fixSimpleRenames( func_node )
def processModule( module_nodes ):

    infos = AnonObj( class_names=[], func_names=[] )

    ConverterChain( [ ImportNameConverter, ImportFromConverter ] ).convertAll( module_nodes )

    class_gath = ClassConverter()
    class_matches = class_gath.gather( module_nodes )

    if not class_matches:
        print( "NO CLASSES" )
    else:
        for class_match in class_matches:
            print( "\nCLASS", class_match.name )
            class_gath.processOne( class_match )
            fixComments( class_match.suite )

            func_matches = FunctionConverter( in_class=True ).gather( class_match.suite )
            for func_match in func_matches:
                print( "----METHOD", func_match.name )
                processFunction( func_match.node, in_class=True )
            infos.class_names.append( str( class_match.name ).strip() )

    func_matches = FunctionConverter().gather( module_nodes )
    for func_match in func_matches:
        print( "FUNCTION ", func_match.name )
        processFunction( func_match.node )
        infos.func_names.append( str( func_match.name ).strip() )

    fixComments( module_nodes )
    fixPassStatements( module_nodes )
    fixSemicolons( module_nodes )
    fixSimpleRenames( module_nodes )

    return infos
def test_fixSimpleRenames_02():
    src = '''
        isinstance( a, AClass )
        zip( a, b )

    '''
    nodes = parseSource(src)

    fixSimpleRenames(nodes)

    # dumpTree( nodes )
    # dumpNodes( nodes )
    assert nodesToLines(nodes) == [
        '_pyjs.isInstance( a, AClass )', '_pyjs.listZip( a, b )'
    ]
def processTestModule(module_nodes):

    infos = AnonObj(class_names=[], func_names=[])

    # convert asserts before anything else
    assert_cvtr = AssertToChaiExpectConverter()
    assert_cvtr.processAll(assert_cvtr.gather(module_nodes))

    ConverterChain([ImportNameConverter,
                    ImportFromConverter]).convertAll(module_nodes)

    class_gath = UnittestClassToMochaDescribeConverter("unittest.TestCase")
    class_matches = class_gath.gather(module_nodes)

    if not class_matches:
        print("NO CLASSES")
    else:
        for class_match in class_matches:
            print("\nCLASS", class_match.name)
            class_gath.processOne(class_match)
            fixComments(class_match.suite)

            func_matches = PytestMethodToMochaItConverter(
                in_class=True).gather(class_match.suite)
            for func_match in func_matches:
                print("----METHOD", func_match.name)
                processTestFunction(func_match.node, in_class=True)
            infos.class_names.append(class_match.name.toString())

    func_matches = PytestMethodToMochaItConverter().gather(module_nodes)
    for func_match in func_matches:
        print("FUNCTION ", func_match.name)
        processTestFunction(func_match.node)
        infos.func_names.append(func_match.name.toString())

    fixComments(module_nodes)
    fixPassStatements(module_nodes)
    fixSimpleRenames(module_nodes)

    fixSemicolons(module_nodes)

    return infos
def test_fixSimpleRenames_01():
    src = '''
        x = None
        y = True
        z = False
        f = a.startswith( 'a' )
        g = b.endswith( 'b' )
        c.append( d )
        def __init__( self ): pass
        def __repr__( self ): return 'hhh'
        raise e
        del abc
        if hasattr( x, 'bam' ): pass
        y = getattr( x, 'bam', None )
        setattr( a, b, c )
        print( "bim bam bom" )
    '''
    nodes = parseSource(src)

    fixSimpleRenames(nodes)

    # dumpTree( nodes )
    # dumpNodes( nodes )
    assert nodesToLines(nodes) == [
        "x = null",
        "y = true",
        "z = false",
        "f = a.startsWith( 'a' )",
        "g = b.endsWith( 'b' )",
        "c.push( d )",
        "def constructor( self ): pass",
        "def toString( self ): return 'hhh'",
        "throw e",
        "delete abc",
        "if _pyjs.hasAttr( x, 'bam' ): pass",
        "y = _pyjs.getAttr( x, 'bam', null )",
        "_pyjs.setAttr( a, b, c )",
        'console.log( "bim bam bom" )',
    ]