コード例 #1
0
ファイル: compile.py プロジェクト: BenjaminUJun/slick
 def _represent_code(self, policy_code_string):
     """Walk the AST and represent the code as a kripke structure. or DAG.
     This function should be used to conver"""
     tree = ast.parse(policy_code_string)
     print astpp.dump(tree)
     FuncCallLister().visit(tree)
     print "AAAAAAAAAAAAAAAA"
コード例 #2
0
def getplugin(name=''):
#	''' read the file filenam and create the parsetree '''

	# test data
	if False:
		content='''
		
__Comment__ = 'My macro is a super macro and can be used whenever other macros fail '
__Web__ = "http://forum.freecadweb.org/viewtopic.php?f=8&t=11302"
__Wiki__ = "http://www.freecadweb.org/wiki/index.php?title=Macro_FreeCAD_to_Kerkythea"
__Icon__  = "Part_Common.svg"
__Help__ = "This is the help text of this macro"
__Author__ = "Freek Ad"
__Version__ = 0.1
__Status__ = 'alpha'
__Requires__ = ''

'''
	else:
		content=open(name).read()
	tree = ast.parse(content)
	try:
		# astpp found here
		# http://alexleone.blogspot.de/2010/01/python-ast-pretty-printer.html
		import astpp
		print astpp.dump(tree)
	except:
		pass
	return tree
コード例 #3
0
def getplugin(name=''):
    #	''' read the file filenam and create the parsetree '''

    # test data
    if False:
        content = '''
		
__Comment__ = 'My macro is a super macro and can be used whenever other macros fail '
__Web__ = "http://forum.freecadweb.org/viewtopic.php?f=8&t=11302"
__Wiki__ = "http://www.freecadweb.org/wiki/index.php?title=Macro_FreeCAD_to_Kerkythea"
__Icon__  = "Part_Common.svg"
__Help__ = "This is the help text of this macro"
__Author__ = "Freek Ad"
__Version__ = 0.1
__Status__ = 'alpha'
__Requires__ = ''

'''
    else:
        content = open(name).read()
    tree = ast.parse(content)
    try:
        # astpp found here
        # http://alexleone.blogspot.de/2010/01/python-ast-pretty-printer.html
        import astpp
        print astpp.dump(tree)
    except:
        pass
    return tree
コード例 #4
0
def main(argv):
	parser = argparse.ArgumentParser(prog=argv[0], description=__doc__)
	parser.add_argument('func', help="The function to inspect.")
	args = parser.parse_args(argv[1:])
	
	# Get function.
	func = dev_lib
	for seg in args.func.split('.'):
		func = getattr(func, seg)
	
	print "Function"
	print "--------"
	print repr(func)
		
	# Get function source.
	func_src = inspect.getsourcelines(func)[0]
	pdt.dedent_func_lines(func_src)
	func_src = ''.join(func_src)
	func_src = func_src.replace("\t", "  ")
	
	print 
	print "Source"
	print "------"
	print func_src
	
	# Parse function source to AST.
	mod_ast = ast.parse(func_src)
	
	print "AST"
	print "---"
	print astpp.dump(mod_ast, include_attributes=True, indent=" ")
コード例 #5
0
 def test_filter_process(self, cls_gen):
     self.setup_transformer()
     for simple_cls in cls_gen.generate(1000, 10, 10):
         original_source = simple_cls.get_source_code()
         ast_node = simple_cls.get_ast_node()
         source_code = self.transformer.transform(ast_node)
         new_ast_node = ast.parse(source_code).body[0]
         self.log("Original Source", original_source)
         self.log("New Source", source_code)
         self.log("Original Dump", astpp.dump(ast_node))
         self.log("New Source", astpp.dump(new_ast_node))
         assert ast.dump(ast_node) == ast.dump(new_ast_node)
コード例 #6
0
ファイル: test_scopes.py プロジェクト: Suor/flaws
def _debug_scope(func):
    tree = ast.Module(body=get_body_ast(func))
    fill_scopes(tree)
    # Only shown when test fails
    import astpp
    print(astpp.dump(tree))
    print(tree.scope)
    return tree
コード例 #7
0
ファイル: test_scopes.py プロジェクト: afcarl/flaws
def _debug_scope(func):
    tree = ast.Module(body=get_body_ast(func))
    fill_scopes(tree)
    # Only shown when test fails
    import astpp
    print(astpp.dump(tree))
    print(tree.scope)
    return tree
