rwbase.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. from base64 import encodestring, decodestring
  15. #-----------------------------------------------------------------------------
  16. # Code
  17. #-----------------------------------------------------------------------------
  18. class NotebookReader(object):
  19. def reads(self, s, **kwargs):
  20. """Read a notebook from a string."""
  21. raise NotImplementedError("loads must be implemented in a subclass")
  22. def read(self, fp, **kwargs):
  23. """Read a notebook from a file like object"""
  24. return self.reads(fp.read(), **kwargs)
  25. class NotebookWriter(object):
  26. def writes(self, nb, **kwargs):
  27. """Write a notebook to a string."""
  28. raise NotImplementedError("loads must be implemented in a subclass")
  29. def write(self, nb, fp, **kwargs):
  30. """Write a notebook to a file like object"""
  31. return fp.write(self.writes(nb,**kwargs))