Ejemplo n.º 1
0
    def generate_json_skeleton(self, call_parameters, parsed_args,
                               parsed_globals, **kwargs):

        # Only perform the method if the ``--generate-cli-skeleton`` was
        # included in the command line.
        if getattr(parsed_args, 'generate_cli_skeleton', False):

            # Obtain the model of the operation
            operation_model = self._operation_model

            # Generate the skeleton based on the ``input_shape``.
            argument_generator = ArgumentGenerator()
            operation_input_shape = operation_model.input_shape
            # If the ``input_shape`` is ``None``, generate an empty
            # dictionary.
            if operation_input_shape is None:
                skeleton = {}
            else:
                skeleton = argument_generator.generate_skeleton(
                    operation_input_shape)

            # Write the generated skeleton to standard output.
            sys.stdout.write(json.dumps(skeleton, indent=4))
            sys.stdout.write('\n')
            # This is the return code
            return 0
Ejemplo n.º 2
0
    def generate_json_skeleton(self, call_parameters, parsed_args,
                               parsed_globals, **kwargs):
        if getattr(parsed_args, 'generate_cli_skeleton', None):
            for_output = parsed_args.generate_cli_skeleton == 'output'
            operation_model = self._operation_model

            if for_output:
                service_name = operation_model.service_model.service_name
                operation_name = operation_model.name
                # TODO: It would be better to abstract this logic into
                # classes for both the input and output option such that
                # a similar set of inputs are taken in and output
                # similar functionality.
                return StubbedCLIOperationCaller(self._session).invoke(
                    service_name, operation_name, call_parameters,
                    parsed_globals)
            else:
                argument_generator = ArgumentGenerator()
                operation_input_shape = operation_model.input_shape
                if operation_input_shape is None:
                    skeleton = {}
                else:
                    skeleton = argument_generator.generate_skeleton(
                        operation_input_shape)

                sys.stdout.write(
                    json.dumps(skeleton, indent=4, default=json_encoder)
                )
                sys.stdout.write('\n')
                return 0
    def generate_json_skeleton(self, call_parameters, parsed_args,
                               parsed_globals, **kwargs):
        if getattr(parsed_args, 'generate_cli_skeleton', None):
            for_output = parsed_args.generate_cli_skeleton == 'output'
            operation_model = self._operation_model

            if for_output:
                service_name = operation_model.service_model.service_name
                operation_name = operation_model.name
                # TODO: It would be better to abstract this logic into
                # classes for both the input and output option such that
                # a similar set of inputs are taken in and output
                # similar functionality.
                return StubbedCLIOperationCaller(self._session).invoke(
                    service_name, operation_name, call_parameters,
                    parsed_globals)
            else:
                argument_generator = ArgumentGenerator()
                operation_input_shape = operation_model.input_shape
                if operation_input_shape is None:
                    skeleton = {}
                else:
                    skeleton = argument_generator.generate_skeleton(
                        operation_input_shape)

                sys.stdout.write(
                    json.dumps(skeleton, indent=4, default=json_encoder))
                sys.stdout.write('\n')
                return 0
Ejemplo n.º 4
0
 def _generate_input_skeleton(self, **kwargs):
     outfile = sys.stdout
     input_shape = self._operation_model.input_shape
     skeleton = {}
     if input_shape is not None:
         argument_generator = ArgumentGenerator()
         skeleton = argument_generator.generate_skeleton(input_shape)
     json.dump(skeleton, outfile, indent=4, default=json_encoder)
     outfile.write('\n')
     return 0
Ejemplo n.º 5
0
 def _make_client_call(self, client, operation_name, parameters,
                       parsed_globals):
     method_name = xform_name(operation_name)
     operation_model = client.meta.service_model.operation_model(
         operation_name)
     fake_response = {}
     if operation_model.output_shape:
         argument_generator = ArgumentGenerator(use_member_names=True)
         fake_response = argument_generator.generate_skeleton(
             operation_model.output_shape)
     with Stubber(client) as stubber:
         stubber.add_response(method_name, fake_response)
         return getattr(client, method_name)(**parameters)
 def _make_client_call(self, client, operation_name, parameters,
                       parsed_globals):
     method_name = xform_name(operation_name)
     operation_model = client.meta.service_model.operation_model(
         operation_name)
     fake_response = {}
     if operation_model.output_shape:
         argument_generator = ArgumentGenerator(use_member_names=True)
         fake_response = argument_generator.generate_skeleton(
             operation_model.output_shape)
     with Stubber(client) as stubber:
         stubber.add_response(method_name, fake_response)
         return getattr(client, method_name)(**parameters)
    def generate_json_skeleton(self, call_parameters, parsed_args, parsed_globals, **kwargs):

        # Only perform the method if the ``--generate-cli-skeleton`` was
        # included in the command line.
        if getattr(parsed_args, "generate_cli_skeleton", False):

            # Obtain the model of the operation
            operation_model = self._operation_object.model

            # Generate the skeleton based on the ``input_shape``.
            argument_generator = ArgumentGenerator()
            operation_input_shape = operation_model.input_shape
            # If the ``input_shape`` is ``None``, generate an empty
            # dictionary.
            if operation_input_shape is None:
                skeleton = {}
            else:
                skeleton = argument_generator.generate_skeleton(operation_input_shape)

            # Write the generated skeleton to standard output.
            sys.stdout.write(json.dumps(skeleton, indent=4))
            sys.stdout.write("\n")
            # This is the return code
            return 0
