Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v2/rwbase.py: 16%
80 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Base classes and utilities for readers and writers.
3Authors:
5* Brian Granger
6"""
8# -----------------------------------------------------------------------------
9# Copyright (C) 2008-2011 The IPython Development Team
10#
11# Distributed under the terms of the BSD License. The full license is in
12# the file LICENSE, distributed as part of this software.
13# -----------------------------------------------------------------------------
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
19from base64 import decodebytes, encodebytes
21# -----------------------------------------------------------------------------
22# Code
23# -----------------------------------------------------------------------------
26def restore_bytes(nb):
27 """Restore bytes of image data from unicode-only formats.
29 Base64 encoding is handled elsewhere. Bytes objects in the notebook are
30 always b64-encoded. We DO NOT encode/decode around file formats.
31 """
32 for ws in nb.worksheets:
33 for cell in ws.cells:
34 if cell.cell_type == "code":
35 for output in cell.outputs:
36 if "png" in output:
37 output.png = output.png.encode("ascii")
38 if "jpeg" in output:
39 output.jpeg = output.jpeg.encode("ascii")
40 return nb
43# output keys that are likely to have multiline values
44_multiline_outputs = ["text", "html", "svg", "latex", "javascript", "json"]
47def rejoin_lines(nb):
48 """rejoin multiline text into strings
50 For reversing effects of ``split_lines(nb)``.
52 This only rejoins lines that have been split, so if text objects were not split
53 they will pass through unchanged.
55 Used when reading JSON files that may have been passed through split_lines.
56 """
57 for ws in nb.worksheets:
58 for cell in ws.cells:
59 if cell.cell_type == "code":
60 if "input" in cell and isinstance(cell.input, list):
61 cell.input = "\n".join(cell.input)
62 for output in cell.outputs:
63 for key in _multiline_outputs:
64 item = output.get(key, None)
65 if isinstance(item, list):
66 output[key] = "\n".join(item)
67 else: # text cell
68 for key in ["source", "rendered"]:
69 item = cell.get(key, None)
70 if isinstance(item, list):
71 cell[key] = "\n".join(item)
72 return nb
75def split_lines(nb):
76 """split likely multiline text into lists of strings
78 For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
79 reverse the effects of ``split_lines(nb)``.
81 Used when writing JSON files.
82 """
83 for ws in nb.worksheets:
84 for cell in ws.cells:
85 if cell.cell_type == "code":
86 if "input" in cell and isinstance(cell.input, str):
87 cell.input = cell.input.splitlines()
88 for output in cell.outputs:
89 for key in _multiline_outputs:
90 item = output.get(key, None)
91 if isinstance(item, str):
92 output[key] = item.splitlines()
93 else: # text cell
94 for key in ["source", "rendered"]:
95 item = cell.get(key, None)
96 if isinstance(item, str):
97 cell[key] = item.splitlines()
98 return nb
101# b64 encode/decode are never actually used, because all bytes objects in
102# the notebook are already b64-encoded, and we don't need/want to double-encode
105def base64_decode(nb):
106 """Restore all bytes objects in the notebook from base64-encoded strings.
108 Note: This is never used
109 """
110 for ws in nb.worksheets:
111 for cell in ws.cells:
112 if cell.cell_type == "code":
113 for output in cell.outputs:
114 if "png" in output:
115 if isinstance(output.png, str):
116 output.png = output.png.encode("ascii")
117 output.png = decodebytes(output.png)
118 if "jpeg" in output:
119 if isinstance(output.jpeg, str):
120 output.jpeg = output.jpeg.encode("ascii")
121 output.jpeg = decodebytes(output.jpeg)
122 return nb
125def base64_encode(nb):
126 """Base64 encode all bytes objects in the notebook.
128 These will be b64-encoded unicode strings
130 Note: This is never used
131 """
132 for ws in nb.worksheets:
133 for cell in ws.cells:
134 if cell.cell_type == "code":
135 for output in cell.outputs:
136 if "png" in output:
137 output.png = encodebytes(output.png).decode("ascii")
138 if "jpeg" in output:
139 output.jpeg = encodebytes(output.jpeg).decode("ascii")
140 return nb
143class NotebookReader:
144 """A class for reading notebooks."""
146 def reads(self, s, **kwargs):
147 """Read a notebook from a string."""
148 msg = "loads must be implemented in a subclass"
149 raise NotImplementedError(msg)
151 def read(self, fp, **kwargs):
152 """Read a notebook from a file like object"""
153 return self.read(fp.read(), **kwargs)
156class NotebookWriter:
157 """A class for writing notebooks."""
159 def writes(self, nb, **kwargs):
160 """Write a notebook to a string."""
161 msg = "loads must be implemented in a subclass"
162 raise NotImplementedError(msg)
164 def write(self, nb, fp, **kwargs):
165 """Write a notebook to a file like object"""
166 return fp.write(self.writes(nb, **kwargs))