示例#1
0
def _set_default_object_properties(properties, instance, include_yaml_comments,
                                   yaml_indent):
    """
    Helper for extend_with_default_without_validation().
    Inject default values for the given object properties on the given instance.
    """
    for property_name, subschema in properties.items():
        if instance == "{{NO_DEFAULT}}":
            continue

        if "default" in subschema:
            default = copy.deepcopy(subschema["default"])

            if isinstance(default, list):
                try:
                    # Lists of numbers should use 'flow style'
                    # and so should lists-of-lists of numbers
                    # (e.g. bounding boxes like [[0,0,0],[1,2,3]])
                    if (subschema["items"]["type"] in ("integer", "number")
                            or (subschema["items"]["type"] == "array"
                                and subschema["items"]["items"]["type"]
                                in ("integer", "number"))):
                        default = flow_style(default)
                except KeyError:
                    pass

            if include_yaml_comments and isinstance(default, dict):
                default = CommentedMap(default)
                # To keep track of the current indentation level,
                # we just monkey-patch this member onto the dict.
                default.key_indent = instance.key_indent + yaml_indent
                default.from_default = True

            if include_yaml_comments and isinstance(default, list):
                if not isinstance(default, CommentedSeq):
                    default = CommentedSeq(copy.copy(default))

                # To keep track of the current indentation level,
                # we just monkey-patch this member onto the dict.
                default.key_indent = instance.key_indent + yaml_indent
                default.from_default = True

            if isinstance(instance, Mapping) and property_name not in instance:
                instance[property_name] = default
        else:
            if isinstance(instance, Mapping) and property_name not in instance:
                instance[property_name] = "{{NO_DEFAULT}}"

        if include_yaml_comments and "description" in subschema:
            comment = '\n' + subschema["description"]
            if comment[-1] == '\n':
                comment = comment[:-1]
            instance.yaml_set_comment_before_after_key(property_name, comment,
                                                       instance.key_indent)
示例#2
0
    def set_default_object_properties(validator, properties, instance, schema):
        for property, subschema in properties.items():
            if instance == "{{NO_DEFAULT}}":
                continue
            if "default" in subschema:
                default = copy.deepcopy(subschema["default"])

                if isinstance(default, list):
                    try:
                        # Lists of numbers should use 'flow style'
                        # and so should lists-of-lists of numbers
                        # (e.g. bounding boxes like [[0,0,0],[1,2,3]])
                        if (subschema["items"]["type"] in ("integer", "number")
                                or (subschema["items"]["type"] == "array"
                                    and subschema["items"]["items"]["type"]
                                    in ("integer", "number"))):
                            default = flow_style(default)
                    except KeyError:
                        pass

                if include_yaml_comments and isinstance(default, dict):
                    default = CommentedMap(default)
                    # To keep track of the current indentation level,
                    # we just monkey-patch this member onto the dict.
                    default.key_indent = instance.key_indent + yaml_indent
                    default.from_default = True
                if include_yaml_comments and isinstance(default, list):
                    if not isinstance(default, CommentedSeq):
                        default = CommentedSeq(copy.copy(default))

                    # To keep track of the current indentation level,
                    # we just monkey-patch this member onto the dict.
                    default.key_indent = instance.key_indent + yaml_indent
                    default.from_default = True
                if property not in instance:
                    instance[property] = default
            else:
                if property not in instance:
                    instance[property] = "{{NO_DEFAULT}}"

            if include_yaml_comments and "description" in subschema:
                comment = '\n' + subschema["description"]
                if comment[-1] == '\n':
                    comment = comment[:-1]
                instance.yaml_set_comment_before_after_key(
                    property, comment, instance.key_indent)

        for _error in validate_properties(validator, properties, instance,
                                          schema):
            # Ignore validation errors
            pass
    def set_default_object_properties(validator, properties, instance, schema):
        for property, subschema in properties.items():
            if instance == "{{NO_DEFAULT}}":
                continue
            if "default" in subschema:
                default = copy.deepcopy(subschema["default"])
                
                if isinstance(default, list):
                    try:
                        # Lists of numbers should use 'flow style'
                        # and so should lists-of-lists of numbers
                        # (e.g. bounding boxes like [[0,0,0],[1,2,3]])
                        if ( subschema["items"]["type"] in ("integer", "number") or
                             ( subschema["items"]["type"] == "array" and 
                               subschema["items"]["items"]["type"] in ("integer", "number") ) ):
                            default = flow_style(default)
                    except KeyError:
                        pass
                
                if include_yaml_comments and isinstance(default, dict):
                    default = CommentedMap(default)
                    # To keep track of the current indentation level,
                    # we just monkey-patch this member onto the dict.
                    default.key_indent = instance.key_indent + yaml_indent
                    default.from_default = True
                if include_yaml_comments and isinstance(default, list):
                    if not isinstance(default, CommentedSeq):
                        default = CommentedSeq(copy.copy(default))
                    
                    # To keep track of the current indentation level,
                    # we just monkey-patch this member onto the dict.
                    default.key_indent = instance.key_indent + yaml_indent
                    default.from_default = True
                if property not in instance:
                    instance[property] = default
            else:
                if property not in instance:
                    instance[property] = "{{NO_DEFAULT}}"

            if include_yaml_comments and "description" in subschema:
                comment = '\n' + subschema["description"]
                if comment[-1] == '\n':
                    comment = comment[:-1]
                instance.yaml_set_comment_before_after_key(property, comment, instance.key_indent)

        for _error in validate_properties(validator, properties, instance, schema):
            # Ignore validation errors
            pass