Show More
@@ -35,7 +35,7 b' from IPython.nbformat import v3 as _v_latest' | |||||
35 | from .reader import reads as reader_reads |
|
35 | from .reader import reads as reader_reads | |
36 | from .reader import versions |
|
36 | from .reader import versions | |
37 | from .convert import convert |
|
37 | from .convert import convert | |
38 |
from .validator import |
|
38 | from .validator import validate | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Code |
|
41 | # Code | |
@@ -79,7 +79,7 b' def parse_py(s, **kwargs):' | |||||
79 | def reads_json(s, **kwargs): |
|
79 | def reads_json(s, **kwargs): | |
80 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
80 | """Read a JSON notebook from a string and return the NotebookNode object.""" | |
81 | nbjson = reader_reads(s) |
|
81 | nbjson = reader_reads(s) | |
82 |
num_errors = |
|
82 | num_errors = validate(nbjson) | |
83 | if num_errors > 0: |
|
83 | if num_errors > 0: | |
84 | print("Num errors: %d" % num_errors) |
|
84 | print("Num errors: %d" % num_errors) | |
85 | return convert(nbjson, current_nbformat) |
|
85 | return convert(nbjson, current_nbformat) | |
@@ -87,7 +87,7 b' def reads_json(s, **kwargs):' | |||||
87 |
|
87 | |||
88 | def writes_json(nb, **kwargs): |
|
88 | def writes_json(nb, **kwargs): | |
89 | nbjson = versions[current_nbformat].writes_json(nb, **kwargs) |
|
89 | nbjson = versions[current_nbformat].writes_json(nb, **kwargs) | |
90 |
num_errors = |
|
90 | num_errors = validate(nbjson) | |
91 | if num_errors > 0: |
|
91 | if num_errors > 0: | |
92 | print("Num errors: %d" % num_errors) |
|
92 | print("Num errors: %d" % num_errors) | |
93 | return nbjson |
|
93 | return nbjson |
@@ -1,12 +1,10 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 argparse |
|
|||
5 | import traceback |
|
|||
6 | import json |
|
4 | import json | |
7 | import os |
|
5 | import os | |
8 |
|
6 | |||
9 |
from IPython.external.jsonschema import Draft3Validator |
|
7 | from IPython.external.jsonschema import Draft3Validator | |
10 | import IPython.external.jsonpointer as jsonpointer |
|
8 | import IPython.external.jsonpointer as jsonpointer | |
11 | from IPython.utils.py3compat import iteritems |
|
9 | from IPython.utils.py3compat import iteritems | |
12 |
|
10 | |||
@@ -16,7 +14,7 b' schema = os.path.join(' | |||||
16 | os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema) |
|
14 | os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema) | |
17 |
|
15 | |||
18 |
|
16 | |||
19 |
def |
|
17 | def validate(nbjson, key='/', verbose=True): | |
20 | v3schema = resolve_ref(json.load(open(schema, 'r'))) |
|
18 | v3schema = resolve_ref(json.load(open(schema, 'r'))) | |
21 | if key: |
|
19 | if key: | |
22 | v3schema = jsonpointer.resolve_pointer(v3schema, key) |
|
20 | v3schema = jsonpointer.resolve_pointer(v3schema, key) | |
@@ -34,31 +32,33 b' def resolve_ref(json, base=None):' | |||||
34 |
|
32 | |||
35 | only support local reference to the same json |
|
33 | only support local reference to the same json | |
36 | """ |
|
34 | """ | |
37 |
if not base |
|
35 | if not base: | |
38 | base = json |
|
36 | base = json | |
39 |
|
37 | |||
40 | temp = None |
|
38 | temp = None | |
41 | if type(json) is list: |
|
39 | if type(json) is list: | |
42 |
temp = [] |
|
40 | temp = [] | |
43 | for item in json: |
|
41 | for item in json: | |
44 | temp.append(resolve_ref(item, base=base)) |
|
42 | temp.append(resolve_ref(item, base=base)) | |
45 | elif type(json) is dict: |
|
43 | elif type(json) is dict: | |
46 |
temp = {} |
|
44 | temp = {} | |
47 | for key,value in iteritems(json): |
|
45 | for key, value in iteritems(json): | |
48 | if key == '$ref': |
|
46 | if key == '$ref': | |
49 | return resolve_ref(jsonpointer.resolve_pointer(base,value), base=base) |
|
47 | return resolve_ref( | |
50 | else : |
|
48 | jsonpointer.resolve_pointer(base, value), base=base) | |
51 | temp[key]=resolve_ref(value, base=base) |
|
49 | else: | |
52 | else : |
|
50 | temp[key] = resolve_ref(value, base=base) | |
|
51 | else: | |||
53 | return json |
|
52 | return json | |
54 | return temp |
|
53 | return temp | |
55 |
|
54 | |||
|
55 | ||||
56 | def convert(namein, nameout, indent=2): |
|
56 | def convert(namein, nameout, indent=2): | |
57 | """resolve the references of namein, save the result in nameout""" |
|
57 | """resolve the references of namein, save the result in nameout""" | |
58 | jsn = None |
|
58 | jsn = None | |
59 |
with open(namein) as file |
|
59 | with open(namein) as file: | |
60 | jsn = json.load(file) |
|
60 | jsn = json.load(file) | |
61 | v = resolve_ref(jsn, base=jsn) |
|
61 | v = resolve_ref(jsn, base=jsn) | |
62 | x = jsonpointer.resolve_pointer(v, '/notebook') |
|
62 | x = jsonpointer.resolve_pointer(v, '/notebook') | |
63 | with open(nameout,'w') as file: |
|
63 | with open(nameout, 'w') as file: | |
64 | json.dump(x,file,indent=indent) |
|
64 | json.dump(x, file, indent=indent) |
General Comments 0
You need to be logged in to leave comments.
Login now