| 1 |
import os |
|---|
| 2 |
from model import System, Module, Class, Function, parseFile, processModuleAst |
|---|
| 3 |
|
|---|
| 4 |
class CodeParser(object): |
|---|
| 5 |
""" |
|---|
| 6 |
Information about the structure of a Python module |
|---|
| 7 |
|
|---|
| 8 |
* Collects modules, classes, methods, functions and associated docstrings |
|---|
| 9 |
* Based on mwh's docextractor.model module |
|---|
| 10 |
""" |
|---|
| 11 |
def __init__(self, pyfile, log=None): |
|---|
| 12 |
if log: |
|---|
| 13 |
self.log = log.codeparser |
|---|
| 14 |
else: |
|---|
| 15 |
import logger |
|---|
| 16 |
self.log = logger.default.codeparser |
|---|
| 17 |
self.modules = [] |
|---|
| 18 |
self.classes = [] |
|---|
| 19 |
self.methods = [] |
|---|
| 20 |
self.method_func = [] |
|---|
| 21 |
self.functions = [] |
|---|
| 22 |
self.docstrings = {} |
|---|
| 23 |
|
|---|
| 24 |
(path, filename) = os.path.split(pyfile) |
|---|
| 25 |
(module, ext) = os.path.splitext(filename) |
|---|
| 26 |
self.log("Inspecting file: " + pyfile) |
|---|
| 27 |
|
|---|
| 28 |
self.system = System() |
|---|
| 29 |
try: |
|---|
| 30 |
processModuleAst(parseFile(pyfile), module, self.system) |
|---|
| 31 |
except: |
|---|
| 32 |
return |
|---|
| 33 |
|
|---|
| 34 |
for obj in self.system.orderedallobjects: |
|---|
| 35 |
fullname = obj.fullName() |
|---|
| 36 |
if isinstance(obj, Module): |
|---|
| 37 |
self.modules.append(fullname) |
|---|
| 38 |
if isinstance(obj, Class): |
|---|
| 39 |
self.classes.append(fullname) |
|---|
| 40 |
if isinstance(obj, Function): |
|---|
| 41 |
self.method_func.append(fullname) |
|---|
| 42 |
if isinstance(obj.docstring, str) and obj.docstring.strip(): |
|---|
| 43 |
self.docstrings[fullname] = 1 |
|---|
| 44 |
|
|---|
| 45 |
for method_or_func in self.method_func: |
|---|
| 46 |
method_found = 0 |
|---|
| 47 |
for cls in self.classes: |
|---|
| 48 |
if method_or_func.startswith(cls): |
|---|
| 49 |
self.methods.append(method_or_func) |
|---|
| 50 |
method_found = 1 |
|---|
| 51 |
break |
|---|
| 52 |
if not method_found: |
|---|
| 53 |
self.functions.append(method_or_func) |
|---|
| 54 |
|
|---|
| 55 |
self.log("modules: " + ",".join(self.modules)) |
|---|
| 56 |
self.log("classes: " + ",".join(self.classes)) |
|---|
| 57 |
self.log("methods: " + ",".join(self.methods)) |
|---|
| 58 |
self.log("functions: " + ",".join(self.functions)) |
|---|
| 59 |
|
|---|
| 60 |
def object_count(self): |
|---|
| 61 |
""" |
|---|
| 62 |
Return number of objects found in this module |
|---|
| 63 |
|
|---|
| 64 |
* module |
|---|
| 65 |
* classes |
|---|
| 66 |
* methods |
|---|
| 67 |
* functions |
|---|
| 68 |
""" |
|---|
| 69 |
module_count = len(self.modules) |
|---|
| 70 |
cls_count = len(self.classes) |
|---|
| 71 |
method_count = len(self.methods) |
|---|
| 72 |
func_count = len(self.functions) |
|---|
| 73 |
return module_count + cls_count + method_count + func_count |
|---|
| 74 |
|
|---|
| 75 |
def docstring_count(self): |
|---|
| 76 |
""" |
|---|
| 77 |
Return number of docstrings found in this module |
|---|
| 78 |
""" |
|---|
| 79 |
return len(self.docstrings.keys()) |
|---|
| 80 |
|
|---|
| 81 |
def functions_called(self): |
|---|
| 82 |
""" |
|---|
| 83 |
Return list of functions called by functions/methods |
|---|
| 84 |
defined in this module |
|---|
| 85 |
""" |
|---|
| 86 |
return self.system.func_called.keys() |
|---|