rwbase.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Base classes and utilities for readers and writers.
  2. Authors:
  3. * Brian Granger
  4. """
  5. # -----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file LICENSE, distributed as part of this software.
  10. # -----------------------------------------------------------------------------
  11. # -----------------------------------------------------------------------------
  12. # Imports
  13. # -----------------------------------------------------------------------------
  14. from __future__ import annotations
  15. from base64 import decodebytes, encodebytes
  16. # -----------------------------------------------------------------------------
  17. # Code
  18. # -----------------------------------------------------------------------------
  19. def restore_bytes(nb):
  20. """Restore bytes of image data from unicode-only formats.
  21. Base64 encoding is handled elsewhere. Bytes objects in the notebook are
  22. always b64-encoded. We DO NOT encode/decode around file formats.
  23. """
  24. for ws in nb.worksheets:
  25. for cell in ws.cells:
  26. if cell.cell_type == "code":
  27. for output in cell.outputs:
  28. if "png" in output:
  29. output.png = output.png.encode("ascii")
  30. if "jpeg" in output:
  31. output.jpeg = output.jpeg.encode("ascii")
  32. return nb
  33. # output keys that are likely to have multiline values
  34. _multiline_outputs = ["text", "html", "svg", "latex", "javascript", "json"]
  35. def rejoin_lines(nb):
  36. """rejoin multiline text into strings
  37. For reversing effects of ``split_lines(nb)``.
  38. This only rejoins lines that have been split, so if text objects were not split
  39. they will pass through unchanged.
  40. Used when reading JSON files that may have been passed through split_lines.
  41. """
  42. for ws in nb.worksheets:
  43. for cell in ws.cells:
  44. if cell.cell_type == "code":
  45. if "input" in cell and isinstance(cell.input, list):
  46. cell.input = "\n".join(cell.input)
  47. for output in cell.outputs:
  48. for key in _multiline_outputs:
  49. item = output.get(key, None)
  50. if isinstance(item, list):
  51. output[key] = "\n".join(item)
  52. else: # text cell
  53. for key in ["source", "rendered"]:
  54. item = cell.get(key, None)
  55. if isinstance(item, list):
  56. cell[key] = "\n".join(item)
  57. return nb
  58. def split_lines(nb):
  59. """split likely multiline text into lists of strings
  60. For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
  61. reverse the effects of ``split_lines(nb)``.
  62. Used when writing JSON files.
  63. """
  64. for ws in nb.worksheets:
  65. for cell in ws.cells:
  66. if cell.cell_type == "code":
  67. if "input" in cell and isinstance(cell.input, str):
  68. cell.input = cell.input.splitlines()
  69. for output in cell.outputs:
  70. for key in _multiline_outputs:
  71. item = output.get(key, None)
  72. if isinstance(item, str):
  73. output[key] = item.splitlines()
  74. else: # text cell
  75. for key in ["source", "rendered"]:
  76. item = cell.get(key, None)
  77. if isinstance(item, str):
  78. cell[key] = item.splitlines()
  79. return nb
  80. # b64 encode/decode are never actually used, because all bytes objects in
  81. # the notebook are already b64-encoded, and we don't need/want to double-encode
  82. def base64_decode(nb):
  83. """Restore all bytes objects in the notebook from base64-encoded strings.
  84. Note: This is never used
  85. """
  86. for ws in nb.worksheets:
  87. for cell in ws.cells:
  88. if cell.cell_type == "code":
  89. for output in cell.outputs:
  90. if "png" in output:
  91. if isinstance(output.png, str):
  92. output.png = output.png.encode("ascii")
  93. output.png = decodebytes(output.png)
  94. if "jpeg" in output:
  95. if isinstance(output.jpeg, str):
  96. output.jpeg = output.jpeg.encode("ascii")
  97. output.jpeg = decodebytes(output.jpeg)
  98. return nb
  99. def base64_encode(nb):
  100. """Base64 encode all bytes objects in the notebook.
  101. These will be b64-encoded unicode strings
  102. Note: This is never used
  103. """
  104. for ws in nb.worksheets:
  105. for cell in ws.cells:
  106. if cell.cell_type == "code":
  107. for output in cell.outputs:
  108. if "png" in output:
  109. output.png = encodebytes(output.png).decode("ascii")
  110. if "jpeg" in output:
  111. output.jpeg = encodebytes(output.jpeg).decode("ascii")
  112. return nb
  113. class NotebookReader:
  114. """A class for reading notebooks."""
  115. def reads(self, s, **kwargs):
  116. """Read a notebook from a string."""
  117. msg = "loads must be implemented in a subclass"
  118. raise NotImplementedError(msg)
  119. def read(self, fp, **kwargs):
  120. """Read a notebook from a file like object"""
  121. return self.read(fp.read(), **kwargs)
  122. class NotebookWriter:
  123. """A class for writing notebooks."""
  124. def writes(self, nb, **kwargs):
  125. """Write a notebook to a string."""
  126. msg = "loads must be implemented in a subclass"
  127. raise NotImplementedError(msg)
  128. def write(self, nb, fp, **kwargs):
  129. """Write a notebook to a file like object"""
  130. return fp.write(self.writes(nb, **kwargs))