src/eric7/Utilities/ClassBrowsers/ClbrBaseClasses.py

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

eric ide

mercurial