diff --git a/IPython/nbformat/current.py b/IPython/nbformat/current.py index 5223c4e..5f17051 100644 --- a/IPython/nbformat/current.py +++ b/IPython/nbformat/current.py @@ -28,13 +28,14 @@ from IPython.nbformat.v3 import ( NotebookNode, new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, parse_filename, new_metadata, new_author, new_heading_cell, nbformat, - nbformat_minor, to_notebook_json + nbformat_minor, nbformat_schema, to_notebook_json ) from IPython.nbformat import v3 as _v_latest from .reader import reads as reader_reads from .reader import versions from .convert import convert +from .validator import nbvalidate #----------------------------------------------------------------------------- # Code @@ -44,6 +45,7 @@ current_nbformat = nbformat current_nbformat_minor = nbformat_minor current_nbformat_module = _v_latest.__name__ + def docstring_nbformat_mod(func): """Decorator for docstrings referring to classes/functions accessed through nbformat.current. @@ -76,11 +78,19 @@ def parse_py(s, **kwargs): def reads_json(s, **kwargs): """Read a JSON notebook from a string and return the NotebookNode object.""" - return convert(reader_reads(s), current_nbformat) + nbjson = reader_reads(s) + num_errors = nbvalidate(nbjson) + if num_errors > 0: + print("Num errors: %d" % num_errors) + return convert(nbjson, current_nbformat) def writes_json(nb, **kwargs): - return versions[current_nbformat].writes_json(nb, **kwargs) + nbjson = versions[current_nbformat].writes_json(nb, **kwargs) + num_errors = nbvalidate(nbjson) + if num_errors > 0: + print("Num errors: %d" % num_errors) + return nbjson def reads_py(s, **kwargs): diff --git a/IPython/nbformat/v3/__init__.py b/IPython/nbformat/v3/__init__.py index 2bcc4bc..b874956 100644 --- a/IPython/nbformat/v3/__init__.py +++ b/IPython/nbformat/v3/__init__.py @@ -19,7 +19,8 @@ Authors: from .nbbase import ( NotebookNode, new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, - new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor + new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor, + nbformat_schema ) from .nbjson import reads as reads_json, writes as writes_json diff --git a/IPython/nbformat/v3/nbbase.py b/IPython/nbformat/v3/nbbase.py index fcd86f6..c829cf3 100644 --- a/IPython/nbformat/v3/nbbase.py +++ b/IPython/nbformat/v3/nbbase.py @@ -11,6 +11,7 @@ helpers to build the structs in the right form. import pprint import uuid +import os from IPython.utils.ipstruct import Struct from IPython.utils.py3compat import cast_unicode, unicode_type @@ -22,6 +23,7 @@ from IPython.utils.py3compat import cast_unicode, unicode_type # Change this when incrementing the nbformat version nbformat = 3 nbformat_minor = 0 +nbformat_schema = 'v3.withref.json' class NotebookNode(Struct): pass diff --git a/IPython/nbformat/v3/validator.py b/IPython/nbformat/validator.py similarity index 86% rename from IPython/nbformat/v3/validator.py rename to IPython/nbformat/validator.py old mode 100755 new mode 100644 index bc037cc..7598889 --- a/IPython/nbformat/v3/validator.py +++ b/IPython/nbformat/validator.py @@ -4,23 +4,31 @@ from __future__ import print_function import argparse import traceback import json +import os -from IPython.external.jsonschema import Draft3Validator, validate, ValidationError +from IPython.external.jsonschema import Draft3Validator, validate, ValidationError import IPython.external.jsonpointer as jsonpointer from IPython.utils.py3compat import iteritems -def nbvalidate(nbjson, schema='v3.withref.json', key=None,verbose=True): - v3schema = resolve_ref(json.load(open(schema,'r'))) - if key : - v3schema = jsonpointer.resolve_pointer(v3schema,key) + +from .current import nbformat, nbformat_schema +schema = os.path.join( + os.path.split(__file__)[0], "v%d" % nbformat, nbformat_schema) + + +def nbvalidate(nbjson, key='/notebook', verbose=True): + v3schema = resolve_ref(json.load(open(schema, 'r'))) + if key: + v3schema = jsonpointer.resolve_pointer(v3schema, key) errors = 0 - v = Draft3Validator(v3schema); + v = Draft3Validator(v3schema) for error in v.iter_errors(nbjson): errors = errors + 1 if verbose: print(error) return errors + def resolve_ref(json, base=None): """return a json with resolved internal references @@ -58,9 +66,6 @@ def convert(namein, nameout, indent=2): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('-s', '--schema', - type=str, default='v3.withref.json') - parser.add_argument('-k', '--key', type=str, default='/notebook', help='subkey to extract json schema from json file') @@ -77,7 +82,6 @@ if __name__ == '__main__': args = parser.parse_args() for name in args.filename : nerror = nbvalidate(json.load(open(name,'r')), - schema=args.schema, key=args.key, verbose=args.verbose) if nerror is 0: @@ -86,5 +90,3 @@ if __name__ == '__main__': print(u"[ ]",name,'(%d)'%(nerror)) if args.verbose : print('==================================================') - -