##// END OF EJS Templates
Fix new relative imports
Thomas Kluyver -
Show More
@@ -1,107 +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 from . import v1
22 import v2
22 from . import v2
23 import v3
23 from . 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 # Limit the error message to 80 characters. Display whatever JSON will fit.
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 raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[:77] + "...")
45 return nb_dict
45 return nb_dict
46
46
47 # High level API
47 # High level API
48
48
49 def get_version(nb):
49 def get_version(nb):
50 """Get the version of a notebook.
50 """Get the version of a notebook.
51
51
52 Parameters
52 Parameters
53 ----------
53 ----------
54 nb : dict
54 nb : dict
55 NotebookNode or dict containing notebook data.
55 NotebookNode or dict containing notebook data.
56
56
57 Returns
57 Returns
58 -------
58 -------
59 Tuple containing major (int) and minor (int) version numbers
59 Tuple containing major (int) and minor (int) version numbers
60 """
60 """
61 major = nb.get('nbformat', 1)
61 major = nb.get('nbformat', 1)
62 minor = nb.get('nbformat_minor', 0)
62 minor = nb.get('nbformat_minor', 0)
63 return (major, minor)
63 return (major, minor)
64
64
65
65
66 def reads(s, **kwargs):
66 def reads(s, **kwargs):
67 """Read a notebook from a json string and return the
67 """Read a notebook from a json string and return the
68 NotebookNode object.
68 NotebookNode object.
69
69
70 This function properly reads notebooks of any version. No version
70 This function properly reads notebooks of any version. No version
71 conversion is performed.
71 conversion is performed.
72
72
73 Parameters
73 Parameters
74 ----------
74 ----------
75 s : unicode
75 s : unicode
76 The raw unicode string to read the notebook from.
76 The raw unicode string to read the notebook from.
77
77
78 Returns
78 Returns
79 -------
79 -------
80 nb : NotebookNode
80 nb : NotebookNode
81 The notebook that was read.
81 The notebook that was read.
82 """
82 """
83 nb_dict = parse_json(s, **kwargs)
83 nb_dict = parse_json(s, **kwargs)
84 (major, minor) = get_version(nb_dict)
84 (major, minor) = get_version(nb_dict)
85 if major in versions:
85 if major in versions:
86 return versions[major].to_notebook_json(nb_dict, minor=minor)
86 return versions[major].to_notebook_json(nb_dict, minor=minor)
87 else:
87 else:
88 raise NBFormatError('Unsupported nbformat version %s' % major)
88 raise NBFormatError('Unsupported nbformat version %s' % major)
89
89
90
90
91 def read(fp, **kwargs):
91 def read(fp, **kwargs):
92 """Read a notebook from a file and return the NotebookNode object.
92 """Read a notebook from a file and return the NotebookNode object.
93
93
94 This function properly reads notebooks of any version. No version
94 This function properly reads notebooks of any version. No version
95 conversion is performed.
95 conversion is performed.
96
96
97 Parameters
97 Parameters
98 ----------
98 ----------
99 fp : file
99 fp : file
100 Any file-like object with a read method.
100 Any file-like object with a read method.
101
101
102 Returns
102 Returns
103 -------
103 -------
104 nb : NotebookNode
104 nb : NotebookNode
105 The notebook that was read.
105 The notebook that was read.
106 """
106 """
107 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