|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the builtin API generator. |
|
8 """ |
|
9 |
|
10 |
|
11 class APIGenerator: |
|
12 """ |
|
13 Class implementing the builtin documentation generator. |
|
14 """ |
|
15 def __init__(self, module): |
|
16 """ |
|
17 Constructor |
|
18 |
|
19 @param module The information of the parsed Python file. |
|
20 """ |
|
21 self.module = module |
|
22 |
|
23 def genAPI(self, newStyle, basePackage, includePrivate): |
|
24 """ |
|
25 Public method to generate the API information. |
|
26 |
|
27 @param newStyle flag indicating the api generation for QScintilla 1.7 |
|
28 and newer (boolean) (ignored) |
|
29 @param basePackage name of the base package (string) |
|
30 @param includePrivate flag indicating to include |
|
31 private methods/functions (boolean) |
|
32 @return API information (list of strings) |
|
33 """ |
|
34 self.includePrivate = includePrivate |
|
35 modulePath = self.module.name.split('.') |
|
36 if modulePath[-1] == '__init__': |
|
37 del modulePath[-1] |
|
38 if basePackage: |
|
39 modulePath[0] = basePackage |
|
40 self.moduleName = "{0}.".format('.'.join(modulePath)) |
|
41 self.api = [] |
|
42 self.__addGlobalsAPI() |
|
43 self.__addClassesAPI() |
|
44 self.__addFunctionsAPI() |
|
45 return self.api |
|
46 |
|
47 def genBases(self, includePrivate): |
|
48 """ |
|
49 Public method to generate the base classes information. |
|
50 |
|
51 @param includePrivate flag indicating to include private classes |
|
52 (boolean) |
|
53 @return base classes information (dictionary of list of strings) |
|
54 """ |
|
55 bases = {} |
|
56 self.includePrivate = includePrivate |
|
57 classNames = sorted(self.module.classes.keys()) |
|
58 for className in classNames: |
|
59 if ( |
|
60 not self.__isPrivate(self.module.classes[className]) and |
|
61 className not in bases |
|
62 ): |
|
63 bases[className] = [ |
|
64 b for b in self.module.classes[className].super |
|
65 if b != "object"] |
|
66 return bases |
|
67 |
|
68 def __isPrivate(self, obj): |
|
69 """ |
|
70 Private method to check, if an object is considered private. |
|
71 |
|
72 @param obj reference to the object to be checked |
|
73 @return flag indicating, that object is considered private (boolean) |
|
74 """ |
|
75 private = obj.isPrivate() and not self.includePrivate |
|
76 return private |
|
77 |
|
78 def __addGlobalsAPI(self): |
|
79 """ |
|
80 Private method to generate the api section for global variables. |
|
81 """ |
|
82 from QScintilla.Editor import Editor |
|
83 |
|
84 moduleNameStr = "{0}".format(self.moduleName) |
|
85 |
|
86 for globalName in sorted(self.module.globals.keys()): |
|
87 if not self.__isPrivate(self.module.globals[globalName]): |
|
88 if self.module.globals[globalName].isPublic(): |
|
89 iconId = Editor.AttributeID |
|
90 elif self.module.globals[globalName].isProtected(): |
|
91 iconId = Editor.AttributeProtectedID |
|
92 else: |
|
93 iconId = Editor.AttributePrivateID |
|
94 self.api.append("{0}{1}?{2:d}".format( |
|
95 moduleNameStr, globalName, iconId)) |
|
96 |
|
97 def __addClassesAPI(self): |
|
98 """ |
|
99 Private method to generate the api section for classes. |
|
100 """ |
|
101 classNames = sorted(self.module.classes.keys()) |
|
102 for className in classNames: |
|
103 if not self.__isPrivate(self.module.classes[className]): |
|
104 self.__addClassVariablesAPI(className) |
|
105 self.__addMethodsAPI(className) |
|
106 |
|
107 def __addMethodsAPI(self, className): |
|
108 """ |
|
109 Private method to generate the api section for class methods. |
|
110 |
|
111 @param className name of the class containing the method (string) |
|
112 """ |
|
113 from QScintilla.Editor import Editor |
|
114 |
|
115 _class = self.module.classes[className] |
|
116 methods = sorted(_class.methods.keys()) |
|
117 if '__init__' in methods: |
|
118 methods.remove('__init__') |
|
119 if _class.isPublic(): |
|
120 iconId = Editor.ClassID |
|
121 elif _class.isProtected(): |
|
122 iconId = Editor.ClassProtectedID |
|
123 else: |
|
124 iconId = Editor.ClassPrivateID |
|
125 self.api.append( |
|
126 '{0}{1}?{2:d}({3})'.format( |
|
127 self.moduleName, _class.name, iconId, |
|
128 ', '.join(_class.methods['__init__'].parameters[1:]))) |
|
129 |
|
130 classNameStr = "{0}{1}.".format(self.moduleName, className) |
|
131 for method in methods: |
|
132 if not self.__isPrivate(_class.methods[method]): |
|
133 if _class.methods[method].isPublic(): |
|
134 iconId = Editor.MethodID |
|
135 elif _class.methods[method].isProtected(): |
|
136 iconId = Editor.MethodProtectedID |
|
137 else: |
|
138 iconId = Editor.MethodPrivateID |
|
139 self.api.append( |
|
140 '{0}{1}?{2:d}({3})'.format( |
|
141 classNameStr, method, iconId, |
|
142 ', '.join(_class.methods[method].parameters[1:]))) |
|
143 |
|
144 def __addClassVariablesAPI(self, className): |
|
145 """ |
|
146 Private method to generate class api section for class variables. |
|
147 |
|
148 @param className name of the class containing the class variables |
|
149 (string) |
|
150 """ |
|
151 from QScintilla.Editor import Editor |
|
152 |
|
153 _class = self.module.classes[className] |
|
154 classNameStr = "{0}{1}.".format(self.moduleName, className) |
|
155 for variable in sorted(_class.globals.keys()): |
|
156 if not self.__isPrivate(_class.globals[variable]): |
|
157 if _class.globals[variable].isPublic(): |
|
158 iconId = Editor.AttributeID |
|
159 elif _class.globals[variable].isProtected(): |
|
160 iconId = Editor.AttributeProtectedID |
|
161 else: |
|
162 iconId = Editor.AttributePrivateID |
|
163 self.api.append('{0}{1}?{2:d}'.format( |
|
164 classNameStr, variable, iconId)) |
|
165 |
|
166 def __addFunctionsAPI(self): |
|
167 """ |
|
168 Private method to generate the api section for functions. |
|
169 """ |
|
170 from QScintilla.Editor import Editor |
|
171 |
|
172 funcNames = sorted(self.module.functions.keys()) |
|
173 for funcName in funcNames: |
|
174 if not self.__isPrivate(self.module.functions[funcName]): |
|
175 if self.module.functions[funcName].isPublic(): |
|
176 iconId = Editor.MethodID |
|
177 elif self.module.functions[funcName].isProtected(): |
|
178 iconId = Editor.MethodProtectedID |
|
179 else: |
|
180 iconId = Editor.MethodPrivateID |
|
181 self.api.append( |
|
182 '{0}{1}?{2:d}({3})'.format( |
|
183 self.moduleName, self.module.functions[funcName].name, |
|
184 iconId, |
|
185 ', '.join(self.module.functions[funcName].parameters))) |