zmqhistory.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """ ZMQ Kernel History accessor and manager. """
  2. #-----------------------------------------------------------------------------
  3. # Copyright (C) 2010-2011 The IPython Development Team.
  4. #
  5. # Distributed under the terms of the BSD License.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #-----------------------------------------------------------------------------
  9. #-----------------------------------------------------------------------------
  10. # Imports
  11. #-----------------------------------------------------------------------------
  12. from IPython.core.history import HistoryAccessorBase
  13. from traitlets import Dict, List
  14. try:
  15. from queue import Empty # Py 3
  16. except ImportError:
  17. from Queue import Empty # Py 2
  18. class ZMQHistoryManager(HistoryAccessorBase):
  19. """History accessor and manager for ZMQ-based kernels"""
  20. input_hist_parsed = List([""])
  21. output_hist = Dict()
  22. dir_hist = List()
  23. output_hist_reprs = Dict()
  24. def __init__(self, client):
  25. """
  26. Class to load the command-line history from a ZMQ-based kernel,
  27. and access the history.
  28. Parameters
  29. ----------
  30. client: `IPython.kernel.KernelClient`
  31. The kernel client in order to request the history.
  32. """
  33. self.client = client
  34. def _load_history(self, raw=True, output=False, hist_access_type='range',
  35. **kwargs):
  36. """
  37. Load the history over ZMQ from the kernel. Wraps the history
  38. messaging with loop to wait to get history results.
  39. """
  40. history = []
  41. if hasattr(self.client, "history"):
  42. ## In tests, KernelClient may not have a history method
  43. msg_id = self.client.history(raw=raw, output=output,
  44. hist_access_type=hist_access_type,
  45. **kwargs)
  46. while True:
  47. try:
  48. reply = self.client.get_shell_msg(timeout=1)
  49. except Empty:
  50. break
  51. else:
  52. if reply['parent_header'].get('msg_id') == msg_id:
  53. history = reply['content'].get('history', [])
  54. break
  55. return history
  56. def get_tail(self, n=10, raw=True, output=False, include_latest=False):
  57. return self._load_history(hist_access_type='tail', n=n, raw=raw,
  58. output=output)
  59. def search(self, pattern="*", raw=True, search_raw=True,
  60. output=False, n=None, unique=False):
  61. return self._load_history(hist_access_type='search', pattern=pattern,
  62. raw=raw, search_raw=search_raw,
  63. output=output, n=n, unique=unique)
  64. def get_range(self, session, start=1, stop=None, raw=True,output=False):
  65. return self._load_history(hist_access_type='range', raw=raw,
  66. output=output, start=start, stop=stop,
  67. session=session)
  68. def get_range_by_str(self, rangestr, raw=True, output=False):
  69. return self._load_history(hist_access_type='range', raw=raw,
  70. output=output, rangestr=rangestr)
  71. def end_session(self):
  72. """
  73. Nothing to do for ZMQ-based histories.
  74. """
  75. pass
  76. def reset(self, new_session=True):
  77. """
  78. Nothing to do for ZMQ-based histories.
  79. """
  80. pass