Show More
@@ -10,47 +10,64 b' from IPython.utils.py3compat import iteritems' | |||||
10 |
|
10 | |||
11 |
|
11 | |||
12 | from .current import nbformat, nbformat_schema |
|
12 | from .current import nbformat, nbformat_schema | |
13 | schema = os.path.join( |
|
13 | schema_path = os.path.join( | |
14 | os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema) |
|
14 | os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema) | |
15 |
|
15 | |||
16 |
|
16 | |||
17 |
def validate(nbjson, key=' |
|
17 | def validate(nbjson, key='', verbose=True): | |
18 | v3schema = resolve_ref(json.load(open(schema, 'r'))) |
|
18 | # load the schema file | |
19 | if key: |
|
19 | with open(schema_path, 'r') as fh: | |
20 | v3schema = jsonpointer.resolve_pointer(v3schema, key) |
|
20 | schema_json = json.load(fh) | |
|
21 | ||||
|
22 | # resolve internal references | |||
|
23 | v3schema = resolve_ref(schema_json) | |||
|
24 | v3schema = jsonpointer.resolve_pointer(v3schema, key) | |||
|
25 | ||||
21 | errors = 0 |
|
26 | errors = 0 | |
22 | v = Draft3Validator(v3schema) |
|
27 | v = Draft3Validator(v3schema) | |
23 | for error in v.iter_errors(nbjson): |
|
28 | for error in v.iter_errors(nbjson): | |
24 | errors = errors + 1 |
|
29 | errors = errors + 1 | |
25 | if verbose: |
|
30 | if verbose: | |
26 | print(error) |
|
31 | print(error) | |
|
32 | ||||
27 | return errors |
|
33 | return errors | |
28 |
|
34 | |||
29 |
|
35 | |||
30 |
def resolve_ref(json, |
|
36 | def resolve_ref(json, schema=None): | |
31 | """return a json with resolved internal references |
|
37 | """return a json with resolved internal references | |
32 |
|
38 | |||
33 | only support local reference to the same json |
|
39 | only support local reference to the same json | |
34 | """ |
|
40 | """ | |
35 | if not base: |
|
|||
36 | base = json |
|
|||
37 |
|
41 | |||
38 | temp = None |
|
42 | if not schema: | |
|
43 | schema = json | |||
|
44 | ||||
|
45 | # if it's a list, resolve references for each item in the list | |||
39 | if type(json) is list: |
|
46 | if type(json) is list: | |
40 |
|
|
47 | resolved = [] | |
41 | for item in json: |
|
48 | for item in json: | |
42 |
|
|
49 | resolved.append(resolve_ref(item, schema=schema)) | |
|
50 | ||||
|
51 | # if it's a dictionary, resolve references for each item in the | |||
|
52 | # dictionary | |||
43 | elif type(json) is dict: |
|
53 | elif type(json) is dict: | |
44 |
|
|
54 | resolved = {} | |
45 |
for key, |
|
55 | for key, ref in iteritems(json): | |
|
56 | # if the key is equal to $ref, then replace the entire | |||
|
57 | # dictionary with the resolved value | |||
46 | if key == '$ref': |
|
58 | if key == '$ref': | |
47 | return resolve_ref( |
|
59 | assert len(json) == 1 | |
48 |
|
|
60 | pointer = jsonpointer.resolve_pointer(schema, ref) | |
|
61 | resolved = resolve_ref(pointer, schema=schema) | |||
|
62 | ||||
49 | else: |
|
63 | else: | |
50 |
|
|
64 | resolved[key] = resolve_ref(ref, schema=schema) | |
|
65 | ||||
|
66 | # otherwise it's a normal object, so just return it | |||
51 | else: |
|
67 | else: | |
52 |
re |
|
68 | resolved = json | |
53 | return temp |
|
69 | ||
|
70 | return resolved | |||
54 |
|
71 | |||
55 |
|
72 | |||
56 | def convert(namein, nameout, indent=2): |
|
73 | def convert(namein, nameout, indent=2): | |
@@ -58,7 +75,7 b' def convert(namein, nameout, indent=2):' | |||||
58 | jsn = None |
|
75 | jsn = None | |
59 | with open(namein) as file: |
|
76 | with open(namein) as file: | |
60 | jsn = json.load(file) |
|
77 | jsn = json.load(file) | |
61 |
v = resolve_ref(jsn |
|
78 | v = resolve_ref(jsn) | |
62 | x = jsonpointer.resolve_pointer(v, '/notebook') |
|
79 | x = jsonpointer.resolve_pointer(v, '/notebook') | |
63 | with open(nameout, 'w') as file: |
|
80 | with open(nameout, 'w') as file: | |
64 | json.dump(x, file, indent=indent) |
|
81 | json.dump(x, file, indent=indent) |
General Comments 0
You need to be logged in to leave comments.
Login now