コード例 #8
0
def main(argv=None):
    """
    Parse source file, find satisfiability, validity.
    """
    if argv is None:
        argv = sys.argv

    Z3_INFO = False
    AST_INFO = False

    # Parse command line options
    try:
        opts, args = getopt.getopt(argv[1:], "d z a",
                                   ["doc", "z3info", "astinfo"])
    except getopt.error as msg:
        print(msg)
        print(
            "Usage: python prover.py [-adz] [--astinfo] [--doc] [--z3info] source_file"
        )
        return 2

    # Process options
    for o, a in opts:
        if o in ("-d", "--doc"):
            print(__doc__)
            sys.exit(0)
        if o in ("-z", "--z3info"):
            Z3_INFO = True
        if o in ("-a", "--astinfo"):
            AST_INFO = True

    arg_index = len(argv) - 1

    with open(argv[arg_index], "r") as myfile:
        data = myfile.read()

    # Convert special syntax into assertions calls
    data = re.sub('#@\s*requires\s*(.*)\n', r'requires \1\n', data, 0, re.M)
    data = re.sub('#@\s*assures\s*(.*)\n', r'assures \1\n', data, 0, re.M)

    # Create Abstract Syntax Tree
    tree = ast.parse(data)

    # visit AST and create z3 definitions of each function in the source
    function_visitor = Z3FunctionVisitor()
    function_visitor.visit(tree)

    # Visit AST and aggregate Z3 function calls
    source_visitor = Z3Visitor(Z3_INFO)
    source_visitor.visit(tree)

    if AST_INFO:
        print("------- Abstract Syntax Tree -------")
        print(astpp.dump(tree))

    return 0
コード例 #9
0
ファイル: transforms.py プロジェクト: acleone/sageserver
def transform_ast(exec_msg, source_ast, source_lines, globals_):
    """
    Called after the code is parsed into an AST.
    
    :param exec_msg: a :class:`msg.Exec` instance.
    :param source_ast: the ast of the source
    :param source_lines: ``source.splitlines()``
    :returns: the transformed ast.
    """
    dh = exec_msg['displayhook']
    if dh == 'LAST':
        source_ast = displayhook_last(source_ast)
    elif dh == 'ALL':
        source_ast = DisplayhookAll().visit(source_ast)
    ah = exec_msg['assignhook']
    if ah == 'ALL':
        source_ast = AssignhookAll(source_lines).visit(source_ast)
    if exec_msg['print_ast']:
        print astpp.dump(source_ast)
    return source_ast
コード例 #10
0
ファイル: driver.py プロジェクト: kennywbin/500lines
def compile_toplevel(module_name, filename, source):
    t = ast.parse(source)
    try:
        import astpp
    except ImportError:
        astpp = ast
    report(astpp.dump(t))
    their_globals = compiler.make_globals(module_name)
    f = compiler.byte_compile(module_name, filename, t, their_globals)
    diss(f.__code__)
    return f
コード例 #11
0
ファイル: driver.py プロジェクト: 0xsKu/500lines
def compile_toplevel(module_name, filename, source):
    t = ast.parse(source)
    try:
        import astpp
    except ImportError:
        astpp = ast
    report(astpp.dump(t))
    their_globals = compiler.make_globals(module_name)
    f = compiler.byte_compile(module_name, filename, t, their_globals)
    diss(f.__code__)
    return f
コード例 #12
0
ファイル: prog_ast.py プロジェクト: fredokun/MrPython
def EBinOp(node):
    #print(astpp.dump(node))
    binop_type_name = node.op.__class__.__name__
    #print("binop type=",binop_type_name)
    if binop_type_name in BINOP_CLASSES:
        left = parse_expression(node.left)
        right = parse_expression(node.right)
        wrap_node = BINOP_CLASSES[binop_type_name](node, left, right)
        return wrap_node
    else:
        print(astpp.dump(node))
        return UnsupportedNode(node)
コード例 #13
0
def EBinOp(node):
    #print(astpp.dump(node))
    binop_type_name = node.op.__class__.__name__
    #print("binop type=",binop_type_name)
    if binop_type_name in BINOP_CLASSES:
        left = parse_expression(node.left)
        right = parse_expression(node.right)
        wrap_node = BINOP_CLASSES[binop_type_name](node, left, right)
        return wrap_node
    else:
        print(astpp.dump(node))
        return UnsupportedNode(node)
