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