##// END OF EJS Templates
Minor tweak to nbformat, still not passing test on Python 3.
Thomas Kluyver -
Show More
@@ -1,62 +1,62 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 from base64 import encodestring
19 from base64 import encodestring
20 from .nbbase import from_dict
20 from .nbbase import from_dict
21 from .rwbase import NotebookReader, NotebookWriter, base64_decode
21 from .rwbase import NotebookReader, NotebookWriter, base64_decode
22 import json
22 import json
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Code
25 # Code
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 class BytesEncoder(json.JSONEncoder):
28 class BytesEncoder(json.JSONEncoder):
29 def default(self, obj):
29 def default(self, obj):
30 if isinstance(obj, bytes):
30 if isinstance(obj, bytes):
31 return unicode(encodestring(bytes))
31 return encodestring(obj).decode('ascii')
32 return json.JSONEncoder.default(self, obj)
32 return json.JSONEncoder.default(self, obj)
33
33
34
34
35 class JSONReader(NotebookReader):
35 class JSONReader(NotebookReader):
36
36
37 def reads(self, s, **kwargs):
37 def reads(self, s, **kwargs):
38 nb = json.loads(s, **kwargs)
38 nb = json.loads(s, **kwargs)
39 nb = self.to_notebook(nb, **kwargs)
39 nb = self.to_notebook(nb, **kwargs)
40 return nb
40 return nb
41
41
42 def to_notebook(self, d, **kwargs):
42 def to_notebook(self, d, **kwargs):
43 return base64_decode(from_dict(d))
43 return base64_decode(from_dict(d))
44
44
45
45
46 class JSONWriter(NotebookWriter):
46 class JSONWriter(NotebookWriter):
47
47
48 def writes(self, nb, **kwargs):
48 def writes(self, nb, **kwargs):
49 kwargs['cls'] = BytesEncoder
49 kwargs['cls'] = BytesEncoder
50 kwargs['indent'] = 4
50 kwargs['indent'] = 4
51 return json.dumps(nb, **kwargs)
51 return json.dumps(nb, **kwargs)
52
52
53
53
54 _reader = JSONReader()
54 _reader = JSONReader()
55 _writer = JSONWriter()
55 _writer = JSONWriter()
56
56
57 reads = _reader.reads
57 reads = _reader.reads
58 read = _reader.read
58 read = _reader.read
59 to_notebook = _reader.to_notebook
59 to_notebook = _reader.to_notebook
60 write = _writer.write
60 write = _writer.write
61 writes = _writer.writes
61 writes = _writer.writes
62
62
General Comments 0
You need to be logged in to leave comments. Login now