Show More
@@ -6,7 +6,7 b' import json' | |||||
6 | import os |
|
6 | import os | |
7 |
|
7 | |||
8 | try: |
|
8 | try: | |
9 |
from jsonschema import |
|
9 | from jsonschema import ValidationError | |
10 | from jsonschema import Draft4Validator as Validator |
|
10 | from jsonschema import Draft4Validator as Validator | |
11 | except ImportError as e: |
|
11 | except ImportError as e: | |
12 | verbose_msg = """ |
|
12 | verbose_msg = """ | |
@@ -25,6 +25,19 b' schema_path = os.path.join(' | |||||
25 | os.path.dirname(__file__), nbformat_schema, |
|
25 | os.path.dirname(__file__), nbformat_schema, | |
26 | ) |
|
26 | ) | |
27 |
|
27 | |||
|
28 | validator = None | |||
|
29 | ||||
|
30 | def _load_schema(): | |||
|
31 | """Load the JSON schema into the global Validator""" | |||
|
32 | global validator | |||
|
33 | if validator is None: | |||
|
34 | # load the schema file | |||
|
35 | with open(schema_path, 'r') as fh: | |||
|
36 | schema_json = json.load(fh) | |||
|
37 | ||||
|
38 | # create the validator | |||
|
39 | validator = Validator(schema_json) | |||
|
40 | return validator | |||
28 |
|
41 | |||
29 | def isvalid(nbjson, ref=None): |
|
42 | def isvalid(nbjson, ref=None): | |
30 | """Checks whether the given notebook JSON conforms to the current |
|
43 | """Checks whether the given notebook JSON conforms to the current | |
@@ -34,33 +47,23 b' def isvalid(nbjson, ref=None):' | |||||
34 | To see the individual errors that were encountered, please use the |
|
47 | To see the individual errors that were encountered, please use the | |
35 | `validate` function instead. |
|
48 | `validate` function instead. | |
36 | """ |
|
49 | """ | |
37 |
|
||||
38 | it = validate(nbjson, ref) |
|
|||
39 | try: |
|
50 | try: | |
40 | it.next() |
|
51 | validate(nbjson, ref) | |
41 |
except |
|
52 | except ValidationError: | |
42 | return True |
|
|||
43 | else: |
|
|||
44 | return False |
|
53 | return False | |
|
54 | else: | |||
|
55 | return True | |||
45 |
|
56 | |||
46 |
|
57 | |||
47 | def validate(nbjson, ref=None): |
|
58 | def validate(nbjson, ref=None): | |
48 | """Checks whether the given notebook JSON conforms to the current |
|
59 | """Checks whether the given notebook JSON conforms to the current | |
49 | notebook format schema. |
|
60 | notebook format schema. | |
50 |
|
61 | |||
51 | Returns a generator for errors. |
|
62 | Raises ValidationError if not valid. | |
52 | """ |
|
63 | """ | |
|
64 | _load_schema() | |||
53 |
|
65 | |||
54 | # load the schema file |
|
|||
55 | with open(schema_path, 'r') as fh: |
|
|||
56 | schema_json = json.load(fh) |
|
|||
57 |
|
||||
58 | # create the validator |
|
|||
59 | v = Validator(schema_json) |
|
|||
60 |
|
||||
61 | # return the iterator on errors |
|
|||
62 | if ref: |
|
66 | if ref: | |
63 |
|
|
67 | return validator.validate(nbjson, {'$ref' : '#/definitions/%s' % ref}) | |
64 | else: |
|
68 | else: | |
65 |
|
|
69 | return validator.validate(nbjson) | |
66 | return errors |
|
General Comments 0
You need to be logged in to leave comments.
Login now