Show More
@@ -0,0 +1,95 b'' | |||
|
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 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | from IPython.core.history import HistoryAccessorBase | |
|
15 | from IPython.utils.traitlets import Dict, List | |
|
16 | ||
|
17 | try: | |
|
18 | from queue import Empty # Py 3 | |
|
19 | except ImportError: | |
|
20 | from Queue import Empty # Py 2 | |
|
21 | ||
|
22 | class ZMQHistoryManager(HistoryAccessorBase): | |
|
23 | """History accessor and manager for ZMQ-based kernels""" | |
|
24 | input_hist_parsed = List([""]) | |
|
25 | output_hist = Dict() | |
|
26 | dir_hist = List() | |
|
27 | output_hist_reprs = Dict() | |
|
28 | ||
|
29 | def __init__(self, client): | |
|
30 | """ | |
|
31 | Class to load the command-line history from a ZMQ-based kernel, | |
|
32 | and access the history. | |
|
33 | ||
|
34 | Parameters | |
|
35 | ---------- | |
|
36 | ||
|
37 | client: `IPython.kernel.KernelClient` | |
|
38 | The kernel client in order to request the history. | |
|
39 | """ | |
|
40 | self.client = client | |
|
41 | ||
|
42 | def _load_history(self, raw=True, output=False, hist_access_type='range', | |
|
43 | **kwargs): | |
|
44 | """ | |
|
45 | Load the history over ZMQ from the kernel. Wraps the history | |
|
46 | messaging with loop to wait to get history results. | |
|
47 | """ | |
|
48 | history = [] | |
|
49 | if hasattr(self.client, "history"): | |
|
50 | ## In tests, KernelClient may not have a history method | |
|
51 | msg_id = self.client.history(raw=raw, output=output, | |
|
52 | hist_access_type=hist_access_type, | |
|
53 | **kwargs) | |
|
54 | while True: | |
|
55 | try: | |
|
56 | reply = self.client.get_shell_msg(timeout=1) | |
|
57 | except Empty: | |
|
58 | break | |
|
59 | else: | |
|
60 | if reply['parent_header'].get('msg_id') == msg_id: | |
|
61 | history = reply['content'].get('history', []) | |
|
62 | break | |
|
63 | return history | |
|
64 | ||
|
65 | def get_tail(self, n=10, raw=True, output=False, include_latest=False): | |
|
66 | return self._load_history(hist_access_type='tail', n=n, raw=raw, | |
|
67 | output=output) | |
|
68 | ||
|
69 | def search(self, pattern="*", raw=True, search_raw=True, | |
|
70 | output=False, n=None, unique=False): | |
|
71 | return self._load_history(hist_access_type='search', pattern=pattern, | |
|
72 | raw=raw, search_raw=search_raw, | |
|
73 | output=output, n=n, unique=unique) | |
|
74 | ||
|
75 | def get_range(self, session, start=1, stop=None, raw=True,output=False): | |
|
76 | return self._load_history(hist_access_type='range', raw=raw, | |
|
77 | output=output, start=start, stop=stop, | |
|
78 | session=session) | |
|
79 | ||
|
80 | def get_range_by_str(self, rangestr, raw=True, output=False): | |
|
81 | return self._load_history(hist_access_type='range', raw=raw, | |
|
82 | output=output, rangestr=rangestr) | |
|
83 | ||
|
84 | def end_session(self): | |
|
85 | """ | |
|
86 | Nothing to do for ZMQ-based histories. | |
|
87 | """ | |
|
88 | pass | |
|
89 | ||
|
90 | def reset(self, new_session=True): | |
|
91 | """ | |
|
92 | Nothing to do for ZMQ-based histories. | |
|
93 | """ | |
|
94 | pass | |
|
95 |
@@ -98,9 +98,24 b' def catch_corrupt_db(f, self, *a, **kw):' | |||
|
98 | 98 | # The hist_file is probably :memory: or something else. |
|
99 | 99 | raise |
|
100 | 100 | |
|
101 | class HistoryAccessorBase(Configurable): | |
|
102 | """An abstract class for History Accessors """ | |
|
101 | 103 | |
|
104 | def get_tail(self, n=10, raw=True, output=False, include_latest=False): | |
|
105 | raise NotImplementedError | |
|
106 | ||
|
107 | def search(self, pattern="*", raw=True, search_raw=True, | |
|
108 | output=False, n=None, unique=False): | |
|
109 | raise NotImplementedError | |
|
110 | ||
|
111 | def get_range(self, session, start=1, stop=None, raw=True,output=False): | |
|
112 | raise NotImplementedError | |
|
102 | 113 | |
|
103 | class HistoryAccessor(Configurable): | |
|
114 | def get_range_by_str(self, rangestr, raw=True, output=False): | |
|
115 | raise NotImplementedError | |
|
116 | ||
|
117 | ||
|
118 | class HistoryAccessor(HistoryAccessorBase): | |
|
104 | 119 | """Access the history database without adding to it. |
|
105 | 120 | |
|
106 | 121 | This is intended for use by standalone history tools. IPython shells use |
@@ -544,7 +559,7 b' class HistoryManager(HistoryAccessor):' | |||
|
544 | 559 | self.input_hist_parsed[:] = [""] |
|
545 | 560 | self.input_hist_raw[:] = [""] |
|
546 | 561 | self.new_session() |
|
547 | ||
|
562 | ||
|
548 | 563 | # ------------------------------ |
|
549 | 564 | # Methods for retrieving history |
|
550 | 565 | # ------------------------------ |
@@ -424,7 +424,7 b' class InteractiveShell(SingletonConfigurable):' | |||
|
424 | 424 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
425 | 425 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
426 | 426 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
427 |
history_manager = Instance('IPython.core.history.History |
|
|
427 | history_manager = Instance('IPython.core.history.HistoryAccessorBase') | |
|
428 | 428 | magics_manager = Instance('IPython.core.magic.MagicsManager') |
|
429 | 429 | |
|
430 | 430 | profile_dir = Instance('IPython.core.application.ProfileDir') |
@@ -23,6 +23,7 b' except ImportError:' | |||
|
23 | 23 | |
|
24 | 24 | from IPython.core import page |
|
25 | 25 | from IPython.core import release |
|
26 | from IPython.terminal.console.zmqhistory import ZMQHistoryManager | |
|
26 | 27 | from IPython.utils.warn import warn, error |
|
27 | 28 | from IPython.utils import io |
|
28 | 29 | from IPython.utils.py3compat import string_types, input |
@@ -32,7 +33,6 b' from IPython.utils.tempdir import NamedFileInTemporaryDirectory' | |||
|
32 | 33 | from IPython.terminal.interactiveshell import TerminalInteractiveShell |
|
33 | 34 | from IPython.terminal.console.completer import ZMQCompleter |
|
34 | 35 | |
|
35 | ||
|
36 | 36 | class ZMQTerminalInteractiveShell(TerminalInteractiveShell): |
|
37 | 37 | """A subclass of TerminalInteractiveShell that uses the 0MQ kernel""" |
|
38 | 38 | _executing = False |
@@ -570,3 +570,9 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):' | |||
|
570 | 570 | |
|
571 | 571 | # Turn off the exit flag, so the mainloop can be restarted if desired |
|
572 | 572 | self.exit_now = False |
|
573 | ||
|
574 | def init_history(self): | |
|
575 | """Sets up the command history. """ | |
|
576 | self.history_manager = ZMQHistoryManager(client=self.client) | |
|
577 | self.configurables.append(self.history_manager) | |
|
578 |
General Comments 0
You need to be logged in to leave comments.
Login now