Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

History memory #312

Merged
merged 1 commit into from Mar 25, 2011
Jump to file or symbol
Failed to load files and symbols.
+41 −40
Split
View
@@ -47,15 +47,22 @@ class HistoryManager(Configurable):
input_hist_raw = List([""])
# A list of directories visited during session
dir_hist = List()
+ def _dir_hist_default(self):
@takluyver

takluyver Mar 25, 2011

Owner

Does this method get automatically picked up by traits to set the default?

@rkern

rkern Mar 25, 2011

Contributor

Yes.

+ try:
+ return [os.getcwd()]
+ except OSError:
+ return []
+
# A dict of output history, keyed with ints from the shell's
# execution count. If there are several outputs from one command,
# only the last one is stored.
output_hist = Dict()
# Contains all outputs, in lists of reprs.
- output_hist_reprs = Instance(defaultdict)
-
+ output_hist_reprs = Instance(defaultdict, args=(list,))
+
# String holding the path to the history file
- hist_file = Unicode()
+ hist_file = Unicode(config=True)
+
# The SQLite database
db = Instance(sqlite3.Connection)
# The number of the current session in the history database
@@ -73,47 +80,48 @@ class HistoryManager(Configurable):
# Variables used to store the three last inputs from the user. On each new
# history update, we populate the user's namespace with these, shifted as
# necessary.
- _i00, _i, _ii, _iii = '','','',''
+ _i00 = Unicode(u'')
+ _i = Unicode(u'')
+ _ii = Unicode(u'')
+ _iii = Unicode(u'')
# A set with all forms of the exit command, so that we don't store them in
# the history (it's annoying to rewind the first entry and land on an exit
# call).
- _exit_commands = None
-
- def __init__(self, shell, config=None):
+ _exit_commands = Instance(set, args=(['Quit', 'quit', 'Exit', 'exit',
+ '%Quit', '%quit', '%Exit', '%exit'],))
+
+ def __init__(self, shell, config=None, **traits):
"""Create a new history manager associated with a shell instance.
"""
# We need a pointer back to the shell for various tasks.
- super(HistoryManager, self).__init__(shell=shell, config=config)
+ super(HistoryManager, self).__init__(shell=shell, config=config,
+ **traits)
- # list of visited directories
- try:
- self.dir_hist = [os.getcwd()]
- except OSError:
- self.dir_hist = []
+ if self.hist_file == u'':
+ # No one has set the hist_file, yet.
+ if shell.profile:
+ histfname = 'history-%s' % shell.profile
+ else:
+ histfname = 'history'
+ self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
- # Now the history file
- if shell.profile:
- histfname = 'history-%s' % shell.profile
- else:
- histfname = 'history'
- self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
try:
self.init_db()
except sqlite3.DatabaseError:
- newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
- os.rename(self.hist_file, newpath)
- print("ERROR! History file wasn't a valid SQLite database.",
- "It was moved to %s" % newpath, "and a new file created.")
- self.init_db()
-
+ if os.path.isfile(self.hist_file):
+ # Try to move the file out of the way.
+ newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
+ os.rename(self.hist_file, newpath)
+ print("ERROR! History file wasn't a valid SQLite database.",
+ "It was moved to %s" % newpath, "and a new file created.")
+ self.init_db()
+ else:
+ # The hist_file is probably :memory: or something else.
+ raise
+
self.new_session()
-
- self._i00, self._i, self._ii, self._iii = '','','',''
- self.output_hist_reprs = defaultdict(list)
- self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
- '%quit', '%Exit', '%exit'])
def init_db(self):
"""Connect to the database, and create tables if necessary."""
@@ -23,18 +23,10 @@ def setUp():
def test_history():
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
- #tmpdir = '/software/temp'
- histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
- # Ensure that we restore the history management that we mess with in
- # this test doesn't affect the IPython instance used by the test suite
- # beyond this test.
+ # Make a new :memory: DB.
hist_manager_ori = ip.history_manager
try:
- ip.history_manager = HistoryManager(shell=ip)
- ip.history_manager.hist_file = histfile
- ip.history_manager.init_db() # Has to be called after changing file
- ip.history_manager.reset()
- print 'test',histfile
+ ip.history_manager = HistoryManager(shell=ip, hist_file=':memory:')
hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
View
@@ -167,6 +167,7 @@ def default_config():
config.TerminalInteractiveShell.colors = 'NoColor'
config.TerminalTerminalInteractiveShell.term_title = False,
config.TerminalInteractiveShell.autocall = 0
+ config.HistoryManager.hist_file = u':memory:'
return config