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,88 @@ 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 .
56
66
See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
57
67
58
68
Args:
59
69
node (ast.Call): a node that represents a function call.
60
70
61
71
Returns:
62
- ast.Call: a node that represents a function call, which has
63
- potentially been modified from the original input.
72
+ ast.AST: if the returned node is None, the original node is removed
73
+ from its location. Otherwise, the original node is replaced with the
74
+ returned node.
64
75
"""
65
76
for function_checker in FUNCTION_CALL_MODIFIERS :
66
- function_checker .check_and_modify_node (node )
77
+ node = function_checker .check_and_modify_node (node )
78
+ return ast .fix_missing_locations (node ) if node else None
79
+
80
+ def visit_Name (self , node ):
81
+ """Visits an ``ast.Name`` node and returns a modified node or None.
82
+ See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
67
83
68
- ast .fix_missing_locations (node )
84
+ Args:
85
+ node (ast.Name): a node that represents an identifier.
86
+
87
+ Returns:
88
+ ast.AST: if the returned node is None, the original node is removed
89
+ from its location. Otherwise, the original node is replaced with the
90
+ returned node.
91
+ """
92
+ for name_checker in NAME_MODIFIERS :
93
+ node = name_checker .check_and_modify_node (node )
94
+ if node is None :
95
+ return None
96
+ node = ast .fix_missing_locations (node )
69
97
return node
70
98
71
99
def visit_Import (self , node ):
72
- """Visits an ``ast.Import`` node and returns a modified node, if needed .
100
+ """Visits an ``ast.Import`` node and returns a modified node or None .
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 the
109
+ 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
84
114
85
- ast .fix_missing_locations (node )
86
- return node
115
+ def visit_Module (self , node ):
116
+ """Visits an ``ast.Module`` node and returns a modified node or None.
117
+ See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
118
+
119
+ The ``ast.NodeTransformer`` walks the abstract syntax tree and modifies
120
+ all other nodes before modifying the ``ast.Module`` node.
121
+
122
+ Args:
123
+ node (ast.Module): a node that represents a Python module.
124
+
125
+ Returns:
126
+ ast.AST: if the returned node is None, the original node is removed
127
+ from its location. Otherwise, the original node is replaced with the
128
+ returned node.
129
+ """
130
+ self .generic_visit (node )
131
+ for module_checker in MODULE_MODIFIERS :
132
+ node = module_checker .check_and_modify_node (node )
133
+ return ast .fix_missing_locations (node ) if node else None
87
134
88
135
def visit_ImportFrom (self , node ):
89
- """Visits an ``ast.ImportFrom`` node and returns a modified node, if needed .
136
+ """Visits an ``ast.ImportFrom`` node and returns a modified node or None .
90
137
See https://docs.python.org/3/library/ast.html#ast.NodeTransformer.
91
138
92
139
Args:
93
140
node (ast.ImportFrom): a node that represents an import statement.
94
141
95
142
Returns:
96
- ast.ImportFrom: a node that represents an import statement, which has
97
- potentially been modified from the original input.
143
+ ast.AST: if the returned node is None, the original node is removed
144
+ from its location. Otherwise, the original node is replaced with the
145
+ returned node.
98
146
"""
99
147
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
148
+ node = import_checker .check_and_modify_node (node )
149
+ return ast .fix_missing_locations (node ) if node else None
0 commit comments