35
35
modifiers .renamed_params .SessionCreateEndpointImageURIRenamer (),
36
36
modifiers .training_params .TrainPrefixRemover (),
37
37
modifiers .training_input .TrainingInputConstructorRefactor (),
38
+ modifiers .serde .SerdeConstructorRenamer (),
38
39
]
39
40
40
41
IMPORT_MODIFIERS = [modifiers .tfs .TensorFlowServingImportRenamer ()]
41
42
43
+ NAME_MODIFIERS = [modifiers .serde .SerdeObjectRenamer ()]
44
+
45
+ MODULE_MODIFIERS = [
46
+ modifiers .serde .SerializerImportInserter (),
47
+ modifiers .serde .DeserializerImportInserter (),
48
+ ]
49
+
42
50
IMPORT_FROM_MODIFIERS = [
43
51
modifiers .predictors .PredictorImportFromRenamer (),
44
52
modifiers .tfs .TensorFlowServingImportFromRenamer (),
45
53
modifiers .training_input .TrainingInputImportFromRenamer (),
54
+ modifiers .serde .SerdeImportFromAmazonCommonRenamer (),
55
+ modifiers .serde .SerdeImportFromPredictorRenamer (),
46
56
]
47
57
48
58
@@ -52,52 +62,90 @@ class ASTTransformer(ast.NodeTransformer):
52
62
"""
53
63
54
64
def visit_Call (self , node ):
55
- """Visits an ``ast.Call`` node and returns a modified node, if needed.
65
+ """Visits an ``ast.Call`` node and returns a modified node or None.
66
+
56
67
See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
57
68
58
69
Args:
59
70
node (ast.Call): a node that represents a function call.
60
71
61
72
Returns:
62
- ast.Call: a node that represents a function call, which has
63
- potentially been modified from the original input.
73
+ ast.AST: if the returned node is None, the original node is removed
74
+ from its location. Otherwise, the original node is replaced with
75
+ the returned node.
64
76
"""
65
77
for function_checker in FUNCTION_CALL_MODIFIERS :
66
- function_checker .check_and_modify_node (node )
78
+ node = function_checker .check_and_modify_node (node )
79
+ return ast .fix_missing_locations (node ) if node else None
80
+
81
+ def visit_Name (self , node ):
82
+ """Visits an ``ast.Name`` node and returns a modified node or None.
67
83
68
- ast .fix_missing_locations (node )
69
- return node
84
+ See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
85
+
86
+ Args:
87
+ node (ast.Name): a node that represents an identifier.
88
+
89
+ Returns:
90
+ ast.AST: if the returned node is None, the original node is removed
91
+ from its location. Otherwise, the original node is replaced with
92
+ the returned node.
93
+ """
94
+ for name_checker in NAME_MODIFIERS :
95
+ node = name_checker .check_and_modify_node (node )
96
+ return ast .fix_missing_locations (node ) if node else None
70
97
71
98
def visit_Import (self , node ):
72
- """Visits an ``ast.Import`` node and returns a modified node, if needed.
99
+ """Visits an ``ast.Import`` node and returns a modified node or None.
100
+
73
101
See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
74
102
75
103
Args:
76
104
node (ast.Import): a node that represents an import statement.
77
105
78
106
Returns:
79
- ast.Import: a node that represents an import statement, which has
80
- potentially been modified from the original input.
107
+ ast.AST: if the returned node is None, the original node is removed
108
+ from its location. Otherwise, the original node is replaced with
109
+ the returned node.
81
110
"""
82
111
for import_checker in IMPORT_MODIFIERS :
83
- import_checker .check_and_modify_node (node )
112
+ node = import_checker .check_and_modify_node (node )
113
+ return ast .fix_missing_locations (node ) if node else None
114
+
115
+ def visit_Module (self , node ):
116
+ """Visits an ``ast.Module`` node and returns a modified node or None.
117
+
118
+ See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
119
+
120
+ The ``ast.NodeTransformer`` walks the abstract syntax tree and modifies
121
+ all other nodes before modifying the ``ast.Module`` node.
84
122
85
- ast .fix_missing_locations (node )
86
- return node
123
+ Args:
124
+ node (ast.Module): a node that represents a Python module.
125
+
126
+ Returns:
127
+ ast.AST: if the returned node is None, the original node is removed
128
+ from its location. Otherwise, the original node is replaced with
129
+ the returned node.
130
+ """
131
+ self .generic_visit (node )
132
+ for module_checker in MODULE_MODIFIERS :
133
+ node = module_checker .check_and_modify_node (node )
134
+ return ast .fix_missing_locations (node ) if node else None
87
135
88
136
def visit_ImportFrom (self , node ):
89
- """Visits an ``ast.ImportFrom`` node and returns a modified node, if needed.
137
+ """Visits an ``ast.ImportFrom`` node and returns a modified node or None.
138
+
90
139
See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
91
140
92
141
Args:
93
142
node (ast.ImportFrom): a node that represents an import statement.
94
143
95
144
Returns:
96
- ast.ImportFrom: a node that represents an import statement, which has
97
- potentially been modified from the original input.
145
+ ast.AST: if the returned node is None, the original node is removed
146
+ from its location. Otherwise, the original node is replaced with
147
+ the returned node.
98
148
"""
99
149
for import_checker in IMPORT_FROM_MODIFIERS :
100
- import_checker .check_and_modify_node (node )
101
-
102
- ast .fix_missing_locations (node )
103
- return node
150
+ node = import_checker .check_and_modify_node (node )
151
+ return ast .fix_missing_locations (node ) if node else None
0 commit comments