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