Skip to content

Commit

Permalink
Merge pull request ipython#940 from minrk/hist_file_config
Browse files Browse the repository at this point in the history
Allow setting HistoryManager.hist_file with config.  This fixes trait init in HistoryAccessor, which prevented setting hist_file via config from having any effect.
  • Loading branch information
fperez committed Oct 30, 2011
2 parents f76ea91 + 088742b commit 602e3c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
36 changes: 27 additions & 9 deletions IPython/core/history.py
Expand Up @@ -65,16 +65,31 @@ class HistoryAccessor(Configurable):
This is intended for use by standalone history tools. IPython shells use
HistoryManager, below, which is a subclass of this."""

# String holding the path to the history file
hist_file = Unicode(config=True)
hist_file = Unicode(config=True,
help="""Path to file to use for SQLite history database.
By default, IPython will put the history database in the IPython profile
directory. If you would rather share one history among profiles,
you ca set this value in each, so that they are consistent.
Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts.
If you see IPython hanging, try setting this to something on a local disk,
e.g::
ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
""")


# The SQLite database
if sqlite3:
db = Instance(sqlite3.Connection)
else:
db = Instance(DummyDB)

def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **traits):
def __init__(self, profile='default', hist_file=u'', config=None, **traits):
"""Create a new history accessor.
Parameters
Expand All @@ -84,15 +99,17 @@ def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **
hist_file : str
Path to an SQLite history database stored by IPython. If specified,
hist_file overrides profile.
shell :
InteractiveShell object, for use by HistoryManager subclass
config :
Config object. hist_file can also be set through this.
"""
# We need a pointer back to the shell for various tasks.
super(HistoryAccessor, self).__init__(shell=shell, config=config,
hist_file=hist_file, **traits)

super(HistoryAccessor, self).__init__(config=config, **traits)
# defer setting hist_file from kwarg until after init,
# otherwise the default kwarg value would clobber any value
# set by config
if hist_file:
self.hist_file = hist_file

if self.hist_file == u'':
# No one has set the hist_file, yet.
self.hist_file = self._get_hist_file_name(profile)
Expand All @@ -106,8 +123,9 @@ def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **
self.init_db()
except sqlite3.DatabaseError:
if os.path.isfile(self.hist_file):
# Try to move the file out of the way.
newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
# Try to move the file out of the way
base,ext = os.path.splitext(self.hist_file)
newpath = base + '-corrupt' + ext
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.")
Expand Down
14 changes: 14 additions & 0 deletions IPython/core/tests/test_history.py
Expand Up @@ -8,12 +8,15 @@
# stdlib
import os
import sys
import tempfile
import unittest
from datetime import datetime

# third party
import nose.tools as nt

# our own packages
from IPython.config.loader import Config
from IPython.utils.tempdir import TemporaryDirectory
from IPython.core.history import HistoryManager, extract_hist_ranges
from IPython.utils import py3compat
Expand Down Expand Up @@ -122,3 +125,14 @@ def test_timestamp_type():
ip = get_ipython()
info = ip.history_manager.get_session_info()
nt.assert_true(isinstance(info[1], datetime))

def test_hist_file_config():
cfg = Config()
tfile = tempfile.NamedTemporaryFile(delete=False)
cfg.HistoryManager.hist_file = tfile.name
try:
hm = HistoryManager(shell=get_ipython(), config=cfg)
nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
finally:
os.remove(tfile.name)

0 comments on commit 602e3c3

Please sign in to comment.