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