-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.py
executable file
·77 lines (69 loc) · 2.65 KB
/
python.py
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
#!/usr/bin/env python
import sys
import ast
import os
class AstVisitor(ast.NodeVisitor):
def visit_ClassDef(self, node):
print 'signature||' + node.name + '|' + node.name + \
'|class|public|' + str(node.lineno) + '|' + \
str(node.col_offset + len('class ') + 1) + '||typesearch'
for item in node.body:
if item.name.startswith('__') == False:
print 'signature|' + node.name + '|' + node.name + '.' + item.name + '|' + item.name + \
'|classmethod|public|' + str(item.lineno) + '|' + \
str(item.col_offset + len('def ') + 1) + '|'
def visit_FunctionDef(self, node):
if node.name.startswith('__') == False:
print 'signature||' + node.name + '|' + node.name + \
'|method|public|' + str(node.lineno) + '|' + \
str(node.col_offset + len('def ') + 1) + '||typesearch'
else:
print 'signature||' + node.name + '|' + node.name + \
'|method|private|' + str(node.lineno) + '|' + \
str(node.col_offset + len('def ') + 1) + '|'
def __handleDirectory(path):
items = os.listdir(path);
for dr in items:
dir = os.path.join(path, dr)
if (os.path.isdir(dir)):
__handleDirectory(dir)
for file in items:
filename = os.path.join(path, file)
if (os.path.isfile(filename)):
extension = os.path.splitext(filename)[1]
if extension == '.py':
__handleFile(filename)
def __handleFile(file):
try:
print 'file|' + file + '|filesearch'
node = ast.parse(open(file, 'r').read())
node = ast.fix_missing_locations(node)
v = AstVisitor()
v.visit(node)
except Exception,e:
print 'error|Failed to parse ' + file
print 'error|' + str(e)
def main(argv):
if len(argv) > 1:
if argv[1] == 'initialize':
return
if argv[1] == 'get-command-definitions':
print ''
return
if argv[1] == 'crawl-file-types':
print '.py'
return
if argv[1] == 'crawl-source':
if os.path.isfile(argv[2]):
with open(argv[2]) as file:
lines = file.readlines()
for ln in lines:
line = ln.replace(os.linesep, '')
if os.path.isdir(line):
__handleDirectory(line)
else:
__handleFile(line)
else:
__handleDirectory(argv[2])
if __name__ == "__main__":
main(sys.argv)