src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/AstUtilities.py

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

eric ide

mercurial