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