eric6/Plugins/CheckerPlugins/CodeStyleChecker/AstUtilities.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8205
4a0f1f896341
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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

eric ide

mercurial