ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran-z <...@git.apache.org>
Subject [GitHub] incubator-ariatosca pull request #97: Aria 48 aria cli
Date Thu, 13 Apr 2017 14:28:24 GMT
Github user ran-z commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/97#discussion_r111402765
  
    --- Diff: aria/modeling/utils.py ---
    @@ -13,12 +13,99 @@
     # See the License for the specific language governing permissions and
     # limitations under the License.
     
    +from json import JSONEncoder
    +from StringIO import StringIO
    +
    +from . import exceptions
     from ..parser.consumption import ConsumptionContext
     from ..parser.exceptions import InvalidValueError
     from ..parser.presentation import Value
     from ..utils.collections import OrderedDict
     from ..utils.console import puts
    -from .exceptions import CannotEvaluateFunctionException
    +from ..utils.type import validate_value_type
    +
    +
    +class ModelJSONEncoder(JSONEncoder):
    +    def default(self, o):  # pylint: disable=method-hidden
    +        from .mixins import ModelMixin
    +        if isinstance(o, ModelMixin):
    +            if hasattr(o, 'value'):
    +                dict_to_return = o.to_dict(fields=('value',))
    +                return dict_to_return['value']
    +            else:
    +                return o.to_dict()
    +        else:
    +            return JSONEncoder.default(self, o)
    +
    +
    +def create_inputs(inputs, template_inputs):
    +    """
    +    :param inputs: key-value dict
    +    :param template_inputs: parameter name to parameter object dict
    +    :return: dict of parameter name to Parameter models
    +    """
    +    merged_inputs = _merge_and_validate_inputs(inputs, template_inputs)
    +
    +    from . import models
    +    input_models = []
    +    for input_name, input_val in merged_inputs.iteritems():
    +        parameter = models.Parameter(
    +            name=input_name,
    +            type_name=template_inputs[input_name].type_name,
    +            description=template_inputs[input_name].description,
    +            value=input_val)
    +        input_models.append(parameter)
    +
    +    return dict((inp.name, inp) for inp in input_models)
    +
    +
    +def _merge_and_validate_inputs(inputs, template_inputs):
    +    """
    +    :param inputs: key-value dict
    +    :param template_inputs: parameter name to parameter object dict
    +    :return:
    +    """
    +    merged_inputs = inputs.copy()
    +
    +    missing_inputs = []
    +    wrong_type_inputs = {}
    +    for input_name, input_template in template_inputs.iteritems():
    +        if input_name not in inputs:
    +            if input_template.value is not None:
    +                merged_inputs[input_name] = input_template.value  # apply default value
    +            else:
    +                missing_inputs.append(input_name)
    +        else:
    +            # Validate input type
    +            try:
    +                validate_value_type(inputs[input_name], input_template.type_name)
    +            except ValueError:
    +                wrong_type_inputs[input_name] = input_template.type_name
    +            except RuntimeError:
    +                # TODO: This error shouldn't be raised (or caught), but right now we
lack support
    +                # for custom data_types, which will raise this error. Skipping their
validation.
    +                pass
    +
    +    if missing_inputs:
    +        raise exceptions.MissingRequiredInputsException(
    +            'Required inputs {0} have not been specified - expected inputs: {1}'
    +            .format(missing_inputs, template_inputs.keys()))
    +
    +    if wrong_type_inputs:
    +        error_message = StringIO()
    +        for param_name, param_type in wrong_type_inputs.iteritems():
    +            error_message.write('Input "{0}" must be of type {1}\n'.
    --- End diff --
    
    :+1: 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message