rwbase.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. """Base classes and utilities for readers and writers."""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from base64 import encodestring, decodestring
  5. from ipython_genutils import py3compat
  6. from ipython_genutils.py3compat import str_to_bytes, unicode_type, string_types
  7. def restore_bytes(nb):
  8. """Restore bytes of image data from unicode-only formats.
  9. Base64 encoding is handled elsewhere. Bytes objects in the notebook are
  10. always b64-encoded. We DO NOT encode/decode around file formats.
  11. Note: this is never used
  12. """
  13. for ws in nb.worksheets:
  14. for cell in ws.cells:
  15. if cell.cell_type == 'code':
  16. for output in cell.outputs:
  17. if 'png' in output:
  18. output.png = str_to_bytes(output.png, 'ascii')
  19. if 'jpeg' in output:
  20. output.jpeg = str_to_bytes(output.jpeg, 'ascii')
  21. return nb
  22. # output keys that are likely to have multiline values
  23. _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']
  24. # FIXME: workaround for old splitlines()
  25. def _join_lines(lines):
  26. """join lines that have been written by splitlines()
  27. Has logic to protect against `splitlines()`, which
  28. should have been `splitlines(True)`
  29. """
  30. if lines and lines[0].endswith(('\n', '\r')):
  31. # created by splitlines(True)
  32. return u''.join(lines)
  33. else:
  34. # created by splitlines()
  35. return u'\n'.join(lines)
  36. def rejoin_lines(nb):
  37. """rejoin multiline text into strings
  38. For reversing effects of ``split_lines(nb)``.
  39. This only rejoins lines that have been split, so if text objects were not split
  40. they will pass through unchanged.
  41. Used when reading JSON files that may have been passed through split_lines.
  42. """
  43. for ws in nb.worksheets:
  44. for cell in ws.cells:
  45. if cell.cell_type == 'code':
  46. if 'input' in cell and isinstance(cell.input, list):
  47. cell.input = _join_lines(cell.input)
  48. for output in cell.outputs:
  49. for key in _multiline_outputs:
  50. item = output.get(key, None)
  51. if isinstance(item, list):
  52. output[key] = _join_lines(item)
  53. else: # text, heading cell
  54. for key in ['source', 'rendered']:
  55. item = cell.get(key, None)
  56. if isinstance(item, list):
  57. cell[key] = _join_lines(item)
  58. return nb
  59. def split_lines(nb):
  60. """split likely multiline text into lists of strings
  61. For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
  62. reverse the effects of ``split_lines(nb)``.
  63. Used when writing JSON files.
  64. """
  65. for ws in nb.worksheets:
  66. for cell in ws.cells:
  67. if cell.cell_type == 'code':
  68. if 'input' in cell and isinstance(cell.input, string_types):
  69. cell.input = cell.input.splitlines(True)
  70. for output in cell.outputs:
  71. for key in _multiline_outputs:
  72. item = output.get(key, None)
  73. if isinstance(item, string_types):
  74. output[key] = item.splitlines(True)
  75. else: # text, heading cell
  76. for key in ['source', 'rendered']:
  77. item = cell.get(key, None)
  78. if isinstance(item, string_types):
  79. cell[key] = item.splitlines(True)
  80. return nb
  81. # b64 encode/decode are never actually used, because all bytes objects in
  82. # the notebook are already b64-encoded, and we don't need/want to double-encode
  83. def base64_decode(nb):
  84. """Restore all bytes objects in the notebook from base64-encoded strings.
  85. Note: This is never used
  86. """
  87. for ws in nb.worksheets:
  88. for cell in ws.cells:
  89. if cell.cell_type == 'code':
  90. for output in cell.outputs:
  91. if 'png' in output:
  92. if isinstance(output.png, unicode_type):
  93. output.png = output.png.encode('ascii')
  94. output.png = decodestring(output.png)
  95. if 'jpeg' in output:
  96. if isinstance(output.jpeg, unicode_type):
  97. output.jpeg = output.jpeg.encode('ascii')
  98. output.jpeg = decodestring(output.jpeg)
  99. return nb
  100. def base64_encode(nb):
  101. """Base64 encode all bytes objects in the notebook.
  102. These will be b64-encoded unicode strings
  103. Note: This is never used
  104. """
  105. for ws in nb.worksheets:
  106. for cell in ws.cells:
  107. if cell.cell_type == 'code':
  108. for output in cell.outputs:
  109. if 'png' in output:
  110. output.png = encodestring(output.png).decode('ascii')
  111. if 'jpeg' in output:
  112. output.jpeg = encodestring(output.jpeg).decode('ascii')
  113. return nb
  114. def strip_transient(nb):
  115. """Strip transient values that shouldn't be stored in files.
  116. This should be called in *both* read and write.
  117. """
  118. nb.pop('orig_nbformat', None)
  119. nb.pop('orig_nbformat_minor', None)
  120. for ws in nb['worksheets']:
  121. for cell in ws['cells']:
  122. cell.get('metadata', {}).pop('trusted', None)
  123. # strip cell.trusted even though it shouldn't be used,
  124. # since it's where the transient value used to be stored.
  125. cell.pop('trusted', None)
  126. return nb
  127. class NotebookReader(object):
  128. """A class for reading notebooks."""
  129. def reads(self, s, **kwargs):
  130. """Read a notebook from a string."""
  131. raise NotImplementedError("loads must be implemented in a subclass")
  132. def read(self, fp, **kwargs):
  133. """Read a notebook from a file like object"""
  134. nbs = fp.read()
  135. if not py3compat.PY3 and not isinstance(nbs, unicode_type):
  136. nbs = py3compat.str_to_unicode(nbs)
  137. return self.reads(nbs, **kwargs)
  138. class NotebookWriter(object):
  139. """A class for writing notebooks."""
  140. def writes(self, nb, **kwargs):
  141. """Write a notebook to a string."""
  142. raise NotImplementedError("loads must be implemented in a subclass")
  143. def write(self, nb, fp, **kwargs):
  144. """Write a notebook to a file like object"""
  145. nbs = self.writes(nb,**kwargs)
  146. if not py3compat.PY3 and not isinstance(nbs, unicode_type):
  147. # this branch is likely only taken for JSON on Python 2
  148. nbs = py3compat.str_to_unicode(nbs)
  149. return fp.write(nbs)