eric6/Plugins/CheckerPlugins/CodeStyleChecker/AstUtilities.py

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

eric ide

mercurial