Show More
@@ -48,17 +48,6 b' def parse_json(s, **kwargs):' | |||||
48 | return nbformat, d |
|
48 | return nbformat, d | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def parse_xml(s, **kwargs): |
|
|||
52 | """Parse a string into a (nbformat, etree) tuple.""" |
|
|||
53 | root = ET.fromstring(s) |
|
|||
54 | nbformat_e = root.find('nbformat') |
|
|||
55 | if nbformat_e is not None: |
|
|||
56 | nbformat = int(nbformat_e.text) |
|
|||
57 | else: |
|
|||
58 | raise NBFormatError('No nbformat version found') |
|
|||
59 | return nbformat, root |
|
|||
60 |
|
||||
61 |
|
||||
62 | def parse_py(s, **kwargs): |
|
51 | def parse_py(s, **kwargs): | |
63 | """Parse a string into a (nbformat, string) tuple.""" |
|
52 | """Parse a string into a (nbformat, string) tuple.""" | |
64 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' |
|
53 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' | |
@@ -87,16 +76,6 b' def writes_json(nb, **kwargs):' | |||||
87 | return v2.writes_json(nb, **kwargs) |
|
76 | return v2.writes_json(nb, **kwargs) | |
88 |
|
77 | |||
89 |
|
78 | |||
90 | def reads_xml(s, **kwargs): |
|
|||
91 | """Read an XML notebook from a string and return the NotebookNode object.""" |
|
|||
92 | nbformat, root = parse_xml(s, **kwargs) |
|
|||
93 | if nbformat == 2: |
|
|||
94 | nb = v2.to_notebook_xml(root, **kwargs) |
|
|||
95 | else: |
|
|||
96 | raise NBFormatError('Unsupported XML nbformat version: %i' % nbformat) |
|
|||
97 | return nb |
|
|||
98 |
|
||||
99 |
|
||||
100 | def reads_py(s, **kwargs): |
|
79 | def reads_py(s, **kwargs): | |
101 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
80 | """Read a .py notebook from a string and return the NotebookNode object.""" | |
102 | nbformat, s = parse_py(s, **kwargs) |
|
81 | nbformat, s = parse_py(s, **kwargs) | |
@@ -133,9 +112,7 b' def reads(s, format, **kwargs):' | |||||
133 | The notebook that was read. |
|
112 | The notebook that was read. | |
134 | """ |
|
113 | """ | |
135 | format = unicode(format) |
|
114 | format = unicode(format) | |
136 |
if format == u' |
|
115 | if format == u'json' or format == u'ipynb': | |
137 | return reads_xml(s, **kwargs) |
|
|||
138 | elif format == u'json' or format == u'ipynb': |
|
|||
139 | return reads_json(s, **kwargs) |
|
116 | return reads_json(s, **kwargs) | |
140 | elif format == u'py': |
|
117 | elif format == u'py': | |
141 | return reads_py(s, **kwargs) |
|
118 | return reads_py(s, **kwargs) | |
@@ -161,9 +138,7 b' def writes(nb, format, **kwargs):' | |||||
161 | The notebook string. |
|
138 | The notebook string. | |
162 | """ |
|
139 | """ | |
163 | format = unicode(format) |
|
140 | format = unicode(format) | |
164 |
if format == u' |
|
141 | if format == u'json' or format == u'ipynb': | |
165 | raise NotImplementedError('Write to XML files is not implemented.') |
|
|||
166 | elif format == u'json' or format == u'ipynb': |
|
|||
167 | return writes_json(nb, **kwargs) |
|
142 | return writes_json(nb, **kwargs) | |
168 | elif format == u'py': |
|
143 | elif format == u'py': | |
169 | return writes_py(nb, **kwargs) |
|
144 | return writes_py(nb, **kwargs) |
@@ -26,10 +26,6 b' from .nbjson import reads as reads_json, writes as writes_json' | |||||
26 | from .nbjson import reads as read_json, writes as write_json |
|
26 | from .nbjson import reads as read_json, writes as write_json | |
27 | from .nbjson import to_notebook as to_notebook_json |
|
27 | from .nbjson import to_notebook as to_notebook_json | |
28 |
|
28 | |||
29 | from .nbxml import reads as reads_xml |
|
|||
30 | from .nbxml import reads as read_xml |
|
|||
31 | from .nbxml import to_notebook as to_notebook_xml |
|
|||
32 |
|
||||
33 | from .nbpy import reads as reads_py, writes as writes_py |
|
29 | from .nbpy import reads as reads_py, writes as writes_py | |
34 | from .nbpy import reads as read_py, writes as write_py |
|
30 | from .nbpy import reads as read_py, writes as write_py | |
35 | from .nbpy import to_notebook as to_notebook_py |
|
31 | from .nbpy import to_notebook as to_notebook_py |
General Comments 0
You need to be logged in to leave comments.
Login now