##// END OF EJS Templates
Fixing XML notebook reader.
Brian E. Granger -
Show More
@@ -1,187 +1,188 b''
1 """Read and write notebook files as XML.
1 """Read and write notebook files as XML.
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, decodestring
19 from base64 import encodestring, decodestring
20 import warnings
20 import warnings
21 from xml.etree import ElementTree as ET
21 from xml.etree import ElementTree as ET
22
22
23 from .rwbase import NotebookReader, NotebookWriter
23 from .rwbase import NotebookReader, NotebookWriter
24 from .nbbase import (
24 from .nbbase import (
25 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
25 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
26 new_metadata
26 )
27 )
27
28
28 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
29 # Code
30 # Code
30 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
31
32
32 def indent(elem, level=0):
33 def indent(elem, level=0):
33 i = "\n" + level*" "
34 i = "\n" + level*" "
34 if len(elem):
35 if len(elem):
35 if not elem.text or not elem.text.strip():
36 if not elem.text or not elem.text.strip():
36 elem.text = i + " "
37 elem.text = i + " "
37 if not elem.tail or not elem.tail.strip():
38 if not elem.tail or not elem.tail.strip():
38 elem.tail = i
39 elem.tail = i
39 for elem in elem:
40 for elem in elem:
40 indent(elem, level+1)
41 indent(elem, level+1)
41 if not elem.tail or not elem.tail.strip():
42 if not elem.tail or not elem.tail.strip():
42 elem.tail = i
43 elem.tail = i
43 else:
44 else:
44 if level and (not elem.tail or not elem.tail.strip()):
45 if level and (not elem.tail or not elem.tail.strip()):
45 elem.tail = i
46 elem.tail = i
46
47
47
48
48 def _get_text(e, tag):
49 def _get_text(e, tag):
49 sub_e = e.find(tag)
50 sub_e = e.find(tag)
50 if sub_e is None:
51 if sub_e is None:
51 return None
52 return None
52 else:
53 else:
53 return sub_e.text
54 return sub_e.text
54
55
55
56
56 def _set_text(nbnode, attr, parent, tag):
57 def _set_text(nbnode, attr, parent, tag):
57 if attr in nbnode:
58 if attr in nbnode:
58 e = ET.SubElement(parent, tag)
59 e = ET.SubElement(parent, tag)
59 e.text = nbnode[attr]
60 e.text = nbnode[attr]
60
61
61
62
62 def _get_int(e, tag):
63 def _get_int(e, tag):
63 sub_e = e.find(tag)
64 sub_e = e.find(tag)
64 if sub_e is None:
65 if sub_e is None:
65 return None
66 return None
66 else:
67 else:
67 return int(sub_e.text)
68 return int(sub_e.text)
68
69
69
70
70 def _set_int(nbnode, attr, parent, tag):
71 def _set_int(nbnode, attr, parent, tag):
71 if attr in nbnode:
72 if attr in nbnode:
72 e = ET.SubElement(parent, tag)
73 e = ET.SubElement(parent, tag)
73 e.text = unicode(nbnode[attr])
74 e.text = unicode(nbnode[attr])
74
75
75
76
76 def _get_bool(e, tag):
77 def _get_bool(e, tag):
77 sub_e = e.find(tag)
78 sub_e = e.find(tag)
78 if sub_e is None:
79 if sub_e is None:
79 return None
80 return None
80 else:
81 else:
81 return bool(int(sub_e.text))
82 return bool(int(sub_e.text))
82
83
83
84
84 def _set_bool(nbnode, attr, parent, tag):
85 def _set_bool(nbnode, attr, parent, tag):
85 if attr in nbnode:
86 if attr in nbnode:
86 e = ET.SubElement(parent, tag)
87 e = ET.SubElement(parent, tag)
87 if nbnode[attr]:
88 if nbnode[attr]:
88 e.text = u'1'
89 e.text = u'1'
89 else:
90 else:
90 e.text = u'0'
91 e.text = u'0'
91
92
92
93
93 def _get_binary(e, tag):
94 def _get_binary(e, tag):
94 sub_e = e.find(tag)
95 sub_e = e.find(tag)
95 if sub_e is None:
96 if sub_e is None:
96 return None
97 return None
97 else:
98 else:
98 return decodestring(sub_e.text)
99 return decodestring(sub_e.text)
99
100
100
101
101 def _set_binary(nbnode, attr, parent, tag):
102 def _set_binary(nbnode, attr, parent, tag):
102 if attr in nbnode:
103 if attr in nbnode:
103 e = ET.SubElement(parent, tag)
104 e = ET.SubElement(parent, tag)
104 e.text = encodestring(nbnode[attr])
105 e.text = encodestring(nbnode[attr])
105
106
106
107
107 class XMLReader(NotebookReader):
108 class XMLReader(NotebookReader):
108
109
109 def reads(self, s, **kwargs):
110 def reads(self, s, **kwargs):
110 root = ET.fromstring(s)
111 root = ET.fromstring(s)
111 return self.to_notebook(root, **kwargs)
112 return self.to_notebook(root, **kwargs)
112
113
113 def to_notebook(self, root, **kwargs):
114 def to_notebook(self, root, **kwargs):
114 warnings.warn('The XML notebook format is no longer supported, '
115 warnings.warn('The XML notebook format is no longer supported, '
115 'please convert your notebooks to JSON.', DeprecationWarning)
116 'please convert your notebooks to JSON.', DeprecationWarning)
116 nbname = _get_text(root,u'name')
117 nbname = _get_text(root,u'name')
117 nbauthor = _get_text(root,u'author')
118 nbauthor = _get_text(root,u'author')
118 nbemail = _get_text(root,u'email')
119 nbemail = _get_text(root,u'email')
119 nblicense = _get_text(root,u'license')
120 nblicense = _get_text(root,u'license')
120 nbcreated = _get_text(root,u'created')
121 nbcreated = _get_text(root,u'created')
121 nbsaved = _get_text(root,u'saved')
122 nbsaved = _get_text(root,u'saved')
122
123
123 worksheets = []
124 worksheets = []
124 for ws_e in root.find(u'worksheets').getiterator(u'worksheet'):
125 for ws_e in root.find(u'worksheets').getiterator(u'worksheet'):
125 wsname = _get_text(ws_e,u'name')
126 wsname = _get_text(ws_e,u'name')
126 cells = []
127 cells = []
127 for cell_e in ws_e.find(u'cells').getiterator():
128 for cell_e in ws_e.find(u'cells').getiterator():
128 if cell_e.tag == u'codecell':
129 if cell_e.tag == u'codecell':
129 input = _get_text(cell_e,u'input')
130 input = _get_text(cell_e,u'input')
130 prompt_number = _get_int(cell_e,u'prompt_number')
131 prompt_number = _get_int(cell_e,u'prompt_number')
131 collapsed = _get_bool(cell_e,u'collapsed')
132 collapsed = _get_bool(cell_e,u'collapsed')
132 language = _get_text(cell_e,u'language')
133 language = _get_text(cell_e,u'language')
133 outputs = []
134 outputs = []
134 for output_e in cell_e.find(u'outputs').getiterator(u'output'):
135 for output_e in cell_e.find(u'outputs').getiterator(u'output'):
135 output_type = _get_text(output_e,u'output_type')
136 output_type = _get_text(output_e,u'output_type')
136 output_text = _get_text(output_e,u'text')
137 output_text = _get_text(output_e,u'text')
137 output_png = _get_binary(output_e,u'png')
138 output_png = _get_binary(output_e,u'png')
138 output_jpeg = _get_binary(output_e,u'jpeg')
139 output_jpeg = _get_binary(output_e,u'jpeg')
139 output_svg = _get_text(output_e,u'svg')
140 output_svg = _get_text(output_e,u'svg')
140 output_html = _get_text(output_e,u'html')
141 output_html = _get_text(output_e,u'html')
141 output_latex = _get_text(output_e,u'latex')
142 output_latex = _get_text(output_e,u'latex')
142 output_json = _get_text(output_e,u'json')
143 output_json = _get_text(output_e,u'json')
143 output_javascript = _get_text(output_e,u'javascript')
144 output_javascript = _get_text(output_e,u'javascript')
144
145
145 out_prompt_number = _get_int(output_e,u'prompt_number')
146 out_prompt_number = _get_int(output_e,u'prompt_number')
146 etype = _get_text(output_e,u'etype')
147 etype = _get_text(output_e,u'etype')
147 evalue = _get_text(output_e,u'evalue')
148 evalue = _get_text(output_e,u'evalue')
148 traceback = []
149 traceback = []
149 traceback_e = output_e.find(u'traceback')
150 traceback_e = output_e.find(u'traceback')
150 if traceback_e is not None:
151 if traceback_e is not None:
151 for frame_e in traceback_e.getiterator(u'frame'):
152 for frame_e in traceback_e.getiterator(u'frame'):
152 traceback.append(frame_e.text)
153 traceback.append(frame_e.text)
153 if len(traceback) == 0:
154 if len(traceback) == 0:
154 traceback = None
155 traceback = None
155 output = new_output(output_type=output_type,output_png=output_png,
156 output = new_output(output_type=output_type,output_png=output_png,
156 output_text=output_text, output_svg=output_svg,
157 output_text=output_text, output_svg=output_svg,
157 output_html=output_html, output_latex=output_latex,
158 output_html=output_html, output_latex=output_latex,
158 output_json=output_json, output_javascript=output_javascript,
159 output_json=output_json, output_javascript=output_javascript,
159 output_jpeg=output_jpeg, prompt_number=out_prompt_number,
160 output_jpeg=output_jpeg, prompt_number=out_prompt_number,
160 etype=etype, evalue=evalue, traceback=traceback
161 etype=etype, evalue=evalue, traceback=traceback
161 )
162 )
162 outputs.append(output)
163 outputs.append(output)
163 cc = new_code_cell(input=input,prompt_number=prompt_number,
164 cc = new_code_cell(input=input,prompt_number=prompt_number,
164 language=language,outputs=outputs,collapsed=collapsed)
165 language=language,outputs=outputs,collapsed=collapsed)
165 cells.append(cc)
166 cells.append(cc)
166 if cell_e.tag == u'htmlcell':
167 if cell_e.tag == u'htmlcell':
167 source = _get_text(cell_e,u'source')
168 source = _get_text(cell_e,u'source')
168 rendered = _get_text(cell_e,u'rendered')
169 rendered = _get_text(cell_e,u'rendered')
169 cells.append(new_text_cell(u'html', source=source, rendered=rendered))
170 cells.append(new_text_cell(u'html', source=source, rendered=rendered))
170 if cell_e.tag == u'markdowncell':
171 if cell_e.tag == u'markdowncell':
171 source = _get_text(cell_e,u'source')
172 source = _get_text(cell_e,u'source')
172 rendered = _get_text(cell_e,u'rendered')
173 rendered = _get_text(cell_e,u'rendered')
173 cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
174 cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
174 ws = new_worksheet(name=wsname,cells=cells)
175 ws = new_worksheet(name=wsname,cells=cells)
175 worksheets.append(ws)
176 worksheets.append(ws)
176
177
177 nb = new_notebook(name=nbname,worksheets=worksheets,author=nbauthor,
178 md = new_metadata(name=nbname)
178 email=nbemail,license=nblicense,saved=nbsaved,created=nbcreated)
179 nb = new_notebook(metadata=md,worksheets=worksheets)
179 return nb
180 return nb
180
181
181
182
182 _reader = XMLReader()
183 _reader = XMLReader()
183
184
184 reads = _reader.reads
185 reads = _reader.reads
185 read = _reader.read
186 read = _reader.read
186 to_notebook = _reader.to_notebook
187 to_notebook = _reader.to_notebook
187
188
General Comments 0
You need to be logged in to leave comments. Login now