예제 #1
0
def get_assigned_and_defined_parameter_values(context, presentation,
                                              field_name):
    """
    Returns the assigned property values while making sure they are defined in our type.

    The property definition's default value, if available, will be used if we did not assign it.

    Makes sure that required properties indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    field_name_plural = pluralize(field_name)
    assignments = getattr(presentation, field_name_plural)
    get_fn_name = '_get_{0}'.format(field_name_plural)
    definitions = getattr(
        the_type, get_fn_name)(context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                values[name] = coerce_parameter_value(context, value,
                                                      definition, value.value)
            else:
                context.validation.report(
                    'assignment to undefined {0} "{1}" in "{2}"'.format(
                        field_name, name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            if values.get(name) is None:
                values[name] = coerce_parameter_value(context, presentation,
                                                      definition,
                                                      definition.default)

    validate_required_values(context, presentation, values, definitions)

    return values
예제 #2
0
def get_assigned_and_defined_property_values(context, presentation):
    """
    Returns the assigned property values while making sure they are defined in our type.

    The property definition's default value, if available, will be used if we did not assign it.

    Makes sure that required properties indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    assignments = presentation.properties
    definitions = the_type._get_properties(
        context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                values[name] = coerce_property_value(context, value,
                                                     definition, value.value)
            else:
                context.validation.report(
                    'assignment to undefined property "%s" in "%s"' %
                    (name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            if (values.get(name) is None) and (definition.default is not None):
                values[name] = coerce_property_value(context, presentation,
                                                     definition,
                                                     definition.default)

    validate_required_values(context, presentation, values, definitions)

    return values
예제 #3
0
def get_parameter_values(context, presentation, field_name):
    values = OrderedDict()

    parameters = getattr(presentation, field_name)

    # Fill in defaults and values
    if parameters:
        for name, parameter in parameters.iteritems():
            if values.get(name) is None:
                if hasattr(parameter, 'value') and (parameter.value
                                                    is not None):
                    # For parameters only:
                    values[name] = coerce_parameter_value(
                        context, presentation, parameter, parameter.value)
                else:
                    default = parameter.default if hasattr(
                        parameter, 'default') else None
                    values[name] = coerce_parameter_value(
                        context, presentation, parameter, default, 'default')

    return values
예제 #4
0
def coerce_data_type_value(context, presentation, data_type, entry_schema, constraints, value, # pylint: disable=unused-argument
                           aspect):
    """
    Handles the :code:`_coerce_data()` hook for complex data types.

    There are two kinds of handling:

    1. If we have a primitive type as our great ancestor, then we do primitive type coersion, and
       just check for constraints.

    2. Otherwise, for normal complex data types we return the assigned property values while making
       sure they are defined in our type. The property definition's default value, if available,
       will be used if we did not assign it. We also make sure that required definitions indeed end
       up with a value.
    """

    primitive_type = data_type._get_primitive_ancestor(context)
    if primitive_type is not None:
        # Must be coercible to primitive ancestor
        value = coerce_to_primitive(context, presentation, primitive_type, constraints, value,
                                    aspect)
    else:
        definitions = data_type._get_properties(context)
        if isinstance(value, dict):
            temp = OrderedDict()

            # Fill in our values, but make sure they are defined
            for name, v in value.iteritems():
                if name in definitions:
                    definition = definitions[name]
                    definition_type = definition._get_type(context)
                    definition_entry_schema = definition.entry_schema
                    definition_constraints = definition._get_constraints(context)
                    temp[name] = coerce_value(context, presentation, definition_type,
                                              definition_entry_schema, definition_constraints, v,
                                              aspect)
                else:
                    context.validation.report(
                        'assignment to undefined property "%s" in type "%s" in "%s"'
                        % (name, data_type._fullname, presentation._fullname),
                        locator=get_locator(v, value, presentation), level=Issue.BETWEEN_TYPES)

            # Fill in defaults from the definitions, and check if required definitions have not been
            # assigned
            for name, definition in definitions.iteritems():
                if (temp.get(name) is None) and hasattr(definition, 'default') \
                    and (definition.default is not None):
                    definition_type = definition._get_type(context)
                    definition_entry_schema = definition.entry_schema
                    definition_constraints = definition._get_constraints(context)
                    temp[name] = coerce_value(context, presentation, definition_type,
                                              definition_entry_schema, definition_constraints,
                                              definition.default, 'default')

                if getattr(definition, 'required', False) and (temp.get(name) is None):
                    context.validation.report(
                        'required property "%s" in type "%s" is not assigned a value in "%s"'
                        % (name, data_type._fullname, presentation._fullname),
                        locator=presentation._get_child_locator('definitions'),
                        level=Issue.BETWEEN_TYPES)

            value = temp
        elif value is not None:
            context.validation.report('value of type "%s" is not a dict in "%s"'
                                      % (data_type._fullname, presentation._fullname),
                                      locator=get_locator(value, presentation),
                                      level=Issue.BETWEEN_TYPES)
            value = None

    return value