Show More
@@ -0,0 +1,344 b'' | |||||
|
1 | { | |||
|
2 | "$schema": "http://json-schema.org/draft-04/schema#", | |||
|
3 | "description": "IPython Notebook v4.0 JSON schema.", | |||
|
4 | "type": "object", | |||
|
5 | "additionalProperties": false, | |||
|
6 | "required": ["metadata", "nbformat_minor", "nbformat", "cells"], | |||
|
7 | "properties": { | |||
|
8 | "metadata": { | |||
|
9 | "description": "Notebook root-level metadata.", | |||
|
10 | "type": "object", | |||
|
11 | "additionalProperties": true, | |||
|
12 | "properties": { | |||
|
13 | "kernel_info": { | |||
|
14 | "description": "Kernel information.", | |||
|
15 | "type": "object", | |||
|
16 | "required": ["name", "language"], | |||
|
17 | "properties": { | |||
|
18 | "name": { | |||
|
19 | "description": "Name of the kernel specification.", | |||
|
20 | "type": "string" | |||
|
21 | }, | |||
|
22 | "language": { | |||
|
23 | "description": "The programming language which this kernel runs.", | |||
|
24 | "type": "string" | |||
|
25 | }, | |||
|
26 | "codemirror_mode": { | |||
|
27 | "description": "The codemirror mode to use for code in this language.", | |||
|
28 | "type": "string" | |||
|
29 | } | |||
|
30 | } | |||
|
31 | }, | |||
|
32 | "signature": { | |||
|
33 | "description": "Hash of the notebook.", | |||
|
34 | "type": "string" | |||
|
35 | }, | |||
|
36 | "orig_nbformat": { | |||
|
37 | "description": "Original notebook format (major number) before converting the notebook between versions.", | |||
|
38 | "type": "integer", | |||
|
39 | "minimum": 1 | |||
|
40 | } | |||
|
41 | } | |||
|
42 | }, | |||
|
43 | "nbformat_minor": { | |||
|
44 | "description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.", | |||
|
45 | "type": "integer", | |||
|
46 | "minimum": 0 | |||
|
47 | }, | |||
|
48 | "nbformat": { | |||
|
49 | "description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.", | |||
|
50 | "type": "integer", | |||
|
51 | "minimum": 4, | |||
|
52 | "maximum": 4 | |||
|
53 | }, | |||
|
54 | "cells": { | |||
|
55 | "description": "Array of cells of the current notebook.", | |||
|
56 | "type": "array", | |||
|
57 | "items": { | |||
|
58 | "type": "object", | |||
|
59 | "oneOf": [ | |||
|
60 | {"$ref": "#/definitions/raw_cell"}, | |||
|
61 | {"$ref": "#/definitions/markdown_cell"}, | |||
|
62 | {"$ref": "#/definitions/heading_cell"}, | |||
|
63 | {"$ref": "#/definitions/code_cell"} | |||
|
64 | ] | |||
|
65 | } | |||
|
66 | } | |||
|
67 | }, | |||
|
68 | ||||
|
69 | "definitions": { | |||
|
70 | ||||
|
71 | "raw_cell": { | |||
|
72 | "description": "Notebook raw nbconvert cell.", | |||
|
73 | "type": "object", | |||
|
74 | "additionalProperties": false, | |||
|
75 | "required": ["cell_type", "metadata"], | |||
|
76 | "properties": { | |||
|
77 | "cell_type": { | |||
|
78 | "description": "String identifying the type of cell.", | |||
|
79 | "enum": ["raw"] | |||
|
80 | }, | |||
|
81 | "metadata": { | |||
|
82 | "description": "Cell-level metadata.", | |||
|
83 | "type": "object", | |||
|
84 | "additionalProperties": true, | |||
|
85 | "properties": { | |||
|
86 | "format": { | |||
|
87 | "description": "Raw cell metadata format for nbconvert.", | |||
|
88 | "type": "string" | |||
|
89 | }, | |||
|
90 | "name": {"$ref": "#/definitions/misc/metadata_name"}, | |||
|
91 | "tags": {"$ref": "#/definitions/misc/metadata_tags"} | |||
|
92 | } | |||
|
93 | }, | |||
|
94 | "source": {"$ref": "#/definitions/misc/source"} | |||
|
95 | } | |||
|
96 | }, | |||
|
97 | ||||
|
98 | "markdown_cell": { | |||
|
99 | "description": "Notebook markdown cell.", | |||
|
100 | "type": "object", | |||
|
101 | "additionalProperties": false, | |||
|
102 | "required": ["cell_type", "metadata"], | |||
|
103 | "properties": { | |||
|
104 | "cell_type": { | |||
|
105 | "description": "String identifying the type of cell.", | |||
|
106 | "enum": ["markdown"] | |||
|
107 | }, | |||
|
108 | "metadata": { | |||
|
109 | "description": "Cell-level metadata.", | |||
|
110 | "type": "object", | |||
|
111 | "properties": { | |||
|
112 | "name": {"$ref": "#/definitions/misc/metadata_name"}, | |||
|
113 | "tags": {"$ref": "#/definitions/misc/metadata_tags"} | |||
|
114 | }, | |||
|
115 | "additionalProperties": true | |||
|
116 | }, | |||
|
117 | "source": {"$ref": "#/definitions/misc/source"} | |||
|
118 | } | |||
|
119 | }, | |||
|
120 | ||||
|
121 | "heading_cell": { | |||
|
122 | "description": "Notebook heading cell.", | |||
|
123 | "type": "object", | |||
|
124 | "additionalProperties": false, | |||
|
125 | "required": ["cell_type", "metadata", "source", "level"], | |||
|
126 | "properties": { | |||
|
127 | "cell_type": { | |||
|
128 | "description": "String identifying the type of cell.", | |||
|
129 | "enum": ["heading"] | |||
|
130 | }, | |||
|
131 | "metadata": { | |||
|
132 | "description": "Cell-level metadata.", | |||
|
133 | "type": "object", | |||
|
134 | "properties": { | |||
|
135 | "name": {"$ref": "#/definitions/misc/metadata_name"}, | |||
|
136 | "tags": {"$ref": "#/definitions/misc/metadata_tags"} | |||
|
137 | }, | |||
|
138 | "additionalProperties": true | |||
|
139 | }, | |||
|
140 | "source": {"$ref": "#/definitions/misc/source"}, | |||
|
141 | "level": { | |||
|
142 | "description": "Level of heading cells.", | |||
|
143 | "type": "integer", | |||
|
144 | "minimum": 1, | |||
|
145 | "maximum": 6 | |||
|
146 | } | |||
|
147 | } | |||
|
148 | }, | |||
|
149 | ||||
|
150 | "code_cell": { | |||
|
151 | "description": "Notebook code cell.", | |||
|
152 | "type": "object", | |||
|
153 | "additionalProperties": false, | |||
|
154 | "required": ["cell_type", "metadata", "source", "outputs", "prompt_number"], | |||
|
155 | "properties": { | |||
|
156 | "cell_type": { | |||
|
157 | "description": "String identifying the type of cell.", | |||
|
158 | "enum": ["code"] | |||
|
159 | }, | |||
|
160 | "metadata": { | |||
|
161 | "description": "Cell-level metadata.", | |||
|
162 | "type": "object", | |||
|
163 | "additionalProperties": true, | |||
|
164 | "properties": { | |||
|
165 | "collapsed": { | |||
|
166 | "description": "Whether the cell is collapsed/expanded.", | |||
|
167 | "type": "boolean" | |||
|
168 | }, | |||
|
169 | "autoscroll": { | |||
|
170 | "description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.", | |||
|
171 | "enum": [true, false, "auto"] | |||
|
172 | }, | |||
|
173 | "name": {"$ref": "#/definitions/misc/metadata_name"}, | |||
|
174 | "tags": {"$ref": "#/definitions/misc/metadata_tags"} | |||
|
175 | } | |||
|
176 | }, | |||
|
177 | "source": {"$ref": "#/definitions/misc/source"}, | |||
|
178 | "outputs": { | |||
|
179 | "description": "Execution, display, or stream outputs.", | |||
|
180 | "type": "array", | |||
|
181 | "items": { | |||
|
182 | "type": "object", | |||
|
183 | "oneOf": [ | |||
|
184 | {"$ref": "#/definitions/execute_result"}, | |||
|
185 | {"$ref": "#/definitions/display_data_output"}, | |||
|
186 | {"$ref": "#/definitions/stream_output"}, | |||
|
187 | {"$ref": "#/definitions/error_output"} | |||
|
188 | ] | |||
|
189 | } | |||
|
190 | }, | |||
|
191 | "prompt_number": { | |||
|
192 | "description": "The code cell's prompt number. Will be null if the cell has not been run.", | |||
|
193 | "type": ["integer", "null"], | |||
|
194 | "minimum": 0 | |||
|
195 | } | |||
|
196 | } | |||
|
197 | }, | |||
|
198 | ||||
|
199 | "execute_result": { | |||
|
200 | "description": "Result of executing a code cell.", | |||
|
201 | "type": "object", | |||
|
202 | "additionalProperties": false, | |||
|
203 | "required": ["output_type", "metadata", "prompt_number"], | |||
|
204 | "properties": { | |||
|
205 | "output_type": { | |||
|
206 | "description": "Type of cell output.", | |||
|
207 | "enum": ["execute_result"] | |||
|
208 | }, | |||
|
209 | "prompt_number": { | |||
|
210 | "description": "A result's prompt number.", | |||
|
211 | "type": ["integer"], | |||
|
212 | "minimum": 0 | |||
|
213 | }, | |||
|
214 | "metadata": {"$ref": "#/definitions/misc/output_metadata"}, | |||
|
215 | "application/json": { | |||
|
216 | "type": "object" | |||
|
217 | } | |||
|
218 | }, | |||
|
219 | "patternProperties": { | |||
|
220 | "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": { | |||
|
221 | "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.", | |||
|
222 | "$ref": "#/definitions/misc/multiline_string" | |||
|
223 | } | |||
|
224 | } | |||
|
225 | }, | |||
|
226 | ||||
|
227 | "display_data_output": { | |||
|
228 | "description": "Data displayed as a result of code cell execution.", | |||
|
229 | "type": "object", | |||
|
230 | "additionalProperties": false, | |||
|
231 | "required": ["output_type", "metadata"], | |||
|
232 | "properties": { | |||
|
233 | "output_type": { | |||
|
234 | "description": "Type of cell output.", | |||
|
235 | "enum": ["display_data"] | |||
|
236 | }, | |||
|
237 | "metadata": {"$ref": "#/definitions/misc/output_metadata"} | |||
|
238 | }, | |||
|
239 | "patternProperties": { | |||
|
240 | "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": { | |||
|
241 | "description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.", | |||
|
242 | "$ref": "#/definitions/misc/multiline_string" | |||
|
243 | } | |||
|
244 | } | |||
|
245 | }, | |||
|
246 | ||||
|
247 | "stream_output": { | |||
|
248 | "description": "Stream output from a code cell.", | |||
|
249 | "type": "object", | |||
|
250 | "additionalProperties": false, | |||
|
251 | "required": ["output_type", "metadata", "stream", "text"], | |||
|
252 | "properties": { | |||
|
253 | "output_type": { | |||
|
254 | "description": "Type of cell output.", | |||
|
255 | "enum": ["stream"] | |||
|
256 | }, | |||
|
257 | "metadata": {"$ref": "#/definitions/misc/output_metadata"}, | |||
|
258 | "stream": { | |||
|
259 | "description": "The stream type/destination.", | |||
|
260 | "type": "string" | |||
|
261 | }, | |||
|
262 | "text": { | |||
|
263 | "description": "The stream's text output, represented as an array of strings.", | |||
|
264 | "$ref": "#/definitions/misc/multiline_string" | |||
|
265 | } | |||
|
266 | } | |||
|
267 | }, | |||
|
268 | ||||
|
269 | "error_output": { | |||
|
270 | "description": "Output of an error that occurred during code cell execution.", | |||
|
271 | "type": "object", | |||
|
272 | "additionalProperties": false, | |||
|
273 | "required": ["output_type", "metadata", "ename", "evalue", "traceback"], | |||
|
274 | "properties": { | |||
|
275 | "output_type": { | |||
|
276 | "description": "Type of cell output.", | |||
|
277 | "enum": ["error"] | |||
|
278 | }, | |||
|
279 | "metadata": {"$ref": "#/definitions/misc/output_metadata"}, | |||
|
280 | "ename": { | |||
|
281 | "description": "The name of the error.", | |||
|
282 | "type": "string" | |||
|
283 | }, | |||
|
284 | "evalue": { | |||
|
285 | "description": "The value, or message, of the error.", | |||
|
286 | "type": "string" | |||
|
287 | }, | |||
|
288 | "traceback": { | |||
|
289 | "description": "The error's traceback, represented as an array of strings.", | |||
|
290 | "type": "array", | |||
|
291 | "items": {"type": "string"} | |||
|
292 | } | |||
|
293 | } | |||
|
294 | }, | |||
|
295 | ||||
|
296 | "misc": { | |||
|
297 | "metadata_name": { | |||
|
298 | "description": "The cell's name. If present, must be a non-empty string.", | |||
|
299 | "type": "string", | |||
|
300 | "pattern": "^.+$" | |||
|
301 | }, | |||
|
302 | "metadata_tags": { | |||
|
303 | "description": "The cell's tags. Tags must be unique, and must not contain commas.", | |||
|
304 | "type": "array", | |||
|
305 | "uniqueItems": true, | |||
|
306 | "items": { | |||
|
307 | "type": "string", | |||
|
308 | "pattern": "^[^,]+$" | |||
|
309 | } | |||
|
310 | }, | |||
|
311 | "source": { | |||
|
312 | "description": "Contents of the cell, represented as an array of lines.", | |||
|
313 | "$ref": "#/definitions/misc/multiline_string" | |||
|
314 | }, | |||
|
315 | "prompt_number": { | |||
|
316 | "description": "The code cell's prompt number. Will be null if the cell has not been run.", | |||
|
317 | "type": ["integer", "null"], | |||
|
318 | "minimum": 0 | |||
|
319 | }, | |||
|
320 | "mimetype": { | |||
|
321 | "patternProperties": { | |||
|
322 | "^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": { | |||
|
323 | "description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.", | |||
|
324 | "$ref": "#/definitions/misc/multiline_string" | |||
|
325 | } | |||
|
326 | } | |||
|
327 | }, | |||
|
328 | "output_metadata": { | |||
|
329 | "description": "Cell output metadata.", | |||
|
330 | "type": "object", | |||
|
331 | "additionalProperties": true | |||
|
332 | }, | |||
|
333 | "multiline_string": { | |||
|
334 | "oneOf" : [ | |||
|
335 | {"type": "string"}, | |||
|
336 | { | |||
|
337 | "type": "array", | |||
|
338 | "items": {"type": "string"} | |||
|
339 | } | |||
|
340 | ] | |||
|
341 | } | |||
|
342 | } | |||
|
343 | } | |||
|
344 | } |
@@ -0,0 +1,66 b'' | |||||
|
1 | # Copyright (c) IPython Development Team. | |||
|
2 | # Distributed under the terms of the Modified BSD License. | |||
|
3 | ||||
|
4 | from __future__ import print_function | |||
|
5 | import json | |||
|
6 | import os | |||
|
7 | ||||
|
8 | try: | |||
|
9 | from jsonschema import SchemaError | |||
|
10 | from jsonschema import Draft4Validator as Validator | |||
|
11 | except ImportError as e: | |||
|
12 | verbose_msg = """ | |||
|
13 | ||||
|
14 | IPython notebook format depends on the jsonschema package: | |||
|
15 | ||||
|
16 | https://pypi.python.org/pypi/jsonschema | |||
|
17 | ||||
|
18 | Please install it first. | |||
|
19 | """ | |||
|
20 | raise ImportError(str(e) + verbose_msg) | |||
|
21 | ||||
|
22 | ||||
|
23 | from .nbbase import nbformat, nbformat_schema | |||
|
24 | schema_path = os.path.join( | |||
|
25 | os.path.dirname(__file__), nbformat_schema, | |||
|
26 | ) | |||
|
27 | ||||
|
28 | ||||
|
29 | def isvalid(nbjson, ref=None): | |||
|
30 | """Checks whether the given notebook JSON conforms to the current | |||
|
31 | notebook format schema. Returns True if the JSON is valid, and | |||
|
32 | False otherwise. | |||
|
33 | ||||
|
34 | To see the individual errors that were encountered, please use the | |||
|
35 | `validate` function instead. | |||
|
36 | """ | |||
|
37 | ||||
|
38 | it = validate(nbjson, ref) | |||
|
39 | try: | |||
|
40 | it.next() | |||
|
41 | except StopIteration: | |||
|
42 | return True | |||
|
43 | else: | |||
|
44 | return False | |||
|
45 | ||||
|
46 | ||||
|
47 | def validate(nbjson, ref=None): | |||
|
48 | """Checks whether the given notebook JSON conforms to the current | |||
|
49 | notebook format schema. | |||
|
50 | ||||
|
51 | Returns a generator for errors. | |||
|
52 | """ | |||
|
53 | ||||
|
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: | |||
|
63 | errors = v.iter_errors(nbjson, {'$ref' : '#/definitions/%s' % ref}) | |||
|
64 | else: | |||
|
65 | errors = v.iter_errors(nbjson) | |||
|
66 | return errors |
General Comments 0
You need to be logged in to leave comments.
Login now