Show More
@@ -1,91 +1,91 b'' | |||||
1 | from __future__ import print_function |
|
1 | from __future__ import print_function | |
2 | import json |
|
2 | import json | |
3 | import os |
|
3 | import os | |
4 |
|
4 | |||
5 | from jsonschema import Draft4Validator, SchemaError |
|
5 | from jsonschema import Draft4Validator, SchemaError | |
6 | import jsonpointer as jsonpointer |
|
6 | import jsonpointer as jsonpointer | |
7 | from IPython.utils.py3compat import iteritems |
|
7 | from IPython.utils.py3compat import iteritems | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | from .current import nbformat, nbformat_schema |
|
10 | from .current import nbformat, nbformat_schema | |
11 | schema_path = os.path.join( |
|
11 | schema_path = os.path.join( | |
12 | os.path.dirname(__file__), "v%d" % nbformat, nbformat_schema) |
|
12 | os.path.dirname(__file__), "v%d" % nbformat, nbformat_schema) | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | def isvalid(nbjson): |
|
15 | def isvalid(nbjson): | |
16 | """Checks whether the given notebook JSON conforms to the current |
|
16 | """Checks whether the given notebook JSON conforms to the current | |
17 | notebook format schema. Returns True if the JSON is valid, and |
|
17 | notebook format schema. Returns True if the JSON is valid, and | |
18 | False otherwise. |
|
18 | False otherwise. | |
19 |
|
19 | |||
20 | To see the individual errors that were encountered, please use the |
|
20 | To see the individual errors that were encountered, please use the | |
21 | `validate` function instead. |
|
21 | `validate` function instead. | |
22 |
|
22 | |||
23 | """ |
|
23 | """ | |
24 |
|
24 | |||
25 | errors = validate(nbjson) |
|
25 | errors = validate(nbjson) | |
26 | return errors == [] |
|
26 | return errors == [] | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def validate(nbjson): |
|
29 | def validate(nbjson): | |
30 | """Checks whether the given notebook JSON conforms to the current |
|
30 | """Checks whether the given notebook JSON conforms to the current | |
31 | notebook format schema, and returns the list of errors. |
|
31 | notebook format schema, and returns the list of errors. | |
32 |
|
32 | |||
33 | """ |
|
33 | """ | |
34 |
|
34 | |||
35 | # load the schema file |
|
35 | # load the schema file | |
36 | with open(schema_path, 'r') as fh: |
|
36 | with open(schema_path, 'r') as fh: | |
37 | schema_json = json.load(fh) |
|
37 | schema_json = json.load(fh) | |
38 |
|
38 | |||
39 | # resolve internal references |
|
39 | # resolve internal references | |
40 |
|
|
40 | schema = resolve_ref(schema_json) | |
41 |
|
|
41 | schema = jsonpointer.resolve_pointer(schema, '/notebook') | |
42 |
|
42 | |||
43 | # count how many errors there are |
|
43 | # count how many errors there are | |
44 |
v = Draft |
|
44 | v = Draft4Validator(schema) | |
45 | errors = list(v.iter_errors(nbjson)) |
|
45 | errors = list(v.iter_errors(nbjson)) | |
46 | return errors |
|
46 | return errors | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def resolve_ref(json, schema=None): |
|
49 | def resolve_ref(json, schema=None): | |
50 | """Resolve internal references within the given JSON. This essentially |
|
50 | """Resolve internal references within the given JSON. This essentially | |
51 | means that dictionaries of this form: |
|
51 | means that dictionaries of this form: | |
52 |
|
52 | |||
53 | {"$ref": "/somepointer"} |
|
53 | {"$ref": "/somepointer"} | |
54 |
|
54 | |||
55 | will be replaced with the resolved reference to `/somepointer`. |
|
55 | will be replaced with the resolved reference to `/somepointer`. | |
56 | This only supports local reference to the same JSON file. |
|
56 | This only supports local reference to the same JSON file. | |
57 |
|
57 | |||
58 | """ |
|
58 | """ | |
59 |
|
59 | |||
60 | if not schema: |
|
60 | if not schema: | |
61 | schema = json |
|
61 | schema = json | |
62 |
|
62 | |||
63 | # if it's a list, resolve references for each item in the list |
|
63 | # if it's a list, resolve references for each item in the list | |
64 | if type(json) is list: |
|
64 | if type(json) is list: | |
65 | resolved = [] |
|
65 | resolved = [] | |
66 | for item in json: |
|
66 | for item in json: | |
67 | resolved.append(resolve_ref(item, schema=schema)) |
|
67 | resolved.append(resolve_ref(item, schema=schema)) | |
68 |
|
68 | |||
69 | # if it's a dictionary, resolve references for each item in the |
|
69 | # if it's a dictionary, resolve references for each item in the | |
70 | # dictionary |
|
70 | # dictionary | |
71 | elif type(json) is dict: |
|
71 | elif type(json) is dict: | |
72 | resolved = {} |
|
72 | resolved = {} | |
73 | for key, ref in iteritems(json): |
|
73 | for key, ref in iteritems(json): | |
74 |
|
74 | |||
75 | # if the key is equal to $ref, then replace the entire |
|
75 | # if the key is equal to $ref, then replace the entire | |
76 | # dictionary with the resolved value |
|
76 | # dictionary with the resolved value | |
77 | if key == '$ref': |
|
77 | if key == '$ref': | |
78 | if len(json) != 1: |
|
78 | if len(json) != 1: | |
79 | raise SchemaError( |
|
79 | raise SchemaError( | |
80 | "objects containing a $ref should only have one item") |
|
80 | "objects containing a $ref should only have one item") | |
81 | pointer = jsonpointer.resolve_pointer(schema, ref) |
|
81 | pointer = jsonpointer.resolve_pointer(schema, ref) | |
82 | resolved = resolve_ref(pointer, schema=schema) |
|
82 | resolved = resolve_ref(pointer, schema=schema) | |
83 |
|
83 | |||
84 | else: |
|
84 | else: | |
85 | resolved[key] = resolve_ref(ref, schema=schema) |
|
85 | resolved[key] = resolve_ref(ref, schema=schema) | |
86 |
|
86 | |||
87 | # otherwise it's a normal object, so just return it |
|
87 | # otherwise it's a normal object, so just return it | |
88 | else: |
|
88 | else: | |
89 | resolved = json |
|
89 | resolved = json | |
90 |
|
90 | |||
91 | return resolved |
|
91 | return resolved |
General Comments 0
You need to be logged in to leave comments.
Login now