|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Classes to modify TrainingInput code to be compatible |
| 14 | +with version 2.0 and later of the SageMaker Python SDK. |
| 15 | +""" |
| 16 | +from __future__ import absolute_import |
| 17 | + |
| 18 | +import ast |
| 19 | + |
| 20 | +from sagemaker.cli.compatibility.v2.modifiers import matching |
| 21 | +from sagemaker.cli.compatibility.v2.modifiers.modifier import Modifier |
| 22 | + |
| 23 | +S3_INPUT_NAME = "s3_input" |
| 24 | +S3_INPUT_NAMESPACES = ("sagemaker", "sagemaker.inputs", "sagemaker.session") |
| 25 | + |
| 26 | + |
| 27 | +class TrainingInputConstructorRefactor(Modifier): |
| 28 | + """A class to refactor *s3_input class.""" |
| 29 | + |
| 30 | + def node_should_be_modified(self, node): |
| 31 | + """Checks if the ``ast.Call`` node instantiates a class of interest. |
| 32 | +
|
| 33 | + This looks for the following calls: |
| 34 | +
|
| 35 | + - ``sagemaker.s3_input`` |
| 36 | + - ``sagemaker.session.s3_input`` |
| 37 | + - ``s3_input`` |
| 38 | +
|
| 39 | + Args: |
| 40 | + node (ast.Call): a node that represents a function call. For more, |
| 41 | + see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + bool: If the ``ast.Call`` instantiates a class of interest. |
| 45 | + """ |
| 46 | + return matching.matches_name_or_namespaces(node, S3_INPUT_NAME, S3_INPUT_NAMESPACES) |
| 47 | + |
| 48 | + def modify_node(self, node): |
| 49 | + """Modifies the ``ast.Call`` node to call ``TrainingInput`` instead. |
| 50 | +
|
| 51 | + Args: |
| 52 | + node (ast.Call): a node that represents a *TrainingInput constructor. |
| 53 | + """ |
| 54 | + if matching.matches_name(node, S3_INPUT_NAME): |
| 55 | + node.func.id = "TrainingInput" |
| 56 | + elif matching.matches_attr(node, S3_INPUT_NAME): |
| 57 | + node.func.attr = "TrainingInput" |
| 58 | + _rename_namespace(node, "session") |
| 59 | + |
| 60 | + |
| 61 | +def _rename_namespace(node, name): |
| 62 | + """Rename namespace ``session`` to ``inputs`` """ |
| 63 | + if isinstance(node.func.value, ast.Attribute) and node.func.value.attr == name: |
| 64 | + node.func.value.attr = "inputs" |
| 65 | + elif isinstance(node.func.value, ast.Name) and node.func.value.id == name: |
| 66 | + node.func.value.id = "inputs" |
| 67 | + |
| 68 | + |
| 69 | +class TrainingInputImportFromRenamer(Modifier): |
| 70 | + """A class to update import statements of ``s3_input``.""" |
| 71 | + |
| 72 | + def node_should_be_modified(self, node): |
| 73 | + """Checks if the import statement imports ``s3_input`` from the correct module. |
| 74 | +
|
| 75 | + Args: |
| 76 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 77 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + bool: If the import statement imports ``s3_input`` from the correct module. |
| 81 | + """ |
| 82 | + return node.module in S3_INPUT_NAMESPACES and any( |
| 83 | + name.name == S3_INPUT_NAME for name in node.names |
| 84 | + ) |
| 85 | + |
| 86 | + def modify_node(self, node): |
| 87 | + """Changes the ``ast.ImportFrom`` node's name from ``s3_input`` to ``TrainingInput``. |
| 88 | +
|
| 89 | + Args: |
| 90 | + node (ast.ImportFrom): a node that represents a ``from ... import ... `` statement. |
| 91 | + For more, see https://docs.python.org/3/library/ast.html#abstract-grammar. |
| 92 | + """ |
| 93 | + for name in node.names: |
| 94 | + if name.name == S3_INPUT_NAME: |
| 95 | + name.name = "TrainingInput" |
| 96 | + if node.module == "sagemaker.session": |
| 97 | + node.module = "sagemaker.inputs" |
0 commit comments