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