##// END OF EJS Templates
don't write orig_nbformat to disk in v3
Min RK -
Show More
@@ -1,72 +1,76 b''
1 """Read and write notebooks in JSON format.
1 """Read and write notebooks in JSON format.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 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 copy
19 import copy
20 import json
20 import json
21
21
22 from .nbbase import from_dict
22 from .nbbase import from_dict
23 from .rwbase import (
23 from .rwbase import (
24 NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines
24 NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines
25 )
25 )
26
26
27 from IPython.utils import py3compat
27 from IPython.utils import py3compat
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Code
30 # Code
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 class BytesEncoder(json.JSONEncoder):
33 class BytesEncoder(json.JSONEncoder):
34 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
34 """A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
35 def default(self, obj):
35 def default(self, obj):
36 if isinstance(obj, bytes):
36 if isinstance(obj, bytes):
37 return obj.decode('ascii')
37 return obj.decode('ascii')
38 return json.JSONEncoder.default(self, obj)
38 return json.JSONEncoder.default(self, obj)
39
39
40
40
41 class JSONReader(NotebookReader):
41 class JSONReader(NotebookReader):
42
42
43 def reads(self, s, **kwargs):
43 def reads(self, s, **kwargs):
44 nb = json.loads(s, **kwargs)
44 nb = json.loads(s, **kwargs)
45 nb = self.to_notebook(nb, **kwargs)
45 nb = self.to_notebook(nb, **kwargs)
46 return nb
46 return nb
47
47
48 def to_notebook(self, d, **kwargs):
48 def to_notebook(self, d, **kwargs):
49 return rejoin_lines(from_dict(d))
49 return rejoin_lines(from_dict(d))
50
50
51
51
52 class JSONWriter(NotebookWriter):
52 class JSONWriter(NotebookWriter):
53
53
54 def writes(self, nb, **kwargs):
54 def writes(self, nb, **kwargs):
55 kwargs['cls'] = BytesEncoder
55 kwargs['cls'] = BytesEncoder
56 kwargs['indent'] = 1
56 kwargs['indent'] = 1
57 kwargs['sort_keys'] = True
57 kwargs['sort_keys'] = True
58 kwargs['separators'] = (',',': ')
58 kwargs['separators'] = (',',': ')
59 nb = copy.deepcopy(nb)
60 # don't write transient values to disk
61 for key in ('orig_nbformat', 'orig_nbformat_minor'):
62 nb.pop(key, None)
59 if kwargs.pop('split_lines', True):
63 if kwargs.pop('split_lines', True):
60 nb = split_lines(copy.deepcopy(nb))
64 nb = split_lines(nb)
61 return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8')
65 return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8')
62
66
63
67
64 _reader = JSONReader()
68 _reader = JSONReader()
65 _writer = JSONWriter()
69 _writer = JSONWriter()
66
70
67 reads = _reader.reads
71 reads = _reader.reads
68 read = _reader.read
72 read = _reader.read
69 to_notebook = _reader.to_notebook
73 to_notebook = _reader.to_notebook
70 write = _writer.write
74 write = _writer.write
71 writes = _writer.writes
75 writes = _writer.writes
72
76
General Comments 0
You need to be logged in to leave comments. Login now