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