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

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2019 - 2022 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 not isinstance(node, ast.Constant):
100 raise TypeError("Illegal node type passed.")
101
102 return node.value
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 isinstance(node, ast.Bytes)
139
140 def isBaseString(node):
141 """
142 Function to check that a node is a bytes or string.
143
144 @param node reference to the node to check
145 @type ast.AST
146 @return flag indicating a bytes or string
147 @rtype bool
148 """
149 return isinstance(node, (ast.Str, ast.Bytes))
150
151 def isNameConstant(node):
152 """
153 Function to check that a node is a name constant.
154
155 @param node reference to the node to check
156 @type ast.AST
157 @return flag indicating a name constant
158 @rtype bool
159 """
160 return isinstance(node, ast.NameConstant)
161
162 def getValue(node):
163 """
164 Function to extract the value of a node.
165
166 @param node reference to the node to extract the value from
167 @type one of ast.Num, ast.Str, ast.Bytes or ast.NameConstant
168 @return value of the node
169 @rtype one of str, bytes, int
170 @exception TypeError raised to indicate an unsupported type
171 """
172 if not isinstance(
173 node, (ast.Num, ast.Str, ast.Bytes, ast.NameConstant)
174 ):
175 raise TypeError("Illegal node type passed.")
176
177 if isinstance(node, ast.Num):
178 return node.n
179
180 elif isinstance(node, (ast.Str, ast.Bytes)):
181 return node.s
182
183 elif isinstance(node, ast.NameConstant):
184 return node.value
185
186 return None

eric ide

mercurial