Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v3/rwbase.py: 16%

94 statements  

« 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.""" 

2 

3# Copyright (c) IPython Development Team. 

4# Distributed under the terms of the Modified BSD License. 

5 

6 

7from base64 import decodebytes, encodebytes 

8 

9 

10def restore_bytes(nb): 

11 """Restore bytes of image data from unicode-only formats. 

12 

13 Base64 encoding is handled elsewhere. Bytes objects in the notebook are 

14 always b64-encoded. We DO NOT encode/decode around file formats. 

15 

16 Note: this is never used 

17 """ 

18 for ws in nb.worksheets: 

19 for cell in ws.cells: 

20 if cell.cell_type == "code": 

21 for output in cell.outputs: 

22 if "png" in output: 

23 output.png = output.png.encode("ascii", "replace") 

24 if "jpeg" in output: 

25 output.jpeg = output.jpeg.encode("ascii", "replace") 

26 return nb 

27 

28 

29# output keys that are likely to have multiline values 

30_multiline_outputs = ["text", "html", "svg", "latex", "javascript", "json"] 

31 

32 

33# FIXME: workaround for old splitlines() 

34def _join_lines(lines): 

35 """join lines that have been written by splitlines() 

36 

37 Has logic to protect against `splitlines()`, which 

38 should have been `splitlines(True)` 

39 """ 

40 if lines and lines[0].endswith(("\n", "\r")): 

41 # created by splitlines(True) 

42 return "".join(lines) 

43 else: 

44 # created by splitlines() 

45 return "\n".join(lines) 

46 

47 

48def rejoin_lines(nb): 

49 """rejoin multiline text into strings 

50 

51 For reversing effects of ``split_lines(nb)``. 

52 

53 This only rejoins lines that have been split, so if text objects were not split 

54 they will pass through unchanged. 

55 

56 Used when reading JSON files that may have been passed through split_lines. 

57 """ 

58 for ws in nb.worksheets: 

59 for cell in ws.cells: 

60 if cell.cell_type == "code": 

61 if "input" in cell and isinstance(cell.input, list): 

62 cell.input = _join_lines(cell.input) 

63 for output in cell.outputs: 

64 for key in _multiline_outputs: 

65 item = output.get(key, None) 

66 if isinstance(item, list): 

67 output[key] = _join_lines(item) 

68 else: # text, heading cell 

69 for key in ["source", "rendered"]: 

70 item = cell.get(key, None) 

71 if isinstance(item, list): 

72 cell[key] = _join_lines(item) 

73 return nb 

74 

75 

76def split_lines(nb): 

77 """split likely multiline text into lists of strings 

78 

79 For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will 

80 reverse the effects of ``split_lines(nb)``. 

81 

82 Used when writing JSON files. 

83 """ 

84 for ws in nb.worksheets: 

85 for cell in ws.cells: 

86 if cell.cell_type == "code": 

87 if "input" in cell and isinstance(cell.input, str): 

88 cell.input = cell.input.splitlines(True) 

89 for output in cell.outputs: 

90 for key in _multiline_outputs: 

91 item = output.get(key, None) 

92 if isinstance(item, str): 

93 output[key] = item.splitlines(True) 

94 else: # text, heading cell 

95 for key in ["source", "rendered"]: 

96 item = cell.get(key, None) 

97 if isinstance(item, str): 

98 cell[key] = item.splitlines(True) 

99 return nb 

100 

101 

102# b64 encode/decode are never actually used, because all bytes objects in 

103# the notebook are already b64-encoded, and we don't need/want to double-encode 

104 

105 

106def base64_decode(nb): 

107 """Restore all bytes objects in the notebook from base64-encoded strings. 

108 

109 Note: This is never used 

110 """ 

111 for ws in nb.worksheets: 

112 for cell in ws.cells: 

113 if cell.cell_type == "code": 

114 for output in cell.outputs: 

115 if "png" in output: 

116 if isinstance(output.png, str): 

117 output.png = output.png.encode("ascii") 

118 output.png = decodebytes(output.png) 

119 if "jpeg" in output: 

120 if isinstance(output.jpeg, str): 

121 output.jpeg = output.jpeg.encode("ascii") 

122 output.jpeg = decodebytes(output.jpeg) 

123 return nb 

124 

125 

126def base64_encode(nb): 

127 """Base64 encode all bytes objects in the notebook. 

128 

129 These will be b64-encoded unicode strings 

130 

131 Note: This is never used 

132 """ 

133 for ws in nb.worksheets: 

134 for cell in ws.cells: 

135 if cell.cell_type == "code": 

136 for output in cell.outputs: 

137 if "png" in output: 

138 output.png = encodebytes(output.png).decode("ascii") 

139 if "jpeg" in output: 

140 output.jpeg = encodebytes(output.jpeg).decode("ascii") 

141 return nb 

142 

143 

144def strip_transient(nb): 

145 """Strip transient values that shouldn't be stored in files. 

146 

147 This should be called in *both* read and write. 

148 """ 

149 nb.pop("orig_nbformat", None) 

150 nb.pop("orig_nbformat_minor", None) 

151 for ws in nb["worksheets"]: 

152 for cell in ws["cells"]: 

153 cell.get("metadata", {}).pop("trusted", None) 

154 # strip cell.trusted even though it shouldn't be used, 

155 # since it's where the transient value used to be stored. 

156 cell.pop("trusted", None) 

157 return nb 

158 

159 

160class NotebookReader: 

161 """A class for reading notebooks.""" 

162 

163 def reads(self, s, **kwargs): 

164 """Read a notebook from a string.""" 

165 msg = "loads must be implemented in a subclass" 

166 raise NotImplementedError(msg) 

167 

168 def read(self, fp, **kwargs): 

169 """Read a notebook from a file like object""" 

170 nbs = fp.read() 

171 return self.reads(nbs, **kwargs) 

172 

173 

174class NotebookWriter: 

175 """A class for writing notebooks.""" 

176 

177 def writes(self, nb, **kwargs): 

178 """Write a notebook to a string.""" 

179 msg = "loads must be implemented in a subclass" 

180 raise NotImplementedError(msg) 

181 

182 def write(self, nb, fp, **kwargs): 

183 """Write a notebook to a file like object""" 

184 nbs = self.writes(nb, **kwargs) 

185 return fp.write(nbs)