##// END OF EJS Templates
Make HistoryManager configurable.
Thomas Kluyver -
Show More
@@ -148,3 +148,14 b' c = get_config()'
148 148 # c.AliasManager.user_aliases = [
149 149 # ('foo', 'echo Hi')
150 150 # ]
151
152 #-----------------------------------------------------------------------------
153 # HistoryManager options
154 #-----------------------------------------------------------------------------
155
156 # Enable logging output as well as input to the database.
157 # c.HistoryManager.db_log_output = True
158
159 # Only write to the database every 40 commands - this can save disk access (and
160 # hence power) over the default of writing on every command.
161 # c.HistoryManager.db_cache_size = 40
@@ -18,45 +18,47 b' import os'
18 18 import sqlite3
19 19
20 20 # Our own packages
21 from IPython.config.configurable import Configurable
21 22 import IPython.utils.io
22 23
23 24 from IPython.testing import decorators as testdec
24 25 from IPython.utils.io import ask_yes_no
26 from IPython.utils.traitlets import Bool, Dict, Instance, Int, List, Unicode
25 27 from IPython.utils.warn import warn
26 28
27 29 #-----------------------------------------------------------------------------
28 30 # Classes and functions
29 31 #-----------------------------------------------------------------------------
30 32
31 class HistoryManager(object):
33 class HistoryManager(Configurable):
32 34 """A class to organize all history-related functionality in one place.
33 35 """
34 36 # Public interface
35 37
36 38 # An instance of the IPython shell we are attached to
37 shell = None
38 # A list to hold processed history
39 input_hist_parsed = None
40 # A list to hold raw history (as typed by user)
41 input_hist_raw = None
39 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
40 # Lists to hold processed and raw history. These start with a blank entry
41 # so that we can index them starting from 1
42 input_hist_parsed = List([""])
43 input_hist_raw = List([""])
42 44 # A list of directories visited during session
43 dir_hist = None
45 dir_hist = List()
44 46 # A dict of output history, keyed with ints from the shell's execution count
45 output_hist = None
46 # String with path to the history file
47 hist_file = None
47 output_hist = Dict()
48 # String holding the path to the history file
49 hist_file = Unicode()
48 50 # The SQLite database
49 db = None
51 db = Instance(sqlite3.Connection)
50 52 # The number of the current session in the history database
51 session_number = None
53 session_number = Int()
52 54 # Should we log output to the database? (default no)
53 db_log_output = False
55 db_log_output = Bool(False, config=True)
54 56 # Write to database every x commands (higher values save disk access & power)
55 57 # Values of 1 or less effectively disable caching.
56 db_cache_size = 0
58 db_cache_size = Int(0, config=True)
57 59 # The input and output caches
58 db_input_cache = None
59 db_output_cache = None
60 db_input_cache = List()
61 db_output_cache = List()
60 62
61 63 # Private interface
62 64 # Variables used to store the three last inputs from the user. On each new
@@ -69,26 +71,11 b' class HistoryManager(object):'
69 71 # call).
70 72 _exit_commands = None
71 73
72 def __init__(self, shell, load_history=False):
74 def __init__(self, shell, config=None):
73 75 """Create a new history manager associated with a shell instance.
74
75 Parameters
76 ----------
77 load_history: bool, optional
78 If True, history will be loaded from file, and the session
79 offset set, so that the next line entered can be retrieved
80 as #1.
81 76 """
82 77 # We need a pointer back to the shell for various tasks.
83 self.shell = shell
84
85 # List of input with multi-line handling. One blank entry so indexing
86 # starts from 1.
87 self.input_hist_parsed = [""]
88 # This one will hold the 'raw' input history, without any
89 # pre-processing. This will allow users to retrieve the input just as
90 # it was exactly typed in by the user, with %hist -r.
91 self.input_hist_raw = [""]
78 super(HistoryManager, self).__init__(shell=shell, config=config)
92 79
93 80 # list of visited directories
94 81 try:
@@ -96,22 +83,18 b' class HistoryManager(object):'
96 83 except OSError:
97 84 self.dir_hist = []
98 85
99 # dict of output history
100 self.output_hist = {}
101
102 86 # Now the history file
103 87 if shell.profile:
104 88 histfname = 'history-%s' % shell.profile
105 89 else:
106 90 histfname = 'history'
107 91 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
92 self.init_db()
108 93
109 94 self._i00, self._i, self._ii, self._iii = '','','',''
110 95
111 96 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
112 97 '%quit', '%Exit', '%exit'])
113
114 self.init_db()
115 98
116 99 def init_db(self):
117 100 self.db = sqlite3.connect(self.hist_file)
@@ -139,8 +122,6 b' class HistoryManager(object):'
139 122 self.db.execute("""UPDATE singletons SET value=? WHERE
140 123 name='session_number'""", (self.session_number+1,))
141 124 self.db.commit()
142 self.db_input_cache = []
143 self.db_output_cache = []
144 125
145 126 def get_db_history(self, session, start=1, stop=None, raw=True):
146 127 """Retrieve input history from the database by session.
@@ -1245,7 +1245,7 b' class InteractiveShell(Configurable, Magic):'
1245 1245
1246 1246 def init_history(self):
1247 1247 """Sets up the command history, and starts regular autosaves."""
1248 self.history_manager = HistoryManager(shell=self)
1248 self.history_manager = HistoryManager(shell=self, config=self.config)
1249 1249
1250 1250 def history_saving_wrapper(self, func):
1251 1251 """ Wrap func for readline history saving
General Comments 0
You need to be logged in to leave comments. Login now