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