-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_parser.py
More file actions
141 lines (117 loc) · 4.25 KB
/
code_parser.py
File metadata and controls
141 lines (117 loc) · 4.25 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
import ast
import os
#import astor
def expand_attribute(attribute):
parent_name = []
while attribute.__class__ is ast.Attribute:
parent_name.append(attribute.attr)
attribute = attribute.value
name = '.'.join(reversed(parent_name))
attribute_id = ''
if attribute.__class__ is ast.Name:
attribute_id = attribute.id
elif attribute.__class__ is ast.Call:
if attribute.func.__class__ is ast.Attribute:
attribute_id = '%s.%s()' % (
expand_attribute(attribute.func.value),
attribute.func.attr)
else:
attribute_id = '%s()' % attribute.func.id
name = attribute_id if name == '' else ("%s.%s" % (attribute_id, name))
return name
class TranslatorVisitor(ast.NodeVisitor):
testing = True
def parse(self, astmodule):
self.symbols = []
self.imports = []
self.visit(astmodule)
a = 234
def visit_Import(self, node):
for item in node.names:
if item.asname:
self.imports.append(item.asname)
else:
self.imports.append(item.name)
return node
def visit_FromImport(self, node):
for item in node.names:
if item.asname:
self.imports.append(item.asname)
else:
self.imports.append("%s.%s" % (item.module, item.name))
return node
def visit_ClassDef(self, node):
if node.name not in self.symbols:
self.symbols.append(node.name)
for item in node.body:
self.visit(item)
return node
def visit_FunctionDef(self, node):
if node.name not in self.symbols:
self.symbols.append(node.name)
for item in node.body:
self.visit(item)
return node
def visit_Name(self, node):
if node.id not in self.symbols:
self.symbols.append(node.id)
return node
def visit_Attribute(self, node):
expanded = expand_attribute(node)
ignore = any(expanded.startswith(i) for i in self.imports)
if not ignore and node.attr not in self.symbols:
self.symbols.append(node.attr)
return node
#class ParserTranslator(ast.NodeTransformer):
#def visit_ClassDef(self, node):
#node.name = get_translation(node.name)
#for item in node.body:
#self.visit(item)
#return node
#def visit_FunctionDef(self, node):
#node.name = get_translation(node.name)
#return node
#def visit_Name(self, node):
#node.id = get_translation(node.id)
#return node
#def visit_Attribute(self, node):
#node.attr = get_translation(node.attr)
#return node
def create_po(path):
visitor = TranslatorVisitor()
for root, dirs, files in os.walk(path, followlinks=True):
for filen in files:
if os.path.splitext(filen.lower())[-1] != '.py':
continue
filename = os.path.join(root, filen)
with open(filename, 'r') as f:
source = f.read()
module = ast.parse(source)
visitor.parse(module)
print visitor.symbols
#name = "%s.po" % os.path.basename(path)
#filename = os.path.join(path, name)
#with open(filename, 'w') as fp:
#json.dump(visitor.code_names, fp, 2)
#return filename
#def translate_source(path, output):
#parser_translator = ParserTranslator()
#for root, dirs, files in os.walk(path, followlinks=True):
#for filen in files:
#if os.path.splitext(filen.lower())[-1] != '.py':
#continue
#filename = os.path.join(root, filen)
#module = None
#with open(filename, 'r') as f:
#source = f.read()
#module = ast.parse(source)
#relative = os.path.relpath(filename, path)
#fullpath = os.path.join(output, relative)
#folder = os.path.dirname(fullpath)
#if not os.path.exists(folder):
#os.makedirs(folder)
#with open(fullpath, 'w') as f2:
#result = parser_translator.visit(module)
#code = astor.to_source(result)
#f2.write(code)