Esempio n. 1
0
def Compile(f):
    """'Compile' the function f"""
    # Parse and extract the function definition AST
    fun = ast.parse(inspect.getsource(f)).body[0]
    print(astor.dump(fun))

    simpleFun = PythonToSimple().visit(fun)

    print(astor.dump(simpleFun))

    return f
Esempio n. 2
0
def Compile(f):
    """'Compile' the function f"""
    # Parse and extract the function definition AST
    fun = ast.parse(textwrap.dedent(inspect.getsource(f))).body[0]
    print("Python AST:\n{}\n".format(astor.dump(fun)))

    simpleFun = PythonToSimple().visit(fun)

    print("Simple IR:\n{}\n".format(astor.dump(simpleFun)))

    # package up our generated simple IR in a
    def run(*args):
        return Interpret(simpleFun, *args)

    return run
Esempio n. 3
0
def getAST(code):
    try:
        a = tokenize(code)
        b = hy_compile(a, "hdb")
        ret = astor.dump(b)
    except PrematureEndOfInput:
        ret = "missing a paren"
    return ret
Esempio n. 4
0
def getAST(code):
    try:
        a = tokenize(code)
        b = hy_compile(a, "hdb")
        ret = astor.dump(b)
    except PrematureEndOfInput:
        ret = "missing a paren"
    return ret
Esempio n. 5
0
File: poc.py Progetto: hylang/hdb
def getAST():
    code = edit_object.edit_text
    try:
        a = tokenize(code)
        b = hy_compile(a, "hdb")
        ret = astor.dump(b)
    except PrematureEndOfInput:
        ret = "missing a paren"
    return ret
Esempio n. 6
0
    def visit(self, node):
        if type(node) in (list, str, type(None), ast.Store, ast.Load):
            return

        if isinstance(node, (int, str)):
            self.write(str(node))
            return

        name = node.__class__.__name__
        try:
            walker = getattr(self, 'visit_' + name)
        except AttributeError:
            print()
            print('node:', name, file=sys.stderr)
            print('data:', node.__dict__, file=sys.stderr)
            astor.dump(node)
            raise NotImplementedError('node type not supported: %s' % name)
        walker(node)
Esempio n. 7
0
File: poc.py Progetto: 10long/hdb
def getAST():
    code = edit_object.edit_text
    try:
        a = tokenize(code)
        b = hy_compile(a, "hdb")
        ret = astor.dump(b)
    except PrematureEndOfInput:
        ret = "missing a paren"
    return ret
