-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate-python-autocomplete-file.py
221 lines (138 loc) · 8.64 KB
/
generate-python-autocomplete-file.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import glob, os, re #re allows for regular expression patterns
# SETUP
# ---------------------------
# change current directory to the libkis folder in the krita source code
# this is where all the python API files are at that we will read
#os.chdir("C:\\dev\\krita\\libs\\libkis") # possible Windows OS dir. need to have two "\" symbols
os.chdir("/home/scottpetrovic/krita/src/libs/libkis")
# YOU SHOULDN'T HAVE TO TOUCH ANYTHING BELOW HERE
#----------------------------------------------
# create new file to save to. "w+" means write and create a file. saves in directory specified above
exportFile = open("PyKrita.py", "w+")
# a bit of a readme for what this does
exportFile.write("# Auto-generated file that reads from H files in the libkis folder \n" +
"# The purpose is to use as auto-complete in Python IDEs \n" )
# grab all h files and iterate through them
for file in glob.glob("*.h"):
currentFile = open(file)
exportFile.write("\n# auto-generated from: " + currentFile.name + " \n" )
allFileLines = currentFile.readlines() # readlines creates a list of the lines
#exportFile.write(allFileLines[1])
# find all classes that need to be exported
classPattern = re.compile("KRITALIBKIS_EXPORT")
lineWithClassFile = -1 # used later to also help with finding class comments
# in a .h file, this will grab what class things are a part of
for i, line in enumerate(allFileLines):
for match in re.finditer(classPattern, line):
lineWithClassFile = i
# first order of business is to get the class name from the file
# currently only one class per file, so this will probably
# break if there are more
bracesPrecursor = ""
# sometimes braces are cuddled and have stuff in it
if len(allFileLines[i]) <= 2:
lineWithClassFile = lineWithClassFile - 1
bracesPrecursor = allFileLines[lineWithClassFile]
className = bracesPrecursor.split(' ')[2]
# start getting the format ready for the class
exportFile.write("class " + className + ":\n")
# now let's find the comments that exist for the class and append it to the botom
#after we find the lines that have the class comments, we can concatenate the lines and clean them up
# we are now checking the next 3 lines above to see if there are comments for the class
# if there aren't don't try to grab the comments later
classCommentsEnd = -1
if "*/" in allFileLines[lineWithClassFile]:
classCommentsEnd = lineWithClassFile;
if "*/" in allFileLines[lineWithClassFile - 1]:
classCommentsEnd = lineWithClassFile - 1;
if "*/" in allFileLines[lineWithClassFile - 2]:
classCommentsEnd = lineWithClassFile - 2;
# if we see some comments for the class, so try to read them...
if classCommentsEnd != -1:
classCommentsStartIndex = -1
for a in range(classCommentsEnd, 0, -1 ) : #go in decreasing order
if "/*" in allFileLines[a]:
classCommentsStartIndex = a
break
if classCommentsStartIndex != -1:
classCommentsStart = classCommentsStartIndex + 1
classCommentsOutput = "" # concatenate everything in here for the comments
for x in range (classCommentsStart, classCommentsEnd ):
classCommentsOutput += allFileLines[x].strip() + "\n "
classCommentsOutput = classCommentsOutput.replace("*", "")
else:
classCommentsOutput = "Trouble Parsing class comments"
else:
classCommentsOutput = "Class not documented"
exportFile.write(" \"\"\"" + classCommentsOutput + " \"\"\"" + "\n\n")
# 2nd thing to do.....find the functions for the class
# find all the functions and output them.
# need to add some spaces for proper indenting
# this just looks for things that have () and
#we save the line of the previous function to get exactly range between signatures of the methods
previousFunctionLineNumber = -1
fnPattern = re.compile("\(.*\)")
for j, line in enumerate(allFileLines):
line = line.strip() # strip white beginning and trailing white space
for match in re.finditer(fnPattern, line):
if line.strip()[0][0] != "*": # this means it is part of a comments
#these aren't functions we can call
if "Q_" not in line and "~" not in line and "operator" not in line:
# if we made it this far that means we are a valid function
# now we need to figure out how to parse this and format it for python
isVirtual = False
returnType = "void"
isExplicit = False
functionLineNumber = j
functionList = line.split("(")[0]
functionList = functionList.replace("*", "").replace("const", "").replace(" >", ">")
if "virtual" in functionList:
isVirtual = True
functionList = functionList.split("virtual")[1]
if "explicit" in functionList:
isExplicit = True
functionList = functionList.split("explicit")[1]
functionList = functionList.strip() # extra spaces at the beginning need to be removed
#first word is usually the return type
if " " in functionList:
returnType = functionList.split(' ')[0]
functionList = functionList.split(' ')[1]
# save off the parameters elsewhere as we have to parse each differently
# we need to clean it up a bit since it is loose and doesn't need variable names and types. we will just keep the types
paramsList = "(" + line.split("(")[1]
paramsList = paramsList.replace("const", "").replace("&", "").replace("*", "").replace(";", "") # remove const stuff
paramsList = paramsList.replace("(", "").replace(")", "").strip()
# clean up parameters with multiple
if "," in paramsList:
multipleParamsList = []
# break it apart and clear everything after the first word
for p in paramsList.split(","):
multipleParamsList.append(p.strip().split(" ")[0])
paramsList = ",".join(multipleParamsList )
elif paramsList != "":
paramsList = paramsList.strip().split(" ")[0]
#Only one parameter. remove everything after the first word
exportFile.write(" def " + functionList + "(" + paramsList + "):" "\n")
# now let's figure out what comments we have. we figured out the return type above. but we need to scrape the h file for comments
#functionLineNumber
functionCommentsEnd = functionLineNumber - 1;
functionCommentsStartIndex = previousFunctionLineNumber
for b in range(functionCommentsEnd, functionCommentsStartIndex+1, -1 ) : #go in decreasing order
if "/*" in allFileLines[b]:
functionCommentsStartIndex = b
break
if functionCommentsStartIndex != -1:
classCommentsStart = classCommentsStartIndex +1 # first line just has "/*", so we can skip that
functionCommentsOutput = "" # concatenate everything in here for the comments
for x in range (functionCommentsStartIndex, functionCommentsEnd ):
functionCommentsOutput += allFileLines[x].strip()
functionCommentsOutput = functionCommentsOutput.replace("*", "").replace("/ ", "")
else:
functionCommentsOutput = "Missing function documentation"
# finally export the final file
exportFile.write(" \"\"\"Return Type: " + returnType + "\n " + functionCommentsOutput + "\"\"\" \n\n" )
# file is done. add some spacing for readability
exportFile.write("\n")
# close the file that we are done with it
currentFile.close()
exportFile.close()