Repository navigation
Expand file tree
/
Copy pathpy2icode.py
More file actions
103 lines (81 loc) · 3.09 KB
/
Copy pathpy2icode.py
File metadata and controls
103 lines (81 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
"""
py2icode.py produces an ICODE representation of a Python file.
Usage: py2icode.py <in_file> ?<out_file>
<in_file> should be a python file for processing. The full path is not
needed. <out_file> is the name of the file which should store the
ICODE translation of <in_file>. If no name is given STDOUT is used.
"""
__date__ = '2010-05-16'
DEBUG = True
import ast, sys
from pyicode import *
__all__ = ['Ast2IcodeVisitor']
class Ast2IcodeVisitor(ast.NodeTransformer):
"""
AST Visitor which creates an ICODE translation of the AST, stored
in its icode attribute.
"""
def __init__(self):
super(AST2ICODEVisitor, self)
self.icode = '' # BAH
def vist_Function(self, node):
pass
# def default(self, node):
# """Gives debug info in place of unwritten visit methods."""
# self.generic_visit(node)
# return node
# self.icode += "\nDEBUG: START NODE\n"
# self.icode += '\t__repr__:' + node.__repr__() + '\n'
# self.icode += '\t__dict__:' + str(node.__dict__) + '\n'
# self.icode += "DEBUG: END NODE\n"
if __name__ == '__main__':
if DEBUG: print ( 'Debugging: ON. Script arguments:' + str ( sys.argv ) )
# Determine the input file.
if len(sys.argv) == 1:
print ( 'You must specify a Python file for processing.' )
sys.exit(1)
else:
i_file = sys.argv[1]
# Determine the output file. Use sys.stdout if none specified.
if len(sys.argv) > 2:
o_file = sys.argv[2]
o_fd = open(sys.argv[2], 'w')
if DEBUG: print ( 'Output file:' + str ( sys.argv[2] ) )
else:
if DEBUG: print ( 'Using STDOUT for output.' )
o_file = ''
o_fd = sys.stdout
# This is the important stuff.
infile = open(i_file).read()
tree = compile(infile, '<string>', 'exec')#, ast.PyCF_ONLY_AST)
outtree = AST2ICODEVisitor().visit(tree)
o_fd.write(outtree.xml())
if not o_file == '':
o_fd.close()
# ...end of important stuff.
####################################################################
# SCRATCH SPACE #
####################################################################
#
# self.icode += "\tSTART CHILD NODES\n"
# for i in node.getChildNodes():
# self.icode += '\t\t__repr__:' + i.__repr__() + '\n'
# self.icode += '\t\t__dict__:' + str(i.__dict__) + '\n'
# self.dispatch(i)
# self.icode += "\tEND CHILD NODES\n"
############# SCRATCH
# def visit_Const(self, node):
# self.generic_visit(node)
# return IcodeConst(node.value, lineno=node.lineno)
# def visit_Assign(self, node):
# print 'ASSIGN'
# for key,val in node.__dict__.items():
# print key, val
# print 'END'
# self.generic_visit(node)
# return IcodeAssign(node.nodes, node.expr, lineno=node.lineno)
# def visit_Import(self, node):
# self.generic_visit(node)
# return IcodeImport(node.names)