Exemple #1
0
def convert(namein, nameout, indent=2):
    """resolve the references of namein, save the result in nameout"""
    jsn = None
    with open(namein) as file:
        jsn = json.load(file)
    v = resolve_ref(jsn, base=jsn)
    x = jsonpointer.resolve_pointer(v, '/notebook')
    with open(nameout, 'w') as file:
        json.dump(x, file, indent=indent)
Exemple #2
0
def convert(namein, nameout, indent=2):
    """resolve the references of namein, save the result in nameout"""
    jsn = None
    with open(namein) as file :
        jsn = json.load(file)
    v = resolve_ref(jsn, base=jsn)
    x = jsonpointer.resolve_pointer(v, '/notebook')
    with open(nameout,'w') as file:
        json.dump(x,file,indent=indent)
Exemple #3
0
def nbvalidate(nbjson, schema='v3.withref.json', key=None, verbose=True):
    v3schema = resolve_ref(json.load(open(schema, 'r')))
    if key:
        v3schema = jsonpointer.resolve_pointer(v3schema, key)
    errors = 0
    v = Draft3Validator(v3schema)
    for error in v.iter_errors(nbjson):
        errors = errors + 1
        if verbose:
            print(error)
    return errors
Exemple #4
0
def nbvalidate(nbjson, schema='v3.withref.json', key=None,verbose=True):
    v3schema = resolve_ref(json.load(open(schema,'r')))
    if key :
        v3schema = jsonpointer.resolve_pointer(v3schema,key)
    errors = 0
    v = Draft3Validator(v3schema);
    for error in v.iter_errors(nbjson):
        errors = errors + 1
        if verbose:
            print(error)
    return errors
Exemple #5
0
def validate(nbjson):
    """Checks whether the given notebook JSON conforms to the current
    notebook format schema, and returns the list of errors.

    """

    # load the schema file
    with open(schema_path, 'r') as fh:
        schema_json = json.load(fh)

    # resolve internal references
    v3schema = resolve_ref(schema_json)
    v3schema = jsonpointer.resolve_pointer(v3schema, '/notebook')

    # count how many errors there are
    v = Draft3Validator(v3schema)
    errors = list(v.iter_errors(nbjson))
    return errors
Exemple #6
0
def validate(nbjson):
    """Checks whether the given notebook JSON conforms to the current
    notebook format schema, and returns the list of errors.

    """

    # load the schema file
    with open(schema_path, 'r') as fh:
        schema_json = json.load(fh)

    # resolve internal references
    v3schema = resolve_ref(schema_json)
    v3schema = jsonpointer.resolve_pointer(v3schema, '/notebook')

    # count how many errors there are
    v = Draft3Validator(v3schema)
    errors = list(v.iter_errors(nbjson))
    return errors
Exemple #7
0
def resolve_ref(json, schema=None):
    """Resolve internal references within the given JSON. This essentially
    means that dictionaries of this form:

    {"$ref": "/somepointer"}

    will be replaced with the resolved reference to `/somepointer`.
    This only supports local reference to the same JSON file.

    """

    if not schema:
        schema = json

    # if it's a list, resolve references for each item in the list
    if type(json) is list:
        resolved = []
        for item in json:
            resolved.append(resolve_ref(item, schema=schema))

    # if it's a dictionary, resolve references for each item in the
    # dictionary
    elif type(json) is dict:
        resolved = {}
        for key, ref in iteritems(json):

            # if the key is equal to $ref, then replace the entire
            # dictionary with the resolved value
            if key == '$ref':
                if len(json) != 1:
                    raise SchemaError(
                        "objects containing a $ref should only have one item")
                pointer = jsonpointer.resolve_pointer(schema, ref)
                resolved = resolve_ref(pointer, schema=schema)

            else:
                resolved[key] = resolve_ref(ref, schema=schema)

    # otherwise it's a normal object, so just return it
    else:
        resolved = json

    return resolved
Exemple #8
0
def resolve_ref(json, schema=None):
    """Resolve internal references within the given JSON. This essentially
    means that dictionaries of this form:

    {"$ref": "/somepointer"}

    will be replaced with the resolved reference to `/somepointer`.
    This only supports local reference to the same JSON file.

    """

    if not schema:
        schema = json

    # if it's a list, resolve references for each item in the list
    if type(json) is list:
        resolved = []
        for item in json:
            resolved.append(resolve_ref(item, schema=schema))

    # if it's a dictionary, resolve references for each item in the
    # dictionary
    elif type(json) is dict:
        resolved = {}
        for key, ref in iteritems(json):

            # if the key is equal to $ref, then replace the entire
            # dictionary with the resolved value
            if key == '$ref':
                if len(json) != 1:
                    raise SchemaError(
                        "objects containing a $ref should only have one item")
                pointer = jsonpointer.resolve_pointer(schema, ref)
                resolved = resolve_ref(pointer, schema=schema)

            else:
                resolved[key] = resolve_ref(ref, schema=schema)

    # otherwise it's a normal object, so just return it
    else:
        resolved = json

    return resolved
Exemple #9
0
def resolve_ref(json, base=None):
    """return a json with resolved internal references

    only support local reference to the same json
    """
    if not base :
        base = json

    if type(json) is list:
        temp = []
        for item in json:
            temp.append(resolve_ref(item, base=base))
    elif type(json) is dict:
        temp = {}
        for key, value in iteritems(json):
            if key == '$ref':
                return resolve_ref(jsonpointer.resolve_pointer(base, value), base=base)
            else:
                temp[key] = resolve_ref(value, base=base)
    else:
        return json
    return temp
Exemple #10
0
def resolve_ref(json, base=None):
    """return a json with resolved internal references

    only support local reference to the same json
    """
    if not base :
        base = json

    temp = None
    if type(json) is list:
        temp = [];
        for item in json:
            temp.append(resolve_ref(item, base=base))
    elif type(json) is dict:
        temp = {};
        for key,value in json.iteritems():
            if key == '$ref':
                return resolve_ref(jsonpointer.resolve_pointer(base,value), base=base)
            else :
                temp[key]=resolve_ref(value, base=base)
    else :
        return json
    return temp