Exemple #1
0
def reorder(object_type, data):
    """Converts dict to OrderedDict using schema order

    This reorders the data in a dict using the order the item is defined in the
    schema.

    :arg object_type: string specifying the type of object
    :arg json_data: the data to reorder

    :returns: data ordered according to the schema

    """
    schema = SCHEMAS[get_version(data)][object_type]

    def _reorder(data, schema):
        if isinstance(data, dict):
            new_dict = OrderedDict()
            for key, val in schema.keyvals:
                if key in data:
                    new_dict[key] = _reorder(data[key], schema[key])
            # FIXME: Add a check here for things that were in data that aren't
            # in the schema and print out warnings.
            return new_dict

        if isinstance(data, list):
            return [_reorder(item, schema.subtype) for item in data]

        return data

    return _reorder(data, schema)
Exemple #2
0
def validate_item(fn, json_data):
    # FIXME: This is kind of cheating. Need a better way to distinguish data
    # types.
    type_ = 'category' if fn.endswith('category.json') else 'video'
    vers = get_version(json_data)
    return SCHEMAS[vers][type_].validate(json_data, 'TOP')