Esempio n. 8
0
def rebuild_bbscript(sourceFilename, outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    f = open(outFilename + ".txt", "w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename, "wb")
    Rebuilder().visit(sourceAST)
    output.close()
Esempio n. 9
0
    def visit_If(self, node):
        find = False
        try:
            find = node.body[0].value.func.id == "_gotolabel"
        except:
            pass

        if isinstance(node.test, Name) and find:
            writeCommandByID(18, [node.body[0].value.args[0]] +
                             decodeVar(node.test))
        elif isinstance(node.test, Name):
            #This is if(SLOT) we need to find out slot index and write it as param of if
            writeCommandByName("if", decodeVar(node.test))
            self.visit_body(node.body)
            writeCommandByName("endIf", [])
            if len(node.orelse) > 0:
                writeCommandByName("else", [])
                self.visit_body(node.orelse)
                writeCommandByName("endElse", [])
        elif isinstance(node.test, UnaryOp) and isinstance(
                node.test.operand, Name):
            #This is if(SLOT) we need to find out slot index and write it as param of if
            writeCommandByName("ifNot", decodeVar(node.test.operand))
            self.visit_body(node.body)
            writeCommandByName("endIfNot", [])
            if len(node.orelse) > 0:
                writeCommandByName("else", [])
                self.visit_body(node.orelse)
                writeCommandByName("endElse", [])
        elif (isinstance(node.test, Call)
              or isinstance(node.test, Compare)) and find:
            self.visit(node.test)
            writeCommandByID(18, [node.body[0].value.args[0], 2, 0])
        elif (isinstance(node.test, Call) or isinstance(node.test, Compare)):
            self.visit(node.test)
            writeCommandByName("if", [2, 0])
            self.visit_body(node.body)
            writeCommandByName("endIf", [])
            if len(node.orelse) > 0:
                writeCommandByName("else", [])
                self.visit_body(node.orelse)
                writeCommandByName("endElse", [])
        elif isinstance(node.test,
                        UnaryOp) and (isinstance(node.test.operand, Call) or
                                      isinstance(node.test.operand, Compare)):
            self.visit(node.test.operand)
            writeCommandByName("ifNot", [2, 0])
            self.visit_body(node.body)
            writeCommandByName("endIfNot", [])
            if len(node.orelse) > 0:
                writeCommandByName("else", [])
                self.visit_body(node.orelse)
                writeCommandByName("endElse", [])
        else:
            print "UNHANDLED IF", astor.dump(node)
Esempio n. 10
0
 def visit_If(self, node):
     if isinstance(node.test, Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("if", decodeVar(node.test))
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif isinstance(node.test, UnaryOp) and isinstance(
             node.test.operand, Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("ifNot", decodeVar(node.test.operand))
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif (isinstance(node.test, Call) or isinstance(node.test, Compare)):
         if isinstance(node.test, Compare):
             self.visit(node.test)
         elif (isinstance(node.test, Call)
               and not node.test.func.id.startswith('conditional')
               and not node.test.func.id == 'op'):
             self.visit(node.test)
             writeCommandByName("if", [2, 0])
         elif isinstance(
                 node.test,
                 Call) and (node.test.func.id.startswith('conditional')
                            or node.test.func.id == 'op'):
             cmdId = commandDBLookup[node.test.func.id]["id"]
             writeCommandByID(cmdId, node.test.args)
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif isinstance(node.test,
                     UnaryOp) and (isinstance(node.test.operand, Call) or
                                   isinstance(node.test.operand, Compare)):
         self.visit(node.test.operand)
         if (isinstance(node.test.operand, Call)):
             writeCommandByName("ifNot", [2, 0])
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     else:
         print("UNHANDLED IF", astor.dump(node))
Esempio n. 11
0
def rebuild_dbzfscript(sourceFilename, outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    if (os.path.isfile(sourceFilename + ".txt")):
        os.remove(sourceFilename + ".txt")
    f = open(sourceFilename + ".txt", "w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename, "wb")
    Rebuilder().visit(sourceAST)
    output.close()
Esempio n. 12
0
def rebuild_dbzfscript(sourceFilename,outFilename):
    global output
    sourceAST = astor.parsefile(sourceFilename)
    if(os.path.isfile(sourceFilename+".txt")):
        os.remove(sourceFilename+".txt")
    f = open(sourceFilename+".txt","w")
    f.write(astor.dump(sourceAST))
    f.close()
    output = open(outFilename,"wb")
    Rebuilder().visit(sourceAST)
    output.close()
Esempio n. 13
0
def testone(fname, f1=None, f2=None):
    try:
        ast1 = astor.parsefile(fname)
    except (SyntaxError, UnicodeDecodeError):
        print("IGNORED %s" % fname)
        return
    dump1 = astor.dump(ast1)
    reconstitute = '# -*- coding: utf-8 -*-\n' + astor.to_source(ast1)
    ast2 = ast.parse(reconstitute, fname)
    dump2 = astor.dump(ast2)
    ok = dump1 == dump2
    print('%-8s%s' % ('OK' if dump1 == dump2 else 'FAIL', fname))
    if not ok and f1 is not None and f2 is not None:
        f1.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
        f2.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
        f1.write(dump1)
        f2.write(dump2)
        f = open('bad.txt', 'w')
        f.write(reconstitute)
        f.close()
    return ok
Esempio n. 14
0
 def _tidy_prepends(assignments):
     """
     Ensure uniqueness of and sort prepend assignments.
     """
     str_reps = {assignment: astor.dump(assignment) for assignment in assignments}
     ass_str_rep_set = set()
     final_assignments = []
     for assignment, str_rep in str_reps.iteritems():
         if str_rep in ass_str_rep_set:
             continue
         final_assignments.append(assignment)
         ass_str_rep_set.add(str_rep)
     return sorted(final_assignments, key=str_reps.get)
    def visit_If(self,node):
        find = False
        try:
            find = node.body[0].value.func.id == "_gotolabel"
        except:
            pass

        if isinstance(node.test,Name) and find:
            writeCommandByID(18,[node.body[0].value.args[0]]+decodeVar(node.test))
        elif isinstance(node.test,Name):
            #This is if(SLOT) we need to find out slot index and write it as param of if
            writeCommandByName("if",decodeVar(node.test))
            self.visit_body(node.body)
            writeCommandByName("endIf",[])
            if len(node.orelse) > 0:
                writeCommandByName("else",[])
                self.visit_body(node.orelse)
                writeCommandByName("endElse",[])
        elif isinstance(node.test,UnaryOp) and isinstance(node.test.operand,Name):
            #This is if(SLOT) we need to find out slot index and write it as param of if
            writeCommandByName("ifNot",decodeVar(node.test.operand))
            self.visit_body(node.body)
            writeCommandByName("endIfNot",[])
            if len(node.orelse) > 0:
                writeCommandByName("else",[])
                self.visit_body(node.orelse)
                writeCommandByName("endElse",[])
        elif (isinstance(node.test,Call) or isinstance(node.test,Compare)) and find:
            self.visit(node.test)
            writeCommandByID(18,[node.body[0].value.args[0],2,0])
        elif (isinstance(node.test,Call) or isinstance(node.test,Compare)):
            self.visit(node.test)
            writeCommandByName("if",[2,0])
            self.visit_body(node.body)
            writeCommandByName("endIf",[])
            if len(node.orelse) > 0:
                writeCommandByName("else",[])
                self.visit_body(node.orelse)
                writeCommandByName("endElse",[])
        elif isinstance(node.test,UnaryOp) and (isinstance(node.test.operand,Call) or isinstance(node.test.operand,Compare)):
            self.visit(node.test.operand)
            writeCommandByName("ifNot",[2,0])
            self.visit_body(node.body)
            writeCommandByName("endIfNot",[])
            if len(node.orelse) > 0:
                writeCommandByName("else",[])
                self.visit_body(node.orelse)
                writeCommandByName("endElse",[])
        else:
            print "UNHANDLED IF",astor.dump(node)
Esempio n. 16
0
def CallAstor(func_name):

    function = getattr(test_func,func_name) 

    func_ast = astor.codetoast(function)

    # make sure to call this from the test directory
    # otherweise need to fix the path here
    filename = str(function.__name__ +'.java')
    f = open(filename, 'w')

    # activate to print AST again
    print(astor.dump(func_ast))
    print>>f, astor.to_source(func_ast, function.__name__)

    return filename
Esempio n. 17
0
def CallAstor(func_name):

    function = getattr(test_func, func_name)

    func_ast = astor.codetoast(function)

    # make sure to call this from the test directory
    # otherweise need to fix the path here
    filename = str(function.__name__ + '.java')
    f = open(filename, 'w')

    # activate to print AST again
    print(astor.dump(func_ast))
    print >> f, astor.to_source(func_ast, function.__name__)

    return filename
 def visit_Compare(self,node):
     params = []
     if isinstance(node.ops[0],Eq):
         params.append(9)
     elif isinstance(node.ops[0],Gt):
         params.append(10)
     elif isinstance(node.ops[0],Lt):
         params.append(11)
     elif isinstance(node.ops[0],GtE):
         params.append(12)
     elif isinstance(node.ops[0],LtE):
         params.append(13)
     else:
         print "UNIMPLEMENTED BINOP",astor.dump(node)
         raise Exception("Unknown Compare")
     writeCommandByName("op",params+decodeVar(node.left)+decodeVar(node.comparators[0]))
Esempio n. 19
0
 def visit_Assign(self,node):
     if isinstance(node.value,BinOp):
         params = []
         if isinstance(node.value.op,Add):
             params.append(0)
         elif isinstance(node.value.op,Sub):
             params.append(1)
         elif isinstance(node.value.op,Mult):
             params.append(2)
         elif isinstance(node.value.op,Div):
             params.append(3)
         else:
             print "UNIMPLEMENTED BINOP",astor.dump(node)
             raise Exception("Unknown Operation!")
         writeCommandByName("ModifyVar_",params+decodeVar(node.targets[0])+decodeVar(node.value.right))
     else:
         writeCommandByName("StoreValue",decodeVar(node.targets[0])+decodeVar(node.value))
Esempio n. 20
0
 def visit_If(self,node):
     if isinstance(node.test,Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("if",decodeVar(node.test))
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif isinstance(node.test,UnaryOp) and isinstance(node.test.operand,Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("ifNot",decodeVar(node.test.operand))
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif (isinstance(node.test,Call) or isinstance(node.test,Compare)):
         if isinstance(node.test,Compare):
             self.visit(node.test)
         elif(isinstance(node.test,Call) and not node.test.func.id.startswith('conditional') and not node.test.func.id == 'op'):
             self.visit(node.test)
             writeCommandByName("if",[2,0])
         elif isinstance(node.test,Call) and (node.test.func.id.startswith('conditional') or node.test.func.id == 'op'):
             cmdId = commandDBLookup[node.test.func.id]["id"]
             writeCommandByID(cmdId,node.test.args)
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif isinstance(node.test,UnaryOp) and (isinstance(node.test.operand,Call) or isinstance(node.test.operand,Compare)):
         self.visit(node.test.operand)
         if(isinstance(node.test.operand,Call)):
             writeCommandByName("ifNot",[2,0])
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     else:
         print "UNHANDLED IF",astor.dump(node)
Esempio n. 21
0
 def visit_Assign(self,node):
     if isinstance(node.value,BinOp):
         params = []
         if isinstance(node.value.op,Add):
             params.append(0)
         elif isinstance(node.value.op,Sub):
             params.append(1)
         elif isinstance(node.value.op,Mult):
             params.append(2)
         elif isinstance(node.value.op,Div):
             params.append(3)
         else:
             print "UNIMPLEMENTED BINOP",astor.dump(node)
             raise Exception("Unknown Operation!")
         writeCommandByName("ModifyVar_",params+decodeVar(node.targets[0])+decodeVar(node.value.right))
     else:
         writeCommandByName("StoreValue",decodeVar(node.targets[0])+decodeVar(node.value))
Esempio n. 22
0
def lpcompile(function):
    function_ast = ast.parse(inspect.getsource(function)).body[0]
    logging.debug("Python AST:\n{}\n".format(astor.dump(function_ast)))
    parser = frontend.LambdaPackParse()
    type_checker = frontend.LambdaPackTypeCheck()
    lp_ast = parser.visit(function_ast)
    logging.debug("IR AST:\n{}\n".format(astor.dump_tree(lp_ast)))
    lp_ast_type_checked = type_checker.visit(lp_ast)
    logging.debug("typed IR AST:\n{}\n".format(
        astor.dump_tree(lp_ast_type_checked)))

    def f(*args, **kwargs):
        backend_generator = frontend.BackendGenerate(*args, **kwargs)
        backend_generator.visit(lp_ast_type_checked)
        return backend_generator.remote_calls

    return f
Esempio n. 23
0
def get_sub_template(template, path):
    sub = template
    for el in path:
        # TODO: optimize it
        if el == 0:
            try:
                sub = sub[0]
            except IndexError:
                return lambda node, _: False
        elif el == 1:
            sub = sub[1:]
        elif isinstance(sub, ast.AST) and el in sub._fields:
            sub = getattr(sub, el)
        elif callable(sub):
            return lambda node, _: True
        else:
            raise Exception('Unknown path', path, 'in', astor.dump(sub))
    return sub
Esempio n. 24
0
 def visit_Compare(self,node):
     params = []
     if isinstance(node.ops[0],Eq):
         params.append(9)
     elif isinstance(node.ops[0],Gt):
         params.append(10)
     elif isinstance(node.ops[0],Lt):
         params.append(11)
     elif isinstance(node.ops[0],GtE):
         params.append(12)
     elif isinstance(node.ops[0],LtE):
         params.append(13)
     elif isinstance(node.ops[0],NotEq):
         params.append(16)
     else:
         print "UNIMPLEMENTED BINOP",astor.dump(node)
         raise Exception("Unknown Compare")
     writeCommandByName("op",params+decodeVar(node.left)+decodeVar(node.comparators[0]))
Esempio n. 25
0
 def visit_If(self, node):
     if isinstance(node.test, Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("if", decodeVar(node.test))
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif isinstance(node.test, UnaryOp) and isinstance(
             node.test.operand, Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("ifNot", decodeVar(node.test.operand))
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif (isinstance(node.test, Call) or isinstance(node.test, Compare)):
         self.visit(node.test)
         if (isinstance(node.test, Call)):
             writeCommandByName("if", [2, 0])
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     elif isinstance(node.test,
                     UnaryOp) and (isinstance(node.test.operand, Call) or
                                   isinstance(node.test.operand, Compare)):
         self.visit(node.test.operand)
         if (isinstance(node.test.operand, Call)):
             writeCommandByName("ifNot", [2, 0])
         self.visit_body(node.body)
         writeCommandByName("endIf", [])
         if len(node.orelse) > 0:
             writeCommandByName("else", [])
             self.visit_body(node.orelse)
             writeCommandByName("endElse", [])
     else:
         print "UNHANDLED IF", astor.dump(node)
Esempio n. 26
0
 def visit_If(self,node):
     if isinstance(node.test,Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("if",decodeVar(node.test))
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif isinstance(node.test,UnaryOp) and isinstance(node.test.operand,Name):
         #This is if(SLOT) we need to find out slot index and write it as param of if
         writeCommandByName("ifNot",decodeVar(node.test.operand))
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif (isinstance(node.test,Call) or isinstance(node.test,Compare)):
         self.visit(node.test)
         if(isinstance(node.test,Call)):
             writeCommandByName("if",[2,0])
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     elif isinstance(node.test,UnaryOp) and (isinstance(node.test.operand,Call) or isinstance(node.test.operand,Compare)):
         self.visit(node.test.operand)
         if(isinstance(node.test.operand,Call)):
             writeCommandByName("ifNot",[2,0])
         self.visit_body(node.body)
         writeCommandByName("endIf",[])
         if len(node.orelse) > 0:
             writeCommandByName("else",[])
             self.visit_body(node.orelse)
             writeCommandByName("endElse",[])
     else:
         print "UNHANDLED IF",astor.dump(node)
Esempio n. 27
0
#!/usr/bin/env python

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import astor

def TestMe(x, y, width=10, foo=None):
    from foo.bar.baz.murgatroyd import sally as bob

    a.b = c.d + x.y.z.a.b
    m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)

func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))

class ConsolidateAttributes(astor.TreeWalk):
    def post_Attribute(self):
        node = self.cur_node
        value = node.value
        value.id += '.' + node.attr
        self.replace(value)

ConsolidateAttributes(func_ast)

class FindNames(astor.TreeWalk):
    def init_Assign(self):
        self.assignments = []
        self.current_assign = None
    def pre_Assign(self):
Esempio n. 28
0
 def visit_body(self,nodebody):
     try:
         for childNode in nodebody:
             self.visit(childNode)
     except AttributeError as e:
         print e,astor.dump(childNode)
Esempio n. 29
0
def parse_kwarg_keys(source, keywords='kwargs', with_vals=False):
    r"""
    Parses the source code to find keys used by the `**kwargs` keywords
    dictionary variable. if `with_vals` is True, we also attempt to infer the
    default values.

    Args:
        source (str):

    Returns:
        list: kwarg_keys

    SeeAlso:
        argparse_funckw
        recursive_parse_kwargs
        parse_kwarg_keys
        parse_func_kwarg_keys
        get_func_kwargs

    Example:
        >>> import ubelt as ub
        >>> source = ub.codeblock(
        >>>    '''
        >>>    x = 'hidden_x'
        >>>    y = 3 # hidden val
        >>>    kwargs.get(x, y)
        >>>    kwargs.get('foo', None)
        >>>    kwargs.pop('bar', 3)
        >>>    kwargs.pop('str', '3fd')
        >>>    kwargs.pop('str', '3f"d')
        >>>    "kwargs.get('baz', None)"
        >>>    kwargs['foo2']
        >>>    #kwargs.get('biz', None)"
        >>>    kwargs['bloop']
        >>>    x = 'bop' in kwargs
        >>>    ''')
        >>> print('source = %s\n' % (source,))
        >>> with_vals = True
        >>> kwarg_items = parse_kwarg_keys(source, with_vals=with_vals)
        >>> result = ('kwarg_items = %s' % (ub.repr2(kwarg_items, nl=1),))
        >>> kwarg_keys = [item[0] for item in kwarg_items]
        >>> assert 'baz' not in kwarg_keys
        >>> assert 'foo' in kwarg_keys
        >>> assert 'bloop' in kwarg_keys
        >>> assert 'bop' not in kwarg_keys
        >>> print(result)
        kwarg_items = [
            ('foo', None),
            ('bar', 3),
            ('str', '3fd'),
            ('str', '3f"d'),
            ('foo2', None),
            ('bloop', None),
        ]
    """
    pt = ast.parse(source)
    kwargs_items = []
    debug = False
    target_kwargs_name = keywords

    if debug:
        import astor
        print('\nInput:')
        print('target_kwargs_name = %r' % (target_kwargs_name, ))
        print('\nSource:')
        print(source)
        print('\nParse:')
        print(astor.dump(pt))

    class KwargParseVisitor(ast.NodeVisitor):
        """
        TODO: understand ut.update_existing and dict update
        ie, know when kwargs is passed to these functions and
        then look assume the object that was updated is a dictionary
        and check wherever that is passed to kwargs as well.

        Other visit_<classname> values:
            http://greentreesnakes.readthedocs.io/en/latest/nodes.html
        """
        def __init__(self):
            super(KwargParseVisitor, self).__init__()
            self.const_lookup = {}
            self.first = True

        def visit_FunctionDef(self, node):
            if debug:
                print('VISIT FunctionDef node = %r' % (node, ))
                # print('node.args.kwarg = %r' % (node.args.kwarg,))
            if six.PY2:
                kwarg_name = node.args.kwarg
            else:
                if node.args.kwarg is None:
                    kwarg_name = None
                else:
                    kwarg_name = node.args.kwarg.arg

            # Record any constants defined in function definitions
            defaults_vals = node.args.defaults
            offset = len(node.args.args) - len(defaults_vals)
            default_keys = node.args.args[offset:]
            for kwname, kwval in zip(default_keys, defaults_vals):
                # try:
                if six.PY2:
                    if isinstance(kwval, ast.Name):
                        val = eval(kwval.id, {}, {})
                        self.const_lookup[kwname.id] = val
                else:
                    if isinstance(kwval, ast.NameConstant):
                        val = kwval.value
                        self.const_lookup[kwname.arg] = val
                # except Exception:
                #     pass

            if self.first or kwarg_name != target_kwargs_name:
                # target kwargs is still in scope
                ast.NodeVisitor.generic_visit(self, node)
                # always visit the first function
                self.first = False

        def visit_Subscript(self, node):
            if debug:
                print('VISIT SUBSCRIPT node = %r' % (node, ))
            if isinstance(node.value, ast.Name):
                if node.value.id == target_kwargs_name:
                    if isinstance(node.slice, ast.Index):
                        index = node.slice
                        key = index.value
                        if isinstance(key, ast.Str):
                            # item = (key.s, None)
                            item = (key.s, None)
                            kwargs_items.append(item)

        @staticmethod
        def _eval_bool_op(val):
            # Can we handle this more intelligently?
            val_value = None
            if isinstance(val.op, ast.Or):
                if any([
                        isinstance(x, ast.NameConstant) and x.value is True
                        for x in val.values
                ]):
                    val_value = True
            elif isinstance(val.op, ast.And):
                if any([
                        isinstance(x, ast.NameConstant) and x.value is False
                        for x in val.values
                ]):
                    val_value = False
            return val_value

        def visit_Call(self, node):
            if debug:
                print('VISIT Call node = %r' % (node, ))
                # print(ut.repr4(node.__dict__,))
            if isinstance(node.func, ast.Attribute):
                try:
                    objname = node.func.value.id
                except AttributeError:
                    return
                methodname = node.func.attr
                # funcname = objname + '.' + methodname
                if objname == target_kwargs_name and methodname in {
                        'get', 'pop'
                }:
                    args = node.args
                    if len(args) == 2:
                        key, val = args
                        if isinstance(key, ast.Name):
                            # TODO lookup constant
                            pass
                        elif isinstance(key, ast.Str):
                            key_value = key.s
                            val_value = None  # ut.NoParam
                            if isinstance(val, ast.Str):
                                val_value = val.s
                            elif isinstance(val, ast.Num):
                                val_value = val.n
                            elif isinstance(val, ast.Name):
                                if val.id == 'None':
                                    val_value = None
                                else:
                                    val_value = self.const_lookup.get(
                                        val.id, None)
                                    # val_value = 'TODO lookup const'
                                    # TODO: lookup constants?
                                    pass
                            elif six.PY3:
                                if isinstance(val, ast.NameConstant):
                                    val_value = val.value
                                elif isinstance(val, ast.Call):
                                    val_value = None
                                elif isinstance(val, ast.BoolOp):
                                    val_value = self._eval_bool_op(val)
                                elif isinstance(val, ast.Dict):
                                    if len(val.keys) == 0:
                                        val_value = {}
                                    else:
                                        val_value = {}
                                    # val_value = callable
                                else:
                                    print(
                                        'Warning: util_inspect doent know how to parse {}'
                                        .format(repr(val)))
                            item = (key_value, val_value)
                            kwargs_items.append(item)
            ast.NodeVisitor.generic_visit(self, node)

    try:
        KwargParseVisitor().visit(pt)
    except Exception:
        raise
        pass
    if with_vals:
        return kwargs_items
    else:
        return [item[0] for item in kwargs_items]
Esempio n. 30
0
        self.continue_(node)
        node.js = '(' + node.test.js + ' ? ' + str(node.body.js) + ':' + str(
            node.orelse.js) + ')'

    def visit_Compare(self, node):
        self.continue_(node)
        node.js = '(' + node.left.js + '==' + (node.comparators[0].js) + ')'

    def visit_BinOp(self, stmt_binop):

        opmap = {}
        opmap[str((ast.Add))] = '+'
        opmap[str((ast.Sub))] = '-'
        opmap[str((ast.Div))] = '/'
        self.continue_(stmt_binop)
        stmt_binop.js = str(stmt_binop.left.js) + opmap[str(type(
            stmt_binop.op))] + str(stmt_binop.right.js)


class ReactParser(FirstParser):
    pass


x = FirstParser()
if len(sys.argv) > 2:
    if sys.argv[2] == 'ast':
        a = ast.parse(open((sys.argv[1])).read())
        print(astor.dump(a))
else:
    x.parse(open((sys.argv[1])).read())
Esempio n. 31
0
        node.js += "}"

    def visit_IfExp(self, node):
        self.continue_(node)
        node.js = "(" + node.test.js + " ? " + str(node.body.js) + ":" + str(node.orelse.js) + ")"

    def visit_Compare(self, node):
        self.continue_(node)
        node.js = "(" + node.left.js + "==" + (node.comparators[0].js) + ")"

    def visit_BinOp(self, stmt_binop):

        opmap = {}
        opmap[str((ast.Add))] = "+"
        opmap[str((ast.Sub))] = "-"
        self.continue_(stmt_binop)
        stmt_binop.js = str(stmt_binop.left.js) + opmap[str(type(stmt_binop.op))] + str(stmt_binop.right.js)


class ReactParser(FirstParser):
    pass


x = FirstParser()
if len(sys.argv) > 2:
    if sys.argv[2] == "ast":
        a = ast.parse(open((sys.argv[1])).read())
        print(astor.dump(a))
else:
    x.parse(open((sys.argv[1])).read())
Esempio n. 32
0
 def visit_body(self,nodebody):
     try:
         for childNode in nodebody:
             self.visit(childNode)
     except AttributeError as e:
         print e,astor.dump(childNode)
Esempio n. 33
0
import astor

f = "test.py"

node = astor.parsefile(f)
print node
print astor.dump(node)
print astor.to_source(node)
Esempio n. 34
0
 def convert_decls(self, exprs, prefix=None):
     is_toplevel = prefix is None
     ret = []
     for e in exprs:
         name = None
         if hasattr(e, "name"):
             name = pyast.Name(
                 e.name, pyast.Load) if not prefix else pyast.Attribute(
                     prefix, e.name, pyast.Load)
         with location(astor.to_source(name) if name else "",
                       loc=self.convert_location(e)):
             if isinstance(e, pyast.FunctionDef):
                 args = list(self.convert_arguments(e.args))
                 function_annotations = self.convert_annotation(
                     e.decorator_list)
                 f = Function(
                     Name(name),
                     Argument(Name(RET_ARGUMENT_NAME),
                              self.convert_type(e.returns)),
                     args,
                     **function_annotations,
                     location=self.convert_location(e),
                 )
                 yield f
             elif isinstance(e, pyast.ClassDef):
                 for ann in e.decorator_list:
                     f = ann.func
                     if hasattr(f, "id") and f.id == "nw":
                         name = ann.args[0]
                 yield from self.convert_decls(e.body, name)
             elif isinstance(e, pyast.Assign):
                 parse_requires(
                     is_toplevel,
                     "Global configuration can only be set at the top level."
                 )
                 t = e.targets[0]
                 parse_requires(
                     isinstance(t, pyast.Name),
                     "Global assignments can only create new constants.")
                 self.__dict__[t.id] = pyast.literal_eval(e.value)
             elif isinstance(e, pyast.Expr):
                 e = e.value
                 if isinstance(e, pyast.Subscript):
                     parse_requires(
                         e.value.attr == "a",
                         "Subscripting may only be used on '.a' for applying annotations"
                     )
                     annotations = self.convert_annotation(e.slice.value)
                     self.type_annotations[Name(
                         e.value.value)] = annotations
                 elif isinstance(e, pyast.BinOp):
                     annotations = self.convert_annotation(e.right)
                     self.type_annotations[Name(e.left)] = annotations
                 else:
                     parse_requires(False,
                                    "Unknown expression: " + astor.dump(e))
             elif isinstance(e, pyast.ImportFrom) or isinstance(
                     e, pyast.Import):
                 parse_requires(isinstance(e, pyast.Import),
                                "Import from not supported.")
                 self.imports.update(
                     {Name(n.name): n.asname
                      for n in e.names})
             else:
                 parse_expects(False, astor.dump(e))
Esempio n. 35
0
def main():
    d = {"1": 1, "2": {b"aaa": [0, 1, 2, 3]}}
    node = build_ast_data(d)
    print(node)
    print(astor.dump(node))
Esempio n. 36
0
def find_funcs_called_with_kwargs(sourcecode, target_kwargs_name='kwargs'):
    r"""
    Finds functions that are called with the keyword `kwargs` variable

    Example:
        >>> # ENABLE_DOCTEST
        >>> sourcecode = ub.codeblock(
                '''
                x, y = list(zip(*ub.ichunks(data, 2)))
                somecall(arg1, arg2, arg3=4, **kwargs)
                import sys
                sys.badcall(**kwargs)
                def foo():
                    bar(**kwargs)
                    ub.holymoly(**kwargs)
                    baz()
                    def biz(**kwargs):
                        foo2(**kwargs)
                ''')
        >>> child_funcnamess = find_funcs_called_with_kwargs(sourcecode)
        >>> print('child_funcnamess = %r' % (child_funcnamess,))
        >>> assert 'foo2' not in child_funcnamess, 'foo2 should not be found'
        >>> assert 'bar' in child_funcnamess, 'bar should be found'
    """
    import ast
    sourcecode = 'from __future__ import print_function\n' + sourcecode
    pt = ast.parse(sourcecode)
    child_funcnamess = []
    debug = False

    if debug:
        print('\nInput:')
        print('target_kwargs_name = %r' % (target_kwargs_name, ))
        print('\nSource:')
        print(sourcecode)
        import astor
        print('\nParse:')
        print(astor.dump(pt))

    class KwargParseVisitor(ast.NodeVisitor):
        """
        TODO: understand dict update ie, know when kwargs is passed to these
        functions and then look assume the object that was updated is a
        dictionary and check wherever that is passed to kwargs as well.
        """
        def visit_FunctionDef(self, node):
            if debug:
                print('\nVISIT FunctionDef node = %r' % (node, ))
                print('node.args.kwarg = %r' % (node.args.kwarg, ))
            if six.PY2:
                kwarg_name = node.args.kwarg
            else:
                if node.args.kwarg is None:
                    kwarg_name = None
                else:
                    kwarg_name = node.args.kwarg.arg
            if kwarg_name != target_kwargs_name:
                # target kwargs is still in scope
                ast.NodeVisitor.generic_visit(self, node)

        def visit_Call(self, node):
            if debug:
                print('\nVISIT Call node = %r' % (node, ))
            if isinstance(node.func, ast.Attribute):
                try:
                    funcname = node.func.value.id + '.' + node.func.attr
                except AttributeError:
                    funcname = None
            elif isinstance(node.func, ast.Name):
                funcname = node.func.id
            else:
                raise NotImplementedError(
                    'do not know how to parse: node.func = %r' % (node.func, ))
            if six.PY2:
                kwargs = node.kwargs
                kwargs_name = None if kwargs is None else kwargs.id
                if funcname is not None and kwargs_name == target_kwargs_name:
                    child_funcnamess.append(funcname)
                if debug:
                    print('funcname = %r' % (funcname, ))
                    print('kwargs_name = %r' % (kwargs_name, ))
            else:
                if node.keywords:
                    for kwargs in node.keywords:
                        if kwargs.arg is None:
                            if hasattr(kwargs.value, 'id'):
                                kwargs_name = kwargs.value.id
                                if funcname is not None and kwargs_name == target_kwargs_name:
                                    child_funcnamess.append(funcname)
                                if debug:
                                    print('funcname = %r' % (funcname, ))
                                    print('kwargs_name = %r' % (kwargs_name, ))
            ast.NodeVisitor.generic_visit(self, node)

    try:
        KwargParseVisitor().visit(pt)
    except Exception:
        raise
    return child_funcnamess