##// END OF EJS Templates
Updated JSON error message to limit full error to 80 characters....
Jonathan Frederic -
Show More
@@ -1,106 +1,107 b''
1 1 """API for reading notebooks.
2 2
3 3 Authors:
4 4
5 5 * Jonathan Frederic
6 6 """
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (C) 2013 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 import json
20 20
21 21 import v1
22 22 import v2
23 23 import v3
24 24
25 25 versions = {
26 26 1: v1,
27 27 2: v2,
28 28 3: v3,
29 29 }
30 30
31 31 #-----------------------------------------------------------------------------
32 32 # Code
33 33 #-----------------------------------------------------------------------------
34 34
35 35 class NotJSONError(ValueError):
36 36 pass
37 37
38 38 def parse_json(s, **kwargs):
39 39 """Parse a JSON string into a dict."""
40 40 try:
41 41 nb_dict = json.loads(s, **kwargs)
42 42 except ValueError:
43 raise NotJSONError("Notebook does not appear to be JSON: %r" % s[:16])
43 # Limit the error message to 80 characters. Display whatever JSON will fit.
44 raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[:77] + "...")
44 45 return nb_dict
45 46
46 47 # High level API
47 48
48 49 def get_version(nb):
49 50 """Get the version of a notebook.
50 51
51 52 Parameters
52 53 ----------
53 54 nb : dict
54 55 NotebookNode or dict containing notebook data.
55 56
56 57 Returns
57 58 -------
58 59 Tuple containing major (int) and minor (int) version numbers
59 60 """
60 61 major = nb.get('nbformat', 1)
61 62 minor = nb.get('nbformat_minor', 0)
62 63 return (major, minor)
63 64
64 65
65 66 def reads(s, **kwargs):
66 67 """Read a notebook from a json string and return the
67 68 NotebookNode object.
68 69
69 70 This function properly reads notebooks of any version. No version
70 71 conversion is performed.
71 72
72 73 Parameters
73 74 ----------
74 75 s : unicode
75 76 The raw unicode string to read the notebook from.
76 77
77 78 Returns
78 79 -------
79 80 nb : NotebookNode
80 81 The notebook that was read.
81 82 """
82 83 nb_dict = parse_json(s, **kwargs)
83 84 (major, minor) = get_version(nb_dict)
84 85 if major in versions:
85 86 return versions[major].to_notebook_json(nb_dict, minor=minor)
86 87 else:
87 88 raise NBFormatError('Unsupported nbformat version %s' % major)
88 89
89 90
90 91 def read(fp, **kwargs):
91 92 """Read a notebook from a file and return the NotebookNode object.
92 93
93 94 This function properly reads notebooks of any version. No version
94 95 conversion is performed.
95 96
96 97 Parameters
97 98 ----------
98 99 fp : file
99 100 Any file-like object with a read method.
100 101
101 102 Returns
102 103 -------
103 104 nb : NotebookNode
104 105 The notebook that was read.
105 106 """
106 107 return reads(fp.read(), **kwargs)
General Comments 0
You need to be logged in to leave comments. Login now