103 ^ |
103 ^ |
104 (?P<ConditionalDefineIndent> [ \t]* ) |
104 (?P<ConditionalDefineIndent> [ \t]* ) |
105 (?: (?: if | elif ) [ \t]+ [^:]* | else [ \t]* ) : (?= \s* def) |
105 (?: (?: if | elif ) [ \t]+ [^:]* | else [ \t]* ) : (?= \s* def) |
106 ) |
106 ) |
107 |
107 |
|
108 | (?P<Import> |
|
109 ^ [ \t]* (?: import | from [ \t]+ \. [ \t]+ import ) [ \t]+ |
|
110 (?P<ImportList> (?: [^#;\\\n]* (?: \\\n )* )* ) |
|
111 ) |
|
112 |
|
113 | (?P<ImportFrom> |
|
114 ^ [ \t]* from [ \t]+ |
|
115 (?P<ImportFromPath> |
|
116 \.* \w+ |
|
117 (?: |
|
118 [ \t]* \. [ \t]* \w+ |
|
119 )* |
|
120 ) |
|
121 [ \t]+ |
|
122 import [ \t]+ |
|
123 (?P<ImportFromList> |
|
124 (?: \( \s* .*? \s* \) ) |
|
125 | |
|
126 (?: [^#;\\\n]* (?: \\\n )* )* ) |
|
127 ) |
|
128 |
108 | (?P<CodingLine> |
129 | (?P<CodingLine> |
109 ^ \# \s* [*_-]* \s* coding[:=] \s* (?P<Coding> [-\w_.]+ ) \s* [*_-]* $ |
130 ^ \# \s* [*_-]* \s* coding[:=] \s* (?P<Coding> [-\w_.]+ ) \s* [*_-]* $ |
110 ) |
131 ) |
111 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search # __IGNORE_WARNING__ |
132 """, re.VERBOSE | re.DOTALL | re.MULTILINE).search # __IGNORE_WARNING__ |
112 |
133 |
207 self.name = '__all__' |
228 self.name = '__all__' |
208 self.file = file |
229 self.file = file |
209 self.lineno = lineno |
230 self.lineno = lineno |
210 self.identifiers = [e.replace('"', '').replace("'", "").strip() |
231 self.identifiers = [e.replace('"', '').replace("'", "").strip() |
211 for e in idents.split(',')] |
232 for e in idents.split(',')] |
|
233 |
|
234 |
|
235 class Imports(object): |
|
236 """ |
|
237 Class to represent the list of imported modules. |
|
238 """ |
|
239 def __init__(self, module, file): |
|
240 """ |
|
241 Constructor |
|
242 |
|
243 @param module name of the module containing the import (string) |
|
244 @param file file name containing the import (string) |
|
245 """ |
|
246 self.module = module |
|
247 self.name = 'import' |
|
248 self.file = file |
|
249 self.imports = {} |
|
250 |
|
251 def addImport(self, moduleName, names, lineno): |
|
252 """ |
|
253 Public method to add a list of imported names. |
|
254 |
|
255 @param moduleName name of the imported module (string) |
|
256 @param names list of names (list of strings) |
|
257 @param lineno line number of the import |
|
258 """ |
|
259 if moduleName not in self.imports: |
|
260 module = ImportedModule(self.module, self.file, moduleName) |
|
261 self.imports[moduleName] = module |
|
262 else: |
|
263 module = self.imports[moduleName] |
|
264 module.addImport(lineno, names) |
|
265 |
|
266 def getImport(self, moduleName): |
|
267 """ |
|
268 Public method to get an imported module item. |
|
269 |
|
270 @param moduleName name of the imported module (string) |
|
271 @return imported module item (ImportedModule) or None |
|
272 """ |
|
273 if moduleName in self.imports: |
|
274 return self.imports[moduleName] |
|
275 else: |
|
276 return None |
|
277 |
|
278 def getImports(self): |
|
279 """ |
|
280 Public method to get all imported module names. |
|
281 |
|
282 @return dictionary of imported module names with name as key and list |
|
283 of line numbers of imports as value |
|
284 """ |
|
285 return self.imports |
|
286 |
|
287 |
|
288 class ImportedModule(object): |
|
289 """ |
|
290 Class to represent an imported module. |
|
291 """ |
|
292 def __init__(self, module, file, importedModule): |
|
293 """ |
|
294 Constructor |
|
295 |
|
296 @param module name of the module containing the import (string) |
|
297 @param file file name containing the import (string) |
|
298 @param importedModule name of the imported module (string) |
|
299 """ |
|
300 self.module = module |
|
301 self.name = 'import' |
|
302 self.file = file |
|
303 self.importedModuleName = importedModule |
|
304 self.linenos = [] |
|
305 self.importedNames = {} |
|
306 # dictionary of imported names with name as key and list of line |
|
307 # numbers as value |
|
308 |
|
309 def addImport(self, lineno, importedNames): |
|
310 """ |
|
311 Public method to add a list of imported names. |
|
312 |
|
313 @param lineno line number of the import |
|
314 @param importedNames list of imported names (list of strings) |
|
315 """ |
|
316 if lineno not in self.linenos: |
|
317 self.linenos.append(lineno) |
|
318 |
|
319 for name in importedNames: |
|
320 if name not in self.importedNames: |
|
321 self.importedNames[name] = [lineno] |
|
322 else: |
|
323 self.importedNames[name].append(lineno) |
212 |
324 |
213 |
325 |
214 def readmodule_ex(module, path=[], inpackage=False, isPyFile=False): |
326 def readmodule_ex(module, path=[], inpackage=False, isPyFile=False): |
215 """ |
327 """ |
216 Read a module file and return a dictionary of classes. |
328 Read a module file and return a dictionary of classes. |
478 lineno = lineno + src.count('\n', last_lineno_pos, start) |
590 lineno = lineno + src.count('\n', last_lineno_pos, start) |
479 last_lineno_pos = start |
591 last_lineno_pos = start |
480 pubs = Publics(module, file, lineno, idents) |
592 pubs = Publics(module, file, lineno, idents) |
481 dict['__all__'] = pubs |
593 dict['__all__'] = pubs |
482 |
594 |
|
595 elif m.start("Import") >= 0: |
|
596 # import module |
|
597 names = [n.strip() for n in |
|
598 "".join(m.group("ImportList").splitlines()) |
|
599 .replace("\\", "").split(',')] |
|
600 lineno = lineno + src.count('\n', last_lineno_pos, start) |
|
601 last_lineno_pos = start |
|
602 if "@@Import@@" not in dict: |
|
603 dict["@@Import@@"] = Imports(module, file) |
|
604 for name in names: |
|
605 dict["@@Import@@"].addImport(name, [], lineno) |
|
606 |
|
607 elif m.start("ImportFrom") >= 0: |
|
608 # from module import stuff |
|
609 mod = m.group("ImportFromPath") |
|
610 namesLines = (m.group("ImportFromList") |
|
611 .replace("(", "").replace(")", "") |
|
612 .replace("\\", "") |
|
613 .strip().splitlines()) |
|
614 namesLines = [line.split("#")[0].strip() |
|
615 for line in namesLines] |
|
616 names = [n.strip() for n in |
|
617 "".join(namesLines) |
|
618 .split(',')] |
|
619 lineno = lineno + src.count('\n', last_lineno_pos, start) |
|
620 last_lineno_pos = start |
|
621 if "@@Import@@" not in dict: |
|
622 dict["@@Import@@"] = Imports(module, file) |
|
623 dict["@@Import@@"].addImport(mod, names, lineno) |
|
624 |
483 elif m.start("ConditionalDefine") >= 0: |
625 elif m.start("ConditionalDefine") >= 0: |
484 # a conditional function/method definition |
626 # a conditional function/method definition |
485 thisindent = _indent(m.group("ConditionalDefineIndent")) |
627 thisindent = _indent(m.group("ConditionalDefineIndent")) |
486 while conditionalsstack and \ |
628 while conditionalsstack and \ |
487 conditionalsstack[-1] >= thisindent: |
629 conditionalsstack[-1] >= thisindent: |