src/eric7/DocumentationTools/APIGenerator.py

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

eric ide

mercurial