##// END OF EJS Templates
Working draft of KernelHistoryManager
Doug Blank -
Show More
@@ -26,6 +26,11 b' except ImportError:'
26 26 sqlite3 = None
27 27 import threading
28 28
29 try:
30 from queue import Empty # Py 3
31 except ImportError:
32 from Queue import Empty # Py 2
33
29 34 # Our own packages
30 35 from IPython.config.configurable import Configurable
31 36 from IPython.external.decorator import decorator
@@ -98,9 +103,56 b' def catch_corrupt_db(f, self, *a, **kw):'
98 103 # The hist_file is probably :memory: or something else.
99 104 raise
100 105
106 class HistoryAccessorBase(Configurable):
107 input_hist_parsed = List([""])
108 input_hist_raw = List([""])
109 output_hist = Dict()
110 dir_hist = List()
111 output_hist_reprs = Dict()
112
113 def end_session(self):
114 pass
115
116 def reset(self, new_session=True):
117 """Clear the session history, releasing all object references, and
118 optionally open a new session."""
119 self.output_hist.clear()
120 # The directory history can't be completely empty
121 self.dir_hist[:] = [py3compat.getcwd()]
122
123 if new_session:
124 if self.session_number:
125 self.end_session()
126 self.input_hist_parsed[:] = [""]
127 self.input_hist_raw[:] = [""]
128 self.new_session()
129
130 def new_session(self, conn=None):
131 pass
132
133 def writeout_cache(self):
134 pass
135
136 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
137 return []
138
139 def search(self, pattern="*", raw=True, search_raw=True,
140 output=False, n=None, unique=False):
141 return []
142
143 def get_range(self, session, start=1, stop=None, raw=True,output=False):
144 return []
145
146 def get_range_by_str(self, rangestr, raw=True, output=False):
147 return []
148
149 def store_inputs(self, line_num, source, source_raw=None):
150 pass
101 151
152 def store_output(self, line_num):
153 pass
102 154
103 class HistoryAccessor(Configurable):
155 class HistoryAccessor(HistoryAccessorBase):
104 156 """Access the history database without adding to it.
105 157
106 158 This is intended for use by standalone history tools. IPython shells use
@@ -544,7 +596,7 b' class HistoryManager(HistoryAccessor):'
544 596 self.input_hist_parsed[:] = [""]
545 597 self.input_hist_raw[:] = [""]
546 598 self.new_session()
547
599
548 600 # ------------------------------
549 601 # Methods for retrieving history
550 602 # ------------------------------
@@ -748,6 +800,54 b' class HistoryManager(HistoryAccessor):'
748 800 finally:
749 801 self.db_output_cache = []
750 802
803 class KernelHistoryManager(HistoryAccessorBase):
804 def __init__(self, client):
805 self.client = client
806 self._load_history()
807
808 def _load_history(self):
809 msg_id = self.client.history()
810 while True:
811 try:
812 reply = self.client.get_shell_msg(timeout=1)
813 except Empty:
814 break
815 else:
816 if reply['parent_header'].get('msg_id') == msg_id:
817 history = reply['content'].get('history', [])
818 break
819 self.history = history
820 print("_load_history:", self.history)
821
822 def writeout_cache(self):
823 """dump cache before certain database lookups."""
824 print("write_cache")
825
826 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
827 print("get_tail: ", n)
828 return self.history[-n:]
829
830 def search(self, pattern="*", raw=True, search_raw=True,
831 output=False, n=None, unique=False):
832 print("search: ", pattern)
833 return []
834
835 def get_range(self, session, start=1, stop=None, raw=True,output=False):
836 print("get_range: ", start, stop)
837 if stop is None:
838 stop = -1
839 return self.history[start:stop]
840
841 def get_range_by_str(self, rangestr, raw=True, output=False):
842 print("get_range_by_str: " + rangestr)
843 return []
844
845 def store_inputs(self, line_num, source, source_raw=None):
846 print("store_inputs")
847
848 def store_output(self, line_num):
849 print("store_output")
850
751 851
752 852 class HistorySavingThread(threading.Thread):
753 853 """This thread takes care of writing history to the database, so that
@@ -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.HistoryManager')
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.core.history import KernelHistoryManager
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 = KernelHistoryManager(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