|
| 1 | +import six |
1 | 2 | import sys
|
2 | 3 | import copy
|
3 | 4 | import logging
|
|
18 | 19 | LOG = logging.getLogger(__name__)
|
19 | 20 |
|
20 | 21 |
|
| 22 | +class ParamCoercion(object): |
| 23 | + """This class coerces string parameters into the correct type. |
| 24 | +
|
| 25 | + By default this converts strings to numerical values if the input |
| 26 | + parameters model indicates that the field should be a number. This is to |
| 27 | + compensate for the fact that values taken in from prompts will always be |
| 28 | + strings and avoids having to create specific interactions for simple |
| 29 | + conversions or having to specify the type in the wizard specification. |
| 30 | + """ |
| 31 | + |
| 32 | + _DEFAULT_DICT = { |
| 33 | + 'integer': int, |
| 34 | + 'float': float, |
| 35 | + 'double': float, |
| 36 | + 'long': int |
| 37 | + } |
| 38 | + |
| 39 | + def __init__(self, type_dict=_DEFAULT_DICT): |
| 40 | + """Initialize a ParamCoercion object. |
| 41 | +
|
| 42 | + :type type_dict: dict |
| 43 | + :param type_dict: (Optional) A dictionary of converstions. Keys are |
| 44 | + strings representing the shape type name and the values are callables |
| 45 | + that given a string will return an instance of an appropriate type for |
| 46 | + that shape type. Defaults to only coerce numbers. |
| 47 | + """ |
| 48 | + self._type_dict = type_dict |
| 49 | + |
| 50 | + def coerce(self, params, shape): |
| 51 | + """Coerce the params according to the given shape. |
| 52 | +
|
| 53 | + :type params: dict |
| 54 | + :param params: The parameters to be given to an operation call. |
| 55 | +
|
| 56 | + :type shape: :class:`botocore.model.Shape` |
| 57 | + :param shape: The input shape for the desired operation. |
| 58 | +
|
| 59 | + :rtype: dict |
| 60 | + :return: The coerced version of the params. |
| 61 | + """ |
| 62 | + name = shape.type_name |
| 63 | + if isinstance(params, dict) and name == 'structure': |
| 64 | + return self._coerce_structure(params, shape) |
| 65 | + elif isinstance(params, dict) and name == 'map': |
| 66 | + return self._coerce_map(params, shape) |
| 67 | + elif isinstance(params, (list, tuple)) and name == 'list': |
| 68 | + return self._coerce_list(params, shape) |
| 69 | + elif isinstance(params, six.string_types) and name in self._type_dict: |
| 70 | + target_type = self._type_dict[shape.type_name] |
| 71 | + return self._coerce_field(params, target_type) |
| 72 | + return params |
| 73 | + |
| 74 | + def _coerce_structure(self, params, shape): |
| 75 | + members = shape.members |
| 76 | + coerced = {} |
| 77 | + for param in members: |
| 78 | + if param in params: |
| 79 | + coerced[param] = self.coerce(params[param], members[param]) |
| 80 | + return coerced |
| 81 | + |
| 82 | + def _coerce_map(self, params, shape): |
| 83 | + coerced = {} |
| 84 | + for key, value in params.items(): |
| 85 | + coerced_key = self.coerce(key, shape.key) |
| 86 | + coerced[coerced_key] = self.coerce(value, shape.value) |
| 87 | + return coerced |
| 88 | + |
| 89 | + def _coerce_list(self, list_param, shape): |
| 90 | + member_shape = shape.member |
| 91 | + coerced_list = [] |
| 92 | + for item in list_param: |
| 93 | + coerced_list.append(self.coerce(item, member_shape)) |
| 94 | + return coerced_list |
| 95 | + |
| 96 | + def _coerce_field(self, value, target_type): |
| 97 | + try: |
| 98 | + return target_type(value) |
| 99 | + except ValueError: |
| 100 | + return value |
| 101 | + |
| 102 | + |
21 | 103 | def stage_error_handler(error, stages, confirm=confirm, prompt=select_prompt):
|
22 | 104 | managed_errors = (
|
23 | 105 | ClientError,
|
@@ -264,6 +346,8 @@ def _handle_request_retrieval(self):
|
264 | 346 | self._env.resolve_parameters(req.get('EnvParameters', {}))
|
265 | 347 | # union of parameters and env_parameters, conflicts favor env params
|
266 | 348 | parameters = dict(parameters, **env_parameters)
|
| 349 | + model = client.meta.service_model.operation_model(req['Operation']) |
| 350 | + parameters = ParamCoercion().coerce(parameters, model.input_shape) |
267 | 351 | # if the operation supports pagination, load all results upfront
|
268 | 352 | if client.can_paginate(operation_name):
|
269 | 353 | # get paginator and create iterator
|
|
0 commit comments