-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyc_parser.py
More file actions
76 lines (59 loc) · 2.04 KB
/
Copy pathpyc_parser.py
File metadata and controls
76 lines (59 loc) · 2.04 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
# Copyright 2013 anthony cantor
# This file is part of pyc.
#
# pyc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyc. If not, see <http://www.gnu.org/licenses/>.
import ast
import StringIO
import sys
import pyc_vis
import pyc_lineage
from pyc_astvisitor import ASTVisitor
from pyc_astvisitor import ASTTxformer
class PrintASTVisitor(ASTVisitor):
def __init__(self, io):
ASTVisitor.__init__(self)
self.io = io
self.pass_fields = True
def format(self, depth, field, val):
fmt = "%s%s"
fmt += (":" if field != '' else '')
fmt += "%s"
return fmt % (" "*depth, field, val)
def default_ast(self, node, *args, **kwargs):
val = "%s():%d" % (node.__class__.__name__, pyc_lineage.src_lineno(node) )
print >>self.io, self.format(self.depth, kwargs.get("field", ""), val)
def default_non_ast(self, obj, *args, **kwargs):
print >>self.io, self.format(self.depth, kwargs.get("field", ""), repr(obj) )
class SrcLineFixer(ASTTxformer):
def default(self, node, lineno):
new_lineno = node.lineno if hasattr(node, 'lineno') else lineno
result = ASTTxformer.default(self, node, new_lineno)
result.lineno = new_lineno
return result
def fix_source_lines(node):
v = SrcLineFixer()
return pyc_vis.walk(v, node, 0)
def parse(src):
result = ast.parse(src)
return fix_source_lines(result)
def tree_to_str(astree):
s = StringIO.StringIO()
v = PrintASTVisitor(s)
pyc_vis.walk(v, astree)
return v.io.getvalue()
def print_astree(astree):
v = PrintASTVisitor(sys.stdout)
pyc_vis.walk(v, astree)
def dump(astree):
return ast.dump(astree)