-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_format.py
More file actions
185 lines (158 loc) · 6.79 KB
/
Copy pathcpp_format.py
File metadata and controls
185 lines (158 loc) · 6.79 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from tabulate import tabulate
from itertools import zip_longest as it
class Cpp:
# initialising the cpp class
def __init__(self):
pass
cpp_class = []
cpp_methods = []
cpp_variables = []
# getting a .cpp name from the directory name
def fCpp(self, filename):
x = filename.rfind("/")
y = filename.rfind('.cpp')
return filename[(x + 1):(y + 4)]
# read and get CPP classes
def cppClass(self, filename):
with open(filename) as cc:
for line in cc:
if 'class' in line:
line = line.strip()
if '//' in line:
line = line.rpartition('//')[0]
elif '/*' in line:
line = line.rpartition('/*')[0]
if ':' in line:
line = line.replace(':', 'inherits')
if '{' in line:
self.cpp_class.append(line.rpartition('{')[0])
else:
self.cpp_class.append(line)
if 'int main()' in line:
line = line.strip()
if '//' in line:
line = line.rpartition('//')[0]
elif '/*' in line:
line = line.rpartition('/*')[0]
if '{' in line:
self.cpp_class.append(line.rpartition('{')[0])
else:
self.cpp_class.append(line)
cc.close()
# read and get CPP methods
def cppMeth(self, filename):
with open(filename) as cm:
for line in cm:
if ('void' in line or 'private' in line or 'public' in line) and ('(' in line and ')' in line and ';' not in line):
line = line.strip()
if '//' in line:
line = line.rpartition('//')[0]
elif '/*' in line:
line = line.rpartition('/*')[0]
if '{' in line:
self.cpp_methods.append(line.rpartition('{')[0])
else:
self.cpp_methods.append(line)
cm.close()
# reads and gets CPP variables
def cppVar(self, filename):
with open(filename) as cv:
temp1 = []
for line in cv:
if 'for' in line:
None
elif 'while' in line:
None
elif 'if' in line:
None
elif ('int ' in line and ';' in line) and ('Print' not in line and 'print' not in line):
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'char ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif ('string ' in line or 'String ' in line) and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'double ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'float ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'byte ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'short ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif 'long ' in line and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
elif ('boolean ' in line or 'bool' in line) and ';' in line:
line = line.strip()
if '=' in line:
line = line.rpartition('=')[0]
temp1.append(line)
else:
line = line.rpartition(';')[0]
temp1.append(line)
cv.close()
var1 = set(temp1)
for x in var1:
self.cpp_variables.append(x)
#tabular print statement
def tcpp(self, filename):
b = open("Extractor.txt", "a") # open a file for appending
print('\n' + self.fCpp(filename))
b.write(self.fCpp(filename) + '\n') # print name of the file into a text file
self.cppClass(filename) # function to seperate class
self.cppVar(filename)
self.cppMeth(filename)
pdtabulate = lambda df: tabulate(df, headers=(('Class', 'Methods', 'Variables')), tablefmt='psql')
print(pdtabulate(it(self.cpp_class, self.cpp_methods, self.cpp_variables)))
b.write(pdtabulate(it(self.cpp_class, self.cpp_methods, self.cpp_variables)) + '\n\n')
self.cpp_class.clear()
self.cpp_methods.clear()
self.cpp_variables.clear()
b.close()