forked from dflook/python-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
48 lines (38 loc) · 1.41 KB
/
util.py
File metadata and controls
48 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import ast
def is_ast_node(node, types):
"""
Is a node one of the specified node types
A node type may be an actual ast class, or a string naming one.
types is a single node type or an iterable of many.
If a node_type specified a specific Constant type (Str, Bytes, Num etc),
returns true for Constant nodes of the correct type.
:type node: ast.AST
:param types:
:rtype: bool
"""
if not isinstance(types, tuple):
types = (types,)
actual_types = []
for node_type in types:
if isinstance(node_type, str):
node_type = getattr(ast, node_type, None)
if node_type is not None:
actual_types.append(node_type)
else:
actual_types.append(node_type)
if isinstance(node, tuple(actual_types)):
return True
if hasattr(ast, 'Constant') and isinstance(node, ast.Constant):
if node.value in [None, True, False]:
return ast.NameConstant in types
elif isinstance(node.value, (int, float, complex)):
return ast.Num in types
elif isinstance(node.value, str):
return ast.Str in types
elif isinstance(node.value, bytes):
return ast.Bytes in types
elif node.value == Ellipsis:
return ast.Ellipsis in types
else:
raise RuntimeError('Unknown Constant value %r' % type(node.value))
return False