payload.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. """Payload system for IPython.
  3. Authors:
  4. * Fernando Perez
  5. * Brian Granger
  6. """
  7. #-----------------------------------------------------------------------------
  8. # Copyright (C) 2008-2011 The IPython Development Team
  9. #
  10. # Distributed under the terms of the BSD License. The full license is in
  11. # the file COPYING, distributed as part of this software.
  12. #-----------------------------------------------------------------------------
  13. #-----------------------------------------------------------------------------
  14. # Imports
  15. #-----------------------------------------------------------------------------
  16. from traitlets.config.configurable import Configurable
  17. from traitlets import List
  18. #-----------------------------------------------------------------------------
  19. # Main payload class
  20. #-----------------------------------------------------------------------------
  21. class PayloadManager(Configurable):
  22. _payload = List([])
  23. def write_payload(self, data, single=True):
  24. """Include or update the specified `data` payload in the PayloadManager.
  25. If a previous payload with the same source exists and `single` is True,
  26. it will be overwritten with the new one.
  27. """
  28. if not isinstance(data, dict):
  29. raise TypeError('Each payload write must be a dict, got: %r' % data)
  30. if single and 'source' in data:
  31. source = data['source']
  32. for i, pl in enumerate(self._payload):
  33. if 'source' in pl and pl['source'] == source:
  34. self._payload[i] = data
  35. return
  36. self._payload.append(data)
  37. def read_payload(self):
  38. return self._payload
  39. def clear_payload(self):
  40. self._payload = []