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