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