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