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