Debian Codegraph @

jupyter_console/zmqhistory.py

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