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