コード例 #14
0
ファイル: parsiflage.py プロジェクト: darius/tailbiter
def main(argv):
    filename = argv[1]
    if 0:
        with open(filename, 'rb') as f:
            tokens = list(tokenize(f.readline))
        print_tokens(tokens)
        demo_parse(tokens)
    else:
        with open(filename, 'rb') as f:
            t = parse(f)
        import astpp
        print(astpp.dump(t, include_attributes=True))
コード例 #15
0
ファイル: parsiflage.py プロジェクト: darius/tailbiter
def demo_parse(tokens):
    far = [0]
    for i, vals in top.run(tokens, far, (0, ())):
        print(i, tokens[i:])
        print('vals', vals)
        try:
            import astpp
        except ImportError:
            continue
        for tree in vals:
            print(tree)
            print(astpp.dump(tree, include_attributes=True))
    print('far', far[0])
コード例 #16
0
 def test_filter_process(self, list_of_ast_class_nodes):
     weight_ssm = 0.7
     weight_cdm = 0.3
     metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm),
         (CallBasedDependenceBetweenMethods(), weight_cdm)]
     cmmm = CrisMethodByMethodMatrix(metrics)
     ccctf = CrisCOMVariableThresholdFilter()
     for python_file, node, class_nodes in list_of_ast_class_nodes:
         for class_node in class_nodes:
             ast_dump = astpp.dump(class_node)
             wrapper = AstClassWrapper(class_node)
             matrix = cmmm.build_method_matrix(wrapper)
             original = deepcopy(matrix.matrix)
             min_coupling = -1
             if len(original) > 0:
                 min_coupling = CrisCOMVariableThresholdFilter.\
                     calculate_median(original)
             filtered_matrix = ccctf.filter_process(matrix)
             assert filtered_matrix != None
             assert not self.has_value_less_than(filtered_matrix.matrix,
                 min_coupling)
コード例 #17
0
ファイル: prog_ast.py プロジェクト: fredokun/MrPython

def print_ast_fields(node):
    for field, val in ast.iter_fields(node):
        print(field)


def python_ast_from_file(filename):
    with tokenize.open(filename) as f:
        modtxt = f.read()
        modast = ast.parse(modtxt, mode="exec")
        return modast

def python_show_tokenized(filename):
    modtxt = ""
    with tokenize.open(filename) as f:
        modtxt = f.read()

    return modtxt

if __name__ == "__main__":
    import sys
    prog1 = Program()
    if len(sys.argv) > 1:
        filename = sys.argv[1]
    else:
        filename = "../../examples/aire.py"
        
    prog1.build_from_file(filename)
    print(astpp.dump(prog1.ast))
コード例 #18
0
def run():
    tree = ast.parse(code)
    print(dump(tree))
コード例 #19
0
def print_ast_fields(node):
    for field, val in ast.iter_fields(node):
        print(field)


def python_ast_from_file(filename):
    with tokenize.open(filename) as f:
        modtxt = f.read()
        modast = ast.parse(modtxt, mode="exec")
        return modast


def python_show_tokenized(filename):
    modtxt = ""
    with tokenize.open(filename) as f:
        modtxt = f.read()

    return modtxt


if __name__ == "__main__":
    import sys
    prog1 = Program()
    if len(sys.argv) > 1:
        filename = sys.argv[1]
    else:
        filename = "../../examples/aire.py"

    prog1.build_from_file(filename)
    print(astpp.dump(prog1.ast))
コード例 #20
0
def d(node):
    import astpp
    import codegen
    print(astpp.dump(node))
    print()
    print(codegen.to_source(node))
コード例 #21
0
import ast
import meta
import astpp

code = open('sample_code.py', 'r').read()
tree = ast.parse(code)

#Get source
source = meta.dump_python_source(tree)

#Get formatted AST repr
formatted_ast = astpp.dump(tree, include_attributes=True)


class FuncLister(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        print node.name
        self.generic_visit(node)


print 'Functions:'
FuncLister().visit(tree)
コード例 #22
0
def dump(tree):
    return astpp.dump(tree)
コード例 #23
0
from ast import *
from astpp import dump
from meta import dump_python_source


class RewriteOps(NodeTransformer):
     _names = {Add: '__add__', Sub: '__sub__', Mult: '__mul__', Div: '__truediv__', FloorDiv: '__floordiv__', Mod: '__mod__', Pow: '__pow__'}

     def visit_BinOp(self, node):
         name = self._names.get(node.op.__class__, None)
         if name is not None:
            return copy_location(Call(func=Attribute(value=node.left, attr=name, ctx=Load()),args=[node.right], keywords=[], starargs=[], kwargs=[]),node)
         else:
            self.generic_visit(node)

source = '"Hello " + "World!"'
print(source,'\n')

node = parse(source)
print(dump(node),'\n')

node = RewriteOps().visit(node)
print(dump(node))

source2 = dump_python_source(node)
print(source2)

print(eval(source))
print(eval(source2))
コード例 #24
0
    po[node.row * 3 + 1][node.col * blockWidth:(node.col + 1) * blockWidth] = list(map(ord, ("{0:^" + str(blockWidth) + "}").format("|")))
    po[node.row * 3 + 2][node.col * blockWidth:(node.col + 1) * blockWidth] = list(map(ord, ("{0:^" + str(blockWidth) + "}").format("V")))
    if isinstance(node.child, dict):
        e1 = nodeToText(po, node.child['Yes'])
        e2 = nodeToText(po, node.child['No'])
        return max(e1, e2)
    else:
        if node.child is None:
            return (node.row + 1) * 3
        else:
            return nodeToText(po, node.child)


tree = ast.parse(open('test.py', 'r').read())
print(tree)
print(astpp.dump(tree))

visitor = FlowchartMakingVisitor()
visitor.visit(tree)
start = visitor.start

followNodePath(start)  # mark each node with it's position in the printout

# print layout
# each column: 10 chars for up arrow space, then 50 chars padded for node space
# each node: 1 line for node itself, 2 lines of arrow (TODO: maybe special case conjunction nodes?)

printout = np.zeros((1000, 1000), dtype=np.uint8)

lastLine = nodeToText(printout, start)
コード例 #25
0
ファイル: ast1.py プロジェクト: bossjones/etc-python
def run():
  tree = ast.parse(code)
  print(dump(tree))
コード例 #26
0
ファイル: compiler.py プロジェクト: dodobas/knights-templater
def kompile(src, debug=False):
    '''
    Creates a new class based on the supplied template, and returnsit.

    class Template(object):
        def __call__(self, context):
            return ''.join(str(x) for x in self._root(context))

        def _root(self, context):
            yield ''
            yield ...
            yield from self.head(context)

    Blocks create new methods, and add a 'yield from self.{block}(context)' to
    the current function

    '''

    parser = Parser(src)
    parser.load_library('knights.tags')
    parser.load_library('knights.helpers')

    # Define the __call__ method
    # return ''.join(str(x) for x in self._root(context))
    func = ast.FunctionDef(
        name='__call__',
        args=ast.arguments(
            args=[
                ast.arg(arg='self', annotation=None),
                ast.arg(arg='context', annotation=None),
            ],
            vararg=None,
            kwonlyargs=[],
            kwarg=None,
            defaults=[],
            kw_defaults=[],
        ),
        body=[
            ast.Return(
                value=ast.Call(
                    func=ast.Attribute(
                        value=ast.Str(s=''),
                        attr='join',
                        ctx=ast.Load()
                    ),
                    args=[
                        ast.GeneratorExp(
                            elt=ast.Call(
                                func=ast.Name(id='str', ctx=ast.Load()),
                                args=[
                                    ast.Name(id='x', ctx=ast.Load()),
                                ],
                                keywords=[], starargs=None, kwargs=None
                            ),
                            generators=[
                                ast.comprehension(
                                    target=ast.Name(id='x', ctx=ast.Store()),
                                    iter=ast.Call(
                                        func=ast.Attribute(
                                            value=ast.Name(id='self', ctx=ast.Load()),
                                            attr='_root',
                                            ctx=ast.Load()
                                        ),
                                        args=[
                                            ast.Name(id='context', ctx=ast.Load()),
                                        ], keywords=[], starargs=None, kwargs=None
                                    ),
                                    ifs=[]
                                ),
                            ]
                        ),
                    ],
                    keywords=[], starargs=None, kwargs=None
                )
            ),
        ],
        decorator_list=[],
    )

    parser.methods.append(func)
    parser.build_method('_root')

    if parser.parent:
        # Remove _root from the method list
        parser.methods = [
            method for method in parser.methods if method.name != '_root'
        ]
    klass = parser.build_class()

    # Wrap it in a module
    inst = ast.Module(body=[klass])

    ast.fix_missing_locations(inst)

    if debug:
        import astpp
        print(astpp.dump(inst))
    # Compile code to create class
    code = compile(inst, filename='<compiler>', mode='exec', optimize=2)

    # Execute it and return the instance
    g = {
        'helpers': parser.helpers,
        'parent': parser.parent,
        'ContextScope': ContextScope,
    }
    eval(code, g)

    return g['Template']