eric6/Utilities/ClassBrowsers/ClbrBaseClasses.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2005 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing base classes used by the various class browsers.
8 """
9
10
11 from __future__ import unicode_literals
12
13
14 class _ClbrBase(object):
15 """
16 Class implementing the base of all class browser objects.
17 """
18 def __init__(self, module, name, file, lineno):
19 """
20 Constructor
21
22 @param module name of the module containing this object
23 @type str
24 @param name name of this object
25 @type str
26 @param file filename containing this object
27 @type str
28 @param lineno linenumber of the object definition
29 @type int
30 """
31 self.module = module
32 self.name = name
33 self.file = file
34 self.lineno = lineno
35 self.endlineno = -1 # marker for "not set"
36
37 def setEndLine(self, endLineNo):
38 """
39 Public method to set the ending line number.
40
41 @param endLineNo number of the last line
42 @type int
43 """
44 self.endlineno = endLineNo
45
46
47 class ClbrBase(_ClbrBase):
48 """
49 Class implementing the base of all complex class browser objects.
50 """
51 def __init__(self, module, name, file, lineno):
52 """
53 Constructor
54
55 @param module name of the module containing this object
56 @type str
57 @param name name of this object
58 @type str
59 @param file filename containing this object
60 @type str
61 @param lineno linenumber of the object definition
62 @type int
63 """
64 _ClbrBase.__init__(self, module, name, file, lineno)
65 self.methods = {}
66 self.attributes = {}
67 self.classes = {}
68 self.globals = {}
69
70 def _addmethod(self, name, function):
71 """
72 Protected method to add information about a method.
73
74 @param name name of method to be added
75 @type str
76 @param function Function object to be added
77 @type Function
78 """
79 self.methods[name] = function
80
81 def _getmethod(self, name):
82 """
83 Protected method to retrieve a method by name.
84
85 @param name name of the method (string)
86 @type str
87 @return the named method
88 @rtype Function or None
89 """
90 try:
91 return self.methods[name]
92 except KeyError:
93 return None
94
95 def _addglobal(self, attr):
96 """
97 Protected method to add information about global variables.
98
99 @param attr Attribute object to be added
100 @type Attribute
101 """
102 if attr.name not in self.globals:
103 self.globals[attr.name] = attr
104 else:
105 self.globals[attr.name].addAssignment(attr.lineno)
106
107 def _getglobal(self, name):
108 """
109 Protected method to retrieve a global variable by name.
110
111 @param name name of the global variable
112 @type str
113 @return the named global variable
114 @rtype Attribute or None
115 """
116 try:
117 return self.globals[name]
118 except KeyError:
119 return None
120
121 def _addattribute(self, attr):
122 """
123 Protected method to add information about attributes.
124
125 @param attr Attribute object to be added
126 @type Attribute
127 """
128 if attr.name not in self.attributes:
129 self.attributes[attr.name] = attr
130 else:
131 self.attributes[attr.name].addAssignment(attr.lineno)
132
133 def _getattribute(self, name):
134 """
135 Protected method to retrieve an attribute by name.
136
137 @param name name of the attribute
138 @type str
139 @return the named attribute
140 @rtype Attribute or None
141 """
142 try:
143 return self.attributes[name]
144 except KeyError:
145 return None
146
147 def _addclass(self, name, _class):
148 """
149 Protected method method to add a nested class to this class.
150
151 @param name name of the class
152 @type str
153 @param _class Class object to be added
154 @type Class
155 """
156 self.classes[name] = _class
157
158
159 class ClbrVisibilityMixinBase(object):
160 """
161 Class implementing the base class of all visibility mixins.
162 """
163 def isPrivate(self):
164 """
165 Public method to check, if the visibility is Private.
166
167 @return flag indicating Private visibility
168 @rtype bool
169 """
170 return self.visibility == 0
171
172 def isProtected(self):
173 """
174 Public method to check, if the visibility is Protected.
175
176 @return flag indicating Protected visibility
177 @rtype bool
178 """
179 return self.visibility == 1
180
181 def isPublic(self):
182 """
183 Public method to check, if the visibility is Public.
184
185 @return flag indicating Public visibility
186 @rtype bool
187 """
188 return self.visibility == 2
189
190 def setPrivate(self):
191 """
192 Public method to set the visibility to Private.
193 """
194 self.visibility = 0
195
196 def setProtected(self):
197 """
198 Public method to set the visibility to Protected.
199 """
200 self.visibility = 1
201
202 def setPublic(self):
203 """
204 Public method to set the visibility to Public.
205 """
206 self.visibility = 2
207
208
209 class Attribute(_ClbrBase):
210 """
211 Class to represent an attribute.
212 """
213 def __init__(self, module, name, file, lineno):
214 """
215 Constructor
216
217 @param module name of the module containing this attribute
218 @type str
219 @param name name of this attribute
220 @type str
221 @param file filename containing this attribute
222 @type str
223 @param lineno line number of the attribute definition
224 @type int
225 """
226 _ClbrBase.__init__(self, module, name, file, lineno)
227
228 self.linenos = [lineno]
229
230 def addAssignment(self, lineno):
231 """
232 Public method to add another assignment line number.
233
234 @param lineno line number of the additional attribute assignment
235 @type int
236 """
237 if lineno not in self.linenos:
238 self.linenos.append(lineno)
239
240
241 class Class(ClbrBase):
242 """
243 Class to represent a class.
244 """
245 def __init__(self, module, name, superClasses, file, lineno):
246 """
247 Constructor
248
249 @param module name of the module containing this class
250 @type str
251 @param name name of this class
252 @type str
253 @param superClasses list of class names this class is inherited from
254 @type list of str
255 @param file filename containing this class
256 @type str
257 @param lineno line number of the class definition
258 @type int
259 """
260 ClbrBase.__init__(self, module, name, file, lineno)
261 if superClasses is None:
262 superClasses = []
263 self.super = superClasses
264
265
266 class Module(ClbrBase):
267 """
268 Class to represent a module.
269 """
270 def __init__(self, module, name, file, lineno):
271 """
272 Constructor
273
274 @param module name of the module containing this module
275 @type str
276 @param name name of this module
277 @type str
278 @param file filename containing this module
279 @type str
280 @param lineno line number of the module definition
281 @type int
282 """
283 ClbrBase.__init__(self, module, name, file, lineno)
284
285
286 class Function(ClbrBase):
287 """
288 Class to represent a function or method.
289 """
290 General = 0
291 Static = 1
292 Class = 2
293
294 def __init__(self, module, name, file, lineno, signature='', separator=',',
295 modifierType=General, annotation=""):
296 """
297 Constructor
298
299 @param module name of the module containing this function
300 @type str
301 @param name name of this function
302 @type str
303 @param file filename containing this function
304 @type str
305 @param lineno line number of the function definition
306 @type int
307 @param signature parameter list of the function
308 @type str
309 @param separator string separating the parameters of the function
310 @type str
311 @param modifierType type of the function
312 @type int
313 @param annotation function return annotation
314 @type str
315 """
316 ClbrBase.__init__(self, module, name, file, lineno)
317 self.parameters = [e.strip() for e in signature.split(separator)]
318 self.modifier = modifierType
319 self.annotation = annotation
320
321
322 class Coding(ClbrBase):
323 """
324 Class to represent a source coding.
325 """
326 def __init__(self, module, file, lineno, coding):
327 """
328 Constructor
329
330 @param module name of the module containing this coding statement
331 @type str
332 @param file filename containing this coding statement
333 @type str
334 @param lineno line number of the coding definition
335 @type int
336 @param coding character coding of the source file
337 @type str
338 """
339 ClbrBase.__init__(self, module, "Coding", file, lineno)
340 self.coding = coding
341
342
343 class Enum(ClbrBase):
344 """
345 Class to represent an enum definition.
346 """
347 def __init__(self, module, name, file, lineno):
348 """
349 Constructor
350
351 @param module name of the module containing this enum
352 @type str
353 @param name name of this enum
354 @type str
355 @param file filename containing this enum
356 @type str
357 @param lineno line number of the enum definition
358 @type int
359 """
360 ClbrBase.__init__(self, module, name, file, lineno)

eric ide

mercurial