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