##// 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 1 """Read and write notebooks in JSON format.
2 2
3 3 Authors:
4 4
5 5 * Brian Granger
6 6 """
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (C) 2008-2011 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 from base64 import encodestring
20 20 from .nbbase import from_dict
21 21 from .rwbase import NotebookReader, NotebookWriter, base64_decode
22 22 import json
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Code
26 26 #-----------------------------------------------------------------------------
27 27
28 28 class BytesEncoder(json.JSONEncoder):
29 29 def default(self, obj):
30 30 if isinstance(obj, bytes):
31 return unicode(encodestring(bytes))
31 return encodestring(obj).decode('ascii')
32 32 return json.JSONEncoder.default(self, obj)
33 33
34 34
35 35 class JSONReader(NotebookReader):
36 36
37 37 def reads(self, s, **kwargs):
38 38 nb = json.loads(s, **kwargs)
39 39 nb = self.to_notebook(nb, **kwargs)
40 40 return nb
41 41
42 42 def to_notebook(self, d, **kwargs):
43 43 return base64_decode(from_dict(d))
44 44
45 45
46 46 class JSONWriter(NotebookWriter):
47 47
48 48 def writes(self, nb, **kwargs):
49 49 kwargs['cls'] = BytesEncoder
50 50 kwargs['indent'] = 4
51 51 return json.dumps(nb, **kwargs)
52 52
53 53
54 54 _reader = JSONReader()
55 55 _writer = JSONWriter()
56 56
57 57 reads = _reader.reads
58 58 read = _reader.read
59 59 to_notebook = _reader.to_notebook
60 60 write = _writer.write
61 61 writes = _writer.writes
62 62
General Comments 0
You need to be logged in to leave comments. Login now