##// END OF EJS Templates
create a ipynbv3 json schema and a validator...
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,166 b''
1 {
2 "description": "custom json structure with references to generate notebook schema",
3 "notebook":{
4 "type": "object",
5 "description": "notebook v3.0 root schema",
6 "$schema": "http://json-schema.org/draft-03/schema",
7 "id": "#notebook",
8 "required": true,
9 "additionalProperties": false,
10 "properties":{
11 "metadata": {
12 "type": "object",
13 "id": "metadata",
14 "required": true,
15 "description": "the metadata atribute can contain any additionnal information",
16 "additionalProperties": true,
17 "properties":{
18 "name": {
19 "id": "name",
20 "description": "the title of the notebook",
21 "type": "string",
22 "id": "name",
23 "required": true
24 }
25 }
26 },
27 "nbformat_minor": {
28 "description": "Notebook format, minor number. Incremented for slight variation of notebook format.",
29 "type": "number",
30 "id": "nbformat_minor",
31 "required": true
32 },
33 "nbformat": {
34 "description": "Notebook format, major number. Incremented between backward incompatible change is introduced.",
35 "type": "number",
36 "id": "nbformat",
37 "required": true
38 },
39 "worksheets": {
40 "description": "Array of worksheet, not used by the current implementation of ipython yet",
41 "type": "array",
42 "id": "worksheets",
43 "required": true,
44 "items": {"$ref": "/worksheet"}
45 }
46 }
47 },
48
49 "worksheet": {
50 "additionalProperties": false,
51 "properties":{
52 "cells": {
53 "type": "array",
54 "$schema": "http://json-schema.org/draft-03/schema",
55 "description": "array of cells of the current worksheet",
56 "id": "#cells",
57 "required": true,
58 "items": {"$ref": "/any_cell"}
59
60 },
61 "metadata": {
62 "type": "object",
63 "description": "metadata of the current worksheet",
64 "id": "metadata",
65 "required": true
66 }
67 }
68 },
69
70 "text_cell": {
71 "type": "object",
72 "description": "scheme for text cel and childrenm (level only optionnal argument for HEader cell)",
73 "$schema": "http://json-schema.org/draft-03/schema",
74 "id": "#cell",
75 "required": true,
76 "additionalProperties": false,
77 "properties":{
78 "cell_type": {
79 "type": "string",
80 "id": "cell_type",
81 "required": true
82 },
83 "level": {
84 "type": "number",
85 "id": "level",
86 "required": false
87 },
88 "metadata": {
89 "type": "object",
90 "id": "metadata",
91 "required": true
92 },
93 "source": {
94 "description": "for code cell, the source code",
95 "type": "array",
96 "id": "source",
97 "required": true,
98 "items":
99 {
100 "type": "string",
101 "description": "each item represent one line of the source code written, terminated by \n",
102 "id": "0",
103 "required": true
104 }
105 }
106 }
107
108 },
109
110 "any_cell": {
111 "description": "Meta cell type that match any cell type",
112 "type": [{"$ref": "/text_cell"},{"$ref":"/code_cell"}],
113 "$schema": "http://json-schema.org/draft-03/schema"
114 },
115
116 "code_cell":{
117 "type": "object",
118 "$schema": "http://json-schema.org/draft-03/schema",
119 "description": "Cell used to execute code",
120 "id": "#cell",
121 "required": true,
122 "additionalProperties": false,
123 "properties":{
124 "cell_type": {
125 "type": "string",
126 "id": "cell_type",
127 "required": true
128 },
129 "metadata": {
130 "type": "object",
131 "id": "metadata",
132 "required": true
133 },
134 "collapsed": {
135 "type": "boolean",
136 "required": true
137 },
138 "input": {
139 "description": "user input for text cells",
140 "type": "array",
141 "id": "input",
142 "required": true,
143 "items":
144 {
145 "type": "string",
146 "id": "input",
147 "required": true
148 }
149 },
150 "outputs": {
151 "description": "output for code cell, to be definied",
152 "required": true,
153 "type": "array"
154 },
155 "prompt_number": {
156 "type": ["number","string"],
157 "minimum": 0
158 },
159 "language": {
160 "type": "string",
161 "required": true
162 }
163 }
164
165 }
166 }
@@ -0,0 +1,88 b''
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3
4 from jsonschema import Validator, validate, ValidationError
5 import jsonpointer
6 import argparse
7 import traceback
8 import json
9
10 v = Validator();
11 def nbvalidate(nbjson, schema='v3.withref.json', key=None,verbose=True):
12 v3schema = resolve_ref(json.load(open(schema,'r')))
13 if key :
14 v3schema = jsonpointer.resolve_pointer(v3schema,key)
15 errors = 0
16 for error in v.iter_errors(nbjson, v3schema):
17 errors = errors + 1
18 if verbose:
19 print(error)
20 return errors
21
22 def resolve_ref(json, base=None):
23 """return a json with resolved internal references
24
25 only support local reference to the same json
26 """
27 if not base :
28 base = json
29
30 temp = None
31 if type(json) is list:
32 temp = [];
33 for item in json:
34 temp.append(resolve_ref(item, base=base))
35 elif type(json) is dict:
36 temp = {};
37 for key,value in json.iteritems():
38 if key == '$ref':
39 return resolve_ref(jsonpointer.resolve_pointer(base,value), base=base)
40 else :
41 temp[key]=resolve_ref(value, base=base)
42 else :
43 return json
44 return temp
45
46 def convert(namein, nameout, indent=2):
47 """resolve the references of namein, save the result in nameout"""
48 jsn = None
49 with open(namein) as file :
50 jsn = json.load(file)
51 v = resolve_ref(jsn, base=jsn)
52 x = jsonpointer.resolve_pointer(v, '/notebook')
53 with open(nameout,'w') as file:
54 json.dump(x,file,indent=indent)
55
56
57 if __name__ == '__main__':
58 parser = argparse.ArgumentParser()
59 parser.add_argument('-s', '--schema',
60 type=str, default='v3.withref.json')
61
62 parser.add_argument('-k', '--key',
63 type=str, default='/notebook',
64 help='subkey to extract json schema from json file')
65
66 parser.add_argument("-v", "--verbose", action="store_true",
67 help="increase output verbosity")
68
69 parser.add_argument('filename',
70 type=str,
71 help="file to validate",
72 nargs='*',
73 metavar='names')
74
75 args = parser.parse_args()
76 for name in args.filename :
77 nerror = nbvalidate(json.load(open(name,'r')),
78 schema=args.schema,
79 key=args.key,
80 verbose=args.verbose)
81 if nerror is 0:
82 print u"[Pass]",name
83 else :
84 print u"[ ]",name,'(%d)'%(nerror)
85 if args.verbose :
86 print '=================================================='
87
88
General Comments 0
You need to be logged in to leave comments. Login now