Ejemplo n.º 8
0
class TestArgumentGenerator(unittest.TestCase):
    def setUp(self):
        self.arg_generator = ArgumentGenerator()

    def assert_skeleton_from_model_is(self, model, generated_skeleton):
        shape = DenormalizedStructureBuilder().with_members(
            model).build_model()
        actual = self.arg_generator.generate_skeleton(shape)
        self.assertEqual(actual, generated_skeleton)

    def test_generate_string(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {'type': 'string'}
            },
            generated_skeleton={
                'A': ''
            }
        )

    def test_generate_scalars(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {'type': 'string'},
                'B': {'type': 'integer'},
                'C': {'type': 'float'},
                'D': {'type': 'boolean'},
            },
            generated_skeleton={
                'A': '',
                'B': 0,
                'C': 0.0,
                'D': True,
            }
        )

    def test_generate_nested_structure(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {
                    'type': 'structure',
                    'members': {
                        'B': {'type': 'string'},
                    }
                }
            },
            generated_skeleton={
                'A': {'B': ''}
            }
        )

    def test_generate_scalar_list(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {
                    'type': 'list',
                    'member': {
                        'type': 'string'
                    }
                },
            },
            generated_skeleton={
                'A': [''],
            }
        )

    def test_generate_scalar_map(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {
                    'type': 'map',
                    'key': {'type': 'string'},
                    'value':  {'type': 'string'},
                }
            },
            generated_skeleton={
                'A': {
                    'KeyName': '',
                }
            }
        )

    def test_handles_recursive_shapes(self):
        # We're not using assert_skeleton_from_model_is
        # because we can't use a DenormalizedStructureBuilder,
        # we need a normalized model to represent recursive
        # shapes.
        shape_map = ShapeResolver({
            'InputShape': {
                'type': 'structure',
                'members': {
                    'A': {'shape': 'RecursiveStruct'},
                    'B': {'shape': 'StringType'},
                }
            },
            'RecursiveStruct': {
                'type': 'structure',
                'members': {
                    'C': {'shape': 'RecursiveStruct'},
                    'D': {'shape': 'StringType'},
                }
            },
            'StringType': {
                'type': 'string',
            }
        })
        shape = shape_map.get_shape_by_name('InputShape')
        actual = self.arg_generator.generate_skeleton(shape)
        expected = {
            'A': {
                'C': {
                    # For recurisve shapes, we'll just show
                    # an empty dict.
                },
                'D': ''
            },
            'B': ''
        }
        self.assertEqual(actual, expected)
Ejemplo n.º 9
0
def _search_jmespath_expression(expression, op_model):
    arg_gen = ArgumentGenerator(use_member_names=True)
    sample_output = arg_gen.generate_skeleton(op_model.output_shape)
    search_result = jmespath.search(expression, sample_output)
    return search_result
Ejemplo n.º 10
0
class TestArgumentGenerator(unittest.TestCase):
    def setUp(self):
        self.arg_generator = ArgumentGenerator()

    def assert_skeleton_from_model_is(self, model, generated_skeleton):
        shape = DenormalizedStructureBuilder().with_members(
            model).build_model()
        actual = self.arg_generator.generate_skeleton(shape)
        self.assertEqual(actual, generated_skeleton)

    def test_generate_string(self):
        self.assert_skeleton_from_model_is(model={'A': {
            'type': 'string'
        }},
                                           generated_skeleton={'A': ''})

    def test_generate_scalars(self):
        self.assert_skeleton_from_model_is(model={
            'A': {
                'type': 'string'
            },
            'B': {
                'type': 'integer'
            },
            'C': {
                'type': 'float'
            },
            'D': {
                'type': 'boolean'
            },
        },
                                           generated_skeleton={
                                               'A': '',
                                               'B': 0,
                                               'C': 0.0,
                                               'D': True,
                                           })

    def test_generate_nested_structure(self):
        self.assert_skeleton_from_model_is(model={
            'A': {
                'type': 'structure',
                'members': {
                    'B': {
                        'type': 'string'
                    },
                }
            }
        },
                                           generated_skeleton={'A': {
                                               'B': ''
                                           }})

    def test_generate_scalar_list(self):
        self.assert_skeleton_from_model_is(model={
            'A': {
                'type': 'list',
                'member': {
                    'type': 'string'
                }
            },
        },
                                           generated_skeleton={
                                               'A': [''],
                                           })

    def test_generate_scalar_map(self):
        self.assert_skeleton_from_model_is(
            model={
                'A': {
                    'type': 'map',
                    'key': {
                        'type': 'string'
                    },
                    'value': {
                        'type': 'string'
                    },
                }
            },
            generated_skeleton={'A': {
                'KeyName': '',
            }})

    def test_handles_recursive_shapes(self):
        # We're not using assert_skeleton_from_model_is
        # because we can't use a DenormalizedStructureBuilder,
        # we need a normalized model to represent recursive
        # shapes.
        shape_map = ShapeResolver({
            'InputShape': {
                'type': 'structure',
                'members': {
                    'A': {
                        'shape': 'RecursiveStruct'
                    },
                    'B': {
                        'shape': 'StringType'
                    },
                }
            },
            'RecursiveStruct': {
                'type': 'structure',
                'members': {
                    'C': {
                        'shape': 'RecursiveStruct'
                    },
                    'D': {
                        'shape': 'StringType'
                    },
                }
            },
            'StringType': {
                'type': 'string',
            }
        })
        shape = shape_map.get_shape_by_name('InputShape')
        actual = self.arg_generator.generate_skeleton(shape)
        expected = {
            'A': {
                'C': {
                    # For recurisve shapes, we'll just show
                    # an empty dict.
                },
                'D': ''
            },
            'B': ''
        }
        self.assertEqual(actual, expected)
Ejemplo n.º 11
0
class TestArgumentGenerator(unittest.TestCase):
    def setUp(self):
        self.arg_generator = ArgumentGenerator()

    def assert_skeleton_from_model_is(self, model, generated_skeleton):
        shape = DenormalizedStructureBuilder().with_members(model).build_model()
        actual = self.arg_generator.generate_skeleton(shape)
        self.assertEqual(actual, generated_skeleton)

    def test_generate_string(self):
        self.assert_skeleton_from_model_is(model={"A": {"type": "string"}}, generated_skeleton={"A": ""})

    def test_generate_scalars(self):
        self.assert_skeleton_from_model_is(
            model={"A": {"type": "string"}, "B": {"type": "integer"}, "C": {"type": "float"}, "D": {"type": "boolean"}},
            generated_skeleton={"A": "", "B": 0, "C": 0.0, "D": True},
        )

    def test_generate_nested_structure(self):
        self.assert_skeleton_from_model_is(
            model={"A": {"type": "structure", "members": {"B": {"type": "string"}}}},
            generated_skeleton={"A": {"B": ""}},
        )

    def test_generate_scalar_list(self):
        self.assert_skeleton_from_model_is(
            model={"A": {"type": "list", "member": {"type": "string"}}}, generated_skeleton={"A": [""]}
        )

    def test_generate_scalar_map(self):
        self.assert_skeleton_from_model_is(
            model={"A": {"type": "map", "key": {"type": "string"}, "value": {"type": "string"}}},
            generated_skeleton={"A": {"KeyName": ""}},
        )

    def test_handles_recursive_shapes(self):
        # We're not using assert_skeleton_from_model_is
        # because we can't use a DenormalizedStructureBuilder,
        # we need a normalized model to represent recursive
        # shapes.
        shape_map = ShapeResolver(
            {
                "InputShape": {
                    "type": "structure",
                    "members": {"A": {"shape": "RecursiveStruct"}, "B": {"shape": "StringType"}},
                },
                "RecursiveStruct": {
                    "type": "structure",
                    "members": {"C": {"shape": "RecursiveStruct"}, "D": {"shape": "StringType"}},
                },
                "StringType": {"type": "string"},
            }
        )
        shape = shape_map.get_shape_by_name("InputShape")
        actual = self.arg_generator.generate_skeleton(shape)
        expected = {
            "A": {
                "C": {
                    # For recurisve shapes, we'll just show
                    # an empty dict.
                },
                "D": "",
            },
            "B": "",
        }
        self.assertEqual(actual, expected)
Ejemplo n.º 12
0
def _search_jmespath_expression(expression, op_model):
    arg_gen = ArgumentGenerator(use_member_names=True)
    sample_output = arg_gen.generate_skeleton(op_model.output_shape)
    search_result = jmespath.search(expression, sample_output)
    return search_result