forked from cognitedata/cognite-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_parser.py
66 lines (51 loc) · 2.31 KB
/
code_parser.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''This module is used to remove typehints from source code to ensure python 2.7 compatibility.'''
import argparse
import ast
import os
import re
import sys
import astunparse
class TypeHintRemover(ast.NodeTransformer):
def visit_FunctionDef(self, node):
# remove the return type defintion
node.returns = None
# remove all argument annotations
if node.args.args:
for arg in node.args.args:
arg.annotation = None
return node
def visit_Import(self, node):
node.names = [n for n in node.names if n.name != 'typing']
return node if node.names else None
def visit_ImportFrom(self, node):
return node if node.module != 'typing' else None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-th', '--remove-type-hints', action='store_true')
parser.add_argument('--suppress-warning', action='store_true')
args = parser.parse_args()
files = [os.path.abspath(os.path.join(os.path.dirname(__file__), 'cognite', file)) for file in
os.listdir('./cognite') if re.match('.+\.py$', file)]
version_dirs = [os.path.abspath(os.path.join(os.path.dirname(__file__), 'cognite', file)) for file in
os.listdir('./cognite') if re.match('^v\d\d$', file)]
for dir in version_dirs:
version_files = [os.path.abspath(os.path.join(dir, file)) for file in os.listdir(dir) if
re.match('.+\.py$', file)]
files.extend(version_files)
if not args.suppress_warning:
if input("This will alter the source code of your project and should not be run without knowing what you're "
"doing. Enter 'PEACOCK' to continue: ") != 'PEACOCK':
sys.exit(0)
for file_path in files:
print(file_path)
# parse the source code into an AST
with open(file_path, 'r') as f:
parsed_source = ast.parse(f.read())
if args.remove_type_hints:
print("*****Removing type hints")
# remove all type annotations, function return type definitions
# and import statements from 'typing'
transformed = TypeHintRemover().visit(parsed_source)
with open(file_path, 'w') as f:
f.write(astunparse.unparse(transformed))
print()