Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v4/rwbase.py: 20%
64 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."""
3# Copyright (c) IPython Development Team.
4# Distributed under the terms of the Modified BSD License.
7def _is_json_mime(mime):
8 """Is a key a JSON mime-type that should be left alone?"""
9 return mime == "application/json" or (
10 mime.startswith("application/") and mime.endswith("+json")
11 )
14def _rejoin_mimebundle(data):
15 """Rejoin the multi-line string fields in a mimebundle (in-place)"""
16 for key, value in list(data.items()):
17 if (
18 not _is_json_mime(key)
19 and isinstance(value, list)
20 and all(isinstance(line, str) for line in value)
21 ):
22 data[key] = "".join(value)
23 return data
26def rejoin_lines(nb):
27 """rejoin multiline text into strings
29 For reversing effects of ``split_lines(nb)``.
31 This only rejoins lines that have been split, so if text objects were not split
32 they will pass through unchanged.
34 Used when reading JSON files that may have been passed through split_lines.
35 """
36 for cell in nb.cells:
37 if "source" in cell and isinstance(cell.source, list):
38 cell.source = "".join(cell.source)
40 attachments = cell.get("attachments", {})
41 for _, attachment in attachments.items():
42 _rejoin_mimebundle(attachment)
44 if cell.get("cell_type", None) == "code":
45 for output in cell.get("outputs", []):
46 output_type = output.get("output_type", "")
47 if output_type in {"execute_result", "display_data"}:
48 _rejoin_mimebundle(output.get("data", {}))
49 elif output_type and isinstance(output.get("text", ""), list):
50 output.text = "".join(output.text)
51 return nb
54_non_text_split_mimes = {
55 "application/javascript",
56 "image/svg+xml",
57}
60def _split_mimebundle(data):
61 """Split multi-line string fields in a mimebundle (in-place)"""
62 for key, value in list(data.items()):
63 if isinstance(value, str) and (key.startswith("text/") or key in _non_text_split_mimes):
64 data[key] = value.splitlines(True)
65 return data
68def split_lines(nb):
69 """split likely multiline text into lists of strings
71 For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
72 reverse the effects of ``split_lines(nb)``.
74 Used when writing JSON files.
75 """
76 for cell in nb.cells:
77 source = cell.get("source", None)
78 if isinstance(source, str):
79 cell["source"] = source.splitlines(True)
81 attachments = cell.get("attachments", {})
82 for _, attachment in attachments.items():
83 _split_mimebundle(attachment)
85 if cell.cell_type == "code":
86 for output in cell.outputs:
87 if output.output_type in {"execute_result", "display_data"}:
88 _split_mimebundle(output.get("data", {}))
89 elif output.output_type == "stream" and isinstance(output.text, str):
90 output.text = output.text.splitlines(True)
91 return nb
94def strip_transient(nb):
95 """Strip transient values that shouldn't be stored in files.
97 This should be called in *both* read and write.
98 """
99 nb.metadata.pop("orig_nbformat", None)
100 nb.metadata.pop("orig_nbformat_minor", None)
101 nb.metadata.pop("signature", None)
102 for cell in nb.cells:
103 cell.metadata.pop("trusted", None)
104 return nb
107class NotebookReader:
108 """A class for reading notebooks."""
110 def reads(self, s, **kwargs):
111 """Read a notebook from a string."""
112 msg = "reads must be implemented in a subclass"
113 raise NotImplementedError(msg)
115 def read(self, fp, **kwargs):
116 """Read a notebook from a file like object"""
117 nbs = fp.read()
118 return self.reads(nbs, **kwargs)
121class NotebookWriter:
122 """A class for writing notebooks."""
124 def writes(self, nb, **kwargs):
125 """Write a notebook to a string."""
126 msg = "writes must be implemented in a subclass"
127 raise NotImplementedError(msg)
129 def write(self, nb, fp, **kwargs):
130 """Write a notebook to a file like object"""
131 nbs = self.writes(nb, **kwargs)
132 return fp.write(nbs)