forked from ItsCubeTime/fake-pykrita
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-python-autocomplete-file.py
More file actions
executable file
·479 lines (359 loc) · 18.9 KB
/
generate-python-autocomplete-file.py
File metadata and controls
executable file
·479 lines (359 loc) · 18.9 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python
import glob, os, re #re allows for regular expression patterns
generatorVersion = 0.1
# 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")
import tempfile
from importlib.machinery import SourceFileLoader
from tkinter.filedialog import askdirectory
#from tkinter.messagebox import askyesno
kritaHomeDir = ""
savedConfig = os.path.join(tempfile.gettempdir(), 'kritaHomeDirSave.py')
if os.path.isfile(savedConfig):
# isToLoadSavedConfig = askyesno("found previous config", f"Krita source path config was found in {savedConfig}, would you like to use it?")
# if isToLoadSavedConfig:
m = SourceFileLoader("myModule", savedConfig).load_module()
kritaHomeDir = getattr(m, "kritaHomeDir")
del m
if kritaHomeDir.__len__() == 0:
kritaHomeDir = askdirectory(title="choose the directory of Krita source code:")
if kritaHomeDir.__len__() == 0:
#"Cancel" was clicked
quit()
if os.path.isdir(kritaHomeDir):
print(f"kritaHomeDir = {kritaHomeDir}")
else:
print(f"kritaHomeDir = {kritaHomeDir}, not a vaild path")
quit(1)
#kritaHomeDir is an apprioporite path where your Krita source code is.
# Note that you need to have the actual source code & not a prebuilt version of Krita. THIS DIRETORY SHOULD HAVE THE CMakeLists.txt FILE directly in it
kritaLibLibKisPath = f"{kritaHomeDir}/libs/libkis"
if os.path.isdir(kritaLibLibKisPath):
exportSaveConfigFile = open(savedConfig, "w")
exportSaveConfigFile.write(f"kritaHomeDir = \"{kritaHomeDir}\"")
exportSaveConfigFile.close()
else:
print(f"You selected krita source code path is: {kritaHomeDir}\nbut we cannot find libs/libkis directory under this path.")
quit(1)
cwd = os.getcwd()
moduleDestinationPath = fr"{cwd}/output".replace('\\', '/') # Where to store the output module
import shutil
try:
shutil.rmtree(moduleDestinationPath)
except:
pass
# raise ""
# files = glob.glob(packageDestinationPath + '/*')
# for f in files:
# os.remove(f)
os.chdir(kritaLibLibKisPath)
# 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
os.makedirs(f"{moduleDestinationPath}", exist_ok=True)
#exportFile = open(f"{moduleDestinationPath}/__init__.py", "w+")
exportFile = open(f"{moduleDestinationPath}/krita.pyi", "w+")
# a bit of a readme for what this does
exportFile.write(f"""
# Auto-generated file that reads from H files in the libkis folder
# The purpose is to use as auto-complete in Python IDEs
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
""")
# sort header files according to dependence
headerFilelist = glob.glob("*.h")
d = { key: [] for key in headerFilelist}
for file in headerFilelist:
currentFile = open(file)
allFileLines = currentFile.readlines()
currentFile.close()
for line in allFileLines:
if line.__contains__("#include"):
includeFile = line.replace("#include", "").replace("\"", "").replace("\n", "").strip()
if headerFilelist.__contains__(includeFile):
d[file].append(includeFile)
includeFile = ""
headerFilelist2 = []
while d.__len__() > 0:
beforeLen = d.__len__()
for key1 in d.keys():
if d[key1].__len__() == 0:
headerFilelist2.append(key1)
for key2 in d.keys():
while d[key2].__contains__(key1):
d[key2].remove(key1)
del d[key1]
break
if d.__len__() == beforeLen:
print("cycle exists!")
print(f"headerFilelist = ${headerFilelist}")
print(f"headerFilelist2 = ${headerFilelist2}")
quit()
# grab all h files and iterate through them
#for file in glob.glob("*.h"):
for file in headerFilelist2:
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])
# remove all empty lines
tempSaveLines = list()
for line in allFileLines:
if line.strip().__len__() == 0:
continue
else:
tempSaveLines.append(line)
allFileLines = tempSaveLines
del tempSaveLines
# if function has too many arguments and some of arguments was splited to the next line
tempSaveLines = list()
for i in range(0, allFileLines.__len__() - 1):
if allFileLines[i].strip()[-1] == ',' and allFileLines[i].count('(') > allFileLines[0].count(')'):
allFileLines[i+1] = allFileLines[i].replace("\n", "").strip() + allFileLines[i+1].strip()
else:
tempSaveLines.append(allFileLines[i])
allFileLines = tempSaveLines
del tempSaveLines
# 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]
if className[-1] == ":":
className = className[:-1]
if className[-1] == "\n":
className = className[:-1]
# 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...
indentspace0 = ''
indentspace4 = ' '
indentspace8 = ' '
indentspace = ''
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"
indentspace = indentspace4
else:
classCommentsOutput = "Class not documented"
indentspace = indentspace0
exportFile.write(f"{indentspace}\"\"\"<{currentFile.name}>\n")
exportFile.write(f"{indentspace}" + classCommentsOutput + f"{indentspace}\"\"\"" + "\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
isInBlockComment = False
fnPattern = re.compile(r"\(.*\)")
for j, line in enumerate(allFileLines):
if line.__contains__('*/'):
isInBlockComment = False
if isInBlockComment:
continue
if line.__contains__('/*'):
isInBlockComment = True
line = line[:(line.find('/*')-1)]
line = line.strip() # strip white beginning and trailing white space
def removeSpacesWithinLimiters(input: str, limitBegin: str, limitEnd: str) -> str:
output: str = ""
isWithinLimiters = False
i = -1
for char in input:
i += 1
if char == limitBegin:
isWithinLimiters = True
elif char == limitEnd:
isWithinLimiters = False
if isWithinLimiters and char == ' ':
continue
else:
output += char
return output
def replaceCppKeyWord(input: str, cppKey: str, pythonKey: str) -> str:
output = ' ' + input + ' '
#match white-character and ,*<>()[]
output = re.sub(f'([\\s,\\*<>\\(\\)\\[\\]]){cppKey}([\\s,\\*<>\\(\\)\\[\\]])', f'\\1{pythonKey}\\2', output)
return output
line = replaceCppKeyWord(line, 'void', 'None')
line = replaceCppKeyWord(line, 'QString', 'str')
line = replaceCppKeyWord(line, 'QList', 'list')
line = replaceCppKeyWord(line, 'qreal', 'float')
line = replaceCppKeyWord(line, 'double', 'float')
line = removeSpacesWithinLimiters(line, '<', '>')
line = line.replace('<', '[').replace('>', ']')
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"
returnType = "None"
isExplicit = False
isStatic = False
functionLineNumber = j
functionList = line.split("(")[0]
functionList = functionList.replace("*", "").replace("const", "")
if "virtual" in functionList:
isVirtual = True
functionList = functionList.split("virtual")[1]
if "explicit" in functionList:
isExplicit = True
functionList = functionList.split("explicit")[1]
if functionList.strip()[0:7] == "static ":
isStatic = True
functionList = functionList.split("static")[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]
if functionList.__len__() < 1:
continue
if functionList[0] == '=':
continue
# 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(";", "").replace("override", "") # remove const stuff
paramsList = paramsList.replace("(", "").replace(")", "").strip()
# clean up parameters with multiple
class ParamTypeAndName:
name: str = ""
type: str = ""
listOfParamTypesAndNames: list[ParamTypeAndName] = []
longestParamName = 0
if True:
# if "," in paramsList:
multipleParamsList = []
if paramsList.__contains__(','):
paramsListSplit = paramsList.split(",")
else:
paramsListSplit = [paramsList]
# break it apart and clear everything after the first word
for p in paramsListSplit:
print(p)
try:
splittedStr = p.strip().split(" ", 1)
print(f"splittedStr {splittedStr}")
if splittedStr[1].__contains__('='):
parameterName = splittedStr[1][:((splittedStr[1].find('=')))].strip()
else:
parameterName = splittedStr[1]
parameterType = splittedStr[0]
print(f"splittedStr: {splittedStr}")
#multipleParamsList.append(f"{parameterName}")
multipleParamsList.append(f"{parameterName}: {parameterType}")
paramTypeAndName = ParamTypeAndName()
paramTypeAndName.name = parameterName
paramTypeAndName.type = parameterType
listOfParamTypesAndNames.append(paramTypeAndName)
longestParamName = max(parameterName.__len__(), longestParamName)
# multipleParamsList.append(f"{parameterName}: {splittedStr[0]}") # @todo Make sure all types are declared in Python, currently we cant type hint
# due to the actual types never being declared.
except Exception as exception:
print(f"Paramsplitting error -> {exception} <- for '{p}' in {paramsList} in '{line}' in {file}")
# raise exception
paramsList = ",".join(multipleParamsList )
elif paramsList != "":
paramsList = paramsList.strip().split(" ")[0]
#Only one parameter. remove everything after the first word
# if paramsList.__len__() < 2:
# continue
if isStatic:
exportFile.write(f"{indentspace}@staticmethod\n")
if not isStatic:
if paramsList.__len__() == 0:
paramsList = "self"
else:
paramsList = "self, " + paramsList
# exportFile.write(f"{indentspace}def " + functionList + "(" + paramsList + "):\n")
exportFile.write(f"{indentspace}def " + functionList + "(" + paramsList + ")->" + returnType + ":\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"
longestParamName = max(longestParamName, 'return'.__len__())
def formatParamForDocString(paramName: str, paramType: str) -> str:
formatParamForDocStringSpacing = ""
i9 = -1
while i9 < longestParamName - paramName.__len__():
i9 += 1
formatParamForDocStringSpacing += " "
return f"\n{indentspace8}{paramName}: {formatParamForDocStringSpacing}{paramType}"
# finally export the final file
# listOfParamTypesAndNames.reverse()
parameterPartOfComment = ""
for param in listOfParamTypesAndNames:
parameterPartOfComment = f"{parameterPartOfComment}{formatParamForDocString(param.name, param.type)}"
newLine = '\n'
functionCommentsOutput = f"{newLine}{indentspace8}{functionCommentsOutput}{newLine}{indentspace8}{parameterPartOfComment}{formatParamForDocString('return', returnType)}{newLine}"
exportFile.write(f"{indentspace8}\"\"\"<{currentFile.name}>\n")
exportFile.write(f"{indentspace8}" + functionCommentsOutput + f"{indentspace8}\"\"\"\n\n" )
# exportFile.write(f"{indentspace8}return {returnType}\n\n" )
# file is done. add some spacing for readability
exportFile.write("\n")
# close the file that we are done with it
currentFile.close()
# exportFileAsStr = exportFile.__str__()
# exportFileAsListOfStringsWhereEachStringRepresentsALine: list[str] = exportFileAsStr.split('\n')
# exportFileAsStrFINAL = ""
# newLine = '\n'
# for line in exportFileAsListOfStringsWhereEachStringRepresentsALine:
# if line.__contains__('#include'):
# continue
# exportFileAsStrFINAL += f"{line}{newLine}"
exportFile.close()