rwbase.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Base classes and function 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. # -----------------------------------------------------------------------------
  15. # Code
  16. # -----------------------------------------------------------------------------
  17. from __future__ import annotations
  18. class NotebookReader:
  19. """The base notebook reader."""
  20. def reads(self, s, **kwargs):
  21. """Read a notebook from a string."""
  22. msg = "loads must be implemented in a subclass"
  23. raise NotImplementedError(msg)
  24. def read(self, fp, **kwargs):
  25. """Read a notebook from a file like object"""
  26. return self.reads(fp.read(), **kwargs)
  27. class NotebookWriter:
  28. """The base notebook writer."""
  29. def writes(self, nb, **kwargs):
  30. """Write a notebook to a string."""
  31. msg = "loads must be implemented in a subclass"
  32. raise NotImplementedError(msg)
  33. def write(self, nb, fp, **kwargs):
  34. """Write a notebook to a file like object"""
  35. return fp.write(self.writes(nb, **kwargs))