7 Module implementing some utility and compatibility functions for working with |
7 Module implementing some utility and compatibility functions for working with |
8 the ast module. |
8 the ast module. |
9 """ |
9 """ |
10 |
10 |
11 import ast |
11 import ast |
12 import sys |
12 import numbers |
13 |
13 |
14 if sys.version_info >= (3, 8, 0): |
|
15 # functions for Python >= 3.8 |
|
16 |
14 |
17 import numbers |
15 def isNumber(node): |
|
16 """ |
|
17 Function to check that a node is a number. |
18 |
18 |
19 def isNumber(node): |
19 @param node reference to the node to check |
20 """ |
20 @type ast.AST |
21 Function to check that a node is a number. |
21 @return flag indicating a number |
|
22 @rtype bool |
|
23 """ |
|
24 return isinstance(node, ast.Constant) and isinstance(node.value, numbers.Number) |
22 |
25 |
23 @param node reference to the node to check |
|
24 @type ast.AST |
|
25 @return flag indicating a number |
|
26 @rtype bool |
|
27 """ |
|
28 return isinstance(node, ast.Constant) and isinstance(node.value, numbers.Number) |
|
29 |
26 |
30 def isString(node): |
27 def isString(node): |
31 """ |
28 """ |
32 Function to check that a node is a string. |
29 Function to check that a node is a string. |
33 |
30 |
34 @param node reference to the node to check |
31 @param node reference to the node to check |
35 @type ast.AST |
32 @type ast.AST |
36 @return flag indicating a string |
33 @return flag indicating a string |
37 @rtype bool |
34 @rtype bool |
38 """ |
35 """ |
39 return isinstance(node, ast.Constant) and isinstance(node.value, str) |
36 return isinstance(node, ast.Constant) and isinstance(node.value, str) |
40 |
37 |
41 def isBytes(node): |
|
42 """ |
|
43 Function to check that a node is a bytes. |
|
44 |
38 |
45 @param node reference to the node to check |
39 def isBytes(node): |
46 @type ast.AST |
40 """ |
47 @return flag indicating a bytes |
41 Function to check that a node is a bytes. |
48 @rtype bool |
|
49 """ |
|
50 return isinstance(node, ast.Constant) and isinstance(node.value, bytes) |
|
51 |
42 |
52 def isBaseString(node): |
43 @param node reference to the node to check |
53 """ |
44 @type ast.AST |
54 Function to check that a node is a bytes or string. |
45 @return flag indicating a bytes |
|
46 @rtype bool |
|
47 """ |
|
48 return isinstance(node, ast.Constant) and isinstance(node.value, bytes) |
55 |
49 |
56 @param node reference to the node to check |
|
57 @type ast.AST |
|
58 @return flag indicating a bytes or string |
|
59 @rtype bool |
|
60 """ |
|
61 return isinstance(node, ast.Constant) and isinstance(node.value, (bytes, str)) |
|
62 |
50 |
63 def isNameConstant(node): |
51 def isBaseString(node): |
64 """ |
52 """ |
65 Function to check that a node is a name constant. |
53 Function to check that a node is a bytes or string. |
66 |
54 |
67 @param node reference to the node to check |
55 @param node reference to the node to check |
68 @type ast.AST |
56 @type ast.AST |
69 @return flag indicating a name constant |
57 @return flag indicating a bytes or string |
70 @rtype bool |
58 @rtype bool |
71 """ |
59 """ |
72 return isinstance(node, ast.Constant) and not isinstance( |
60 return isinstance(node, ast.Constant) and isinstance(node.value, (bytes, str)) |
73 node.value, (bytes, str, numbers.Number) |
|
74 ) |
|
75 |
61 |
76 def isEllipsis(node): |
|
77 """ |
|
78 Function to check that a node is an ellipsis. |
|
79 |
62 |
80 @param node reference to the node to check |
63 def isNameConstant(node): |
81 @type ast.AST |
64 """ |
82 @return flag indicating an ellipsis |
65 Function to check that a node is a name constant. |
83 @rtype bool |
|
84 """ |
|
85 return isinstance(node, ast.Constant) and node.value is Ellipsis |
|
86 |
66 |
87 def getValue(node): |
67 @param node reference to the node to check |
88 """ |
68 @type ast.AST |
89 Function to extract the value of a node. |
69 @return flag indicating a name constant |
|
70 @rtype bool |
|
71 """ |
|
72 return isinstance(node, ast.Constant) and not isinstance( |
|
73 node.value, (bytes, str, numbers.Number) |
|
74 ) |
90 |
75 |
91 @param node reference to the node to extract the value from |
|
92 @type ast.Constant |
|
93 @return value of the node |
|
94 @rtype any |
|
95 @exception TypeError raised to indicate an unsupported type |
|
96 """ |
|
97 if not isinstance(node, ast.Constant): |
|
98 raise TypeError("Illegal node type passed.") |
|
99 |
76 |
100 return node.value |
77 def isEllipsis(node): |
|
78 """ |
|
79 Function to check that a node is an ellipsis. |
101 |
80 |
102 else: |
81 @param node reference to the node to check |
103 # functions for Python < 3.8 |
82 @type ast.AST |
|
83 @return flag indicating an ellipsis |
|
84 @rtype bool |
|
85 """ |
|
86 return isinstance(node, ast.Constant) and node.value is Ellipsis |
104 |
87 |
105 def isNumber(node): |
|
106 """ |
|
107 Function to check that a node is a number. |
|
108 |
88 |
109 @param node reference to the node to check |
89 def getValue(node): |
110 @type ast.AST |
90 """ |
111 @return flag indicating a number |
91 Function to extract the value of a node. |
112 @rtype bool |
|
113 """ |
|
114 return isinstance(node, ast.Num) |
|
115 |
92 |
116 def isString(node): |
93 @param node reference to the node to extract the value from |
117 """ |
94 @type ast.Constant |
118 Function to check that a node is a string. |
95 @return value of the node |
|
96 @rtype any |
|
97 @exception TypeError raised to indicate an unsupported type |
|
98 """ |
|
99 if not isinstance(node, ast.Constant): |
|
100 raise TypeError("Illegal node type passed.") |
119 |
101 |
120 @param node reference to the node to check |
102 return node.value |
121 @type ast.AST |
|
122 @return flag indicating a string |
|
123 @rtype bool |
|
124 """ |
|
125 return isinstance(node, ast.Str) |
|
126 |
|
127 def isBytes(node): |
|
128 """ |
|
129 Function to check that a node is a bytes. |
|
130 |
|
131 @param node reference to the node to check |
|
132 @type ast.AST |
|
133 @return flag indicating a bytes |
|
134 @rtype bool |
|
135 """ |
|
136 return isinstance(node, ast.Bytes) |
|
137 |
|
138 def isBaseString(node): |
|
139 """ |
|
140 Function to check that a node is a bytes or string. |
|
141 |
|
142 @param node reference to the node to check |
|
143 @type ast.AST |
|
144 @return flag indicating a bytes or string |
|
145 @rtype bool |
|
146 """ |
|
147 return isinstance(node, (ast.Str, ast.Bytes)) |
|
148 |
|
149 def isNameConstant(node): |
|
150 """ |
|
151 Function to check that a node is a name constant. |
|
152 |
|
153 @param node reference to the node to check |
|
154 @type ast.AST |
|
155 @return flag indicating a name constant |
|
156 @rtype bool |
|
157 """ |
|
158 return isinstance(node, ast.NameConstant) |
|
159 |
|
160 def isEllipsis(node): |
|
161 """ |
|
162 Function to check that a node is an ellipsis. |
|
163 |
|
164 @param node reference to the node to check |
|
165 @type ast.AST |
|
166 @return flag indicating an ellipsis |
|
167 @rtype bool |
|
168 """ |
|
169 return isinstance(node, ast.Ellipsis) |
|
170 |
|
171 def getValue(node): |
|
172 """ |
|
173 Function to extract the value of a node. |
|
174 |
|
175 @param node reference to the node to extract the value from |
|
176 @type one of ast.Num, ast.Str, ast.Bytes or ast.NameConstant |
|
177 @return value of the node |
|
178 @rtype one of str, bytes, int |
|
179 @exception TypeError raised to indicate an unsupported type |
|
180 """ |
|
181 if not isinstance(node, (ast.Num, ast.Str, ast.Bytes, ast.NameConstant)): |
|
182 raise TypeError("Illegal node type passed.") |
|
183 |
|
184 if isinstance(node, ast.Num): |
|
185 return node.n |
|
186 |
|
187 elif isinstance(node, (ast.Str, ast.Bytes)): |
|
188 return node.s |
|
189 |
|
190 elif isinstance(node, ast.NameConstant): |
|
191 return node.value |
|
192 |
|
193 return None |
|