-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcode_info.py
87 lines (70 loc) · 2.27 KB
/
code_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Classes for managing infos and ast nodes about parsed python code.
Infos are fed by some ast.NodeVisitor and used by some PUML_Generator.
"""
import logging
import ast
logger = logging.getLogger() # (__name__) # pylint: disable=invalid-name
class CodeInfo:
"""
Container for collecting information about various code elements .
"""
def __init__(self):
self.variables = []
self.functions = []
def add_variable(self, name):
"Registers a global variable"
if name not in self.variables:
self.variables.append(name)
def add_function(self, node):
"Registers a function"
# don't store unneeded body
node.body = ast.Pass()
self.functions.append(node)
def done(self, context):
"Signals end of module parsing."
context.print_codeinfo(self)
@staticmethod
def visibility(name):
"""Detects the visibility of given member name, as plantuml convention symbol.
@returns '-' for private, '+' for public
"""
if name.startswith('_'):
return '-'
return '+'
class ClassInfo(CodeInfo):
"""
The parsed class definition.
Elements are grouped by type in arrays while parsing, printing done last.
"""
def __init__(self, node):
super().__init__()
logger.info("New ClassInfo: %s", ast.dump(node))
self.classname = node.name
self.bases = node.bases
# self.classvars = []
self.members = []
# self.methods = []
@property
def classvars(self):
"""The list of class variables.
Class variables are shared by all instances."""
return self.variables
@property
def methods(self):
"""The list of parsed methods of the class"""
return self.functions
def add_classvar(self, name):
"Registers a class variable"
self.add_variable(name)
def add_member(self, name):
"Registers an instance variable if new"
if name not in self.members:
self.members.append(name)
def add_method(self, node):
"Registers a method"
node.body = None
self.add_function(node)
def done(self, context):
"Signals end of class parsing."
context.print_classinfo(self)