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