##// END OF EJS Templates
allow setting HistoryManager.hist_file with config...
MinRK -
Show More
@@ -65,8 +65,23 b' class HistoryAccessor(Configurable):'
65
65
66 This is intended for use by standalone history tools. IPython shells use
66 This is intended for use by standalone history tools. IPython shells use
67 HistoryManager, below, which is a subclass of this."""
67 HistoryManager, below, which is a subclass of this."""
68
68 # String holding the path to the history file
69 # String holding the path to the history file
69 hist_file = Unicode(config=True)
70 hist_file = Unicode(config=True,
71 help="""Path to file to use for SQLite history database.
72
73 By default, IPython will put the history database in the IPython profile
74 directory. If you would rather share one history among profiles,
75 you ca set this value in each, so that they are consistent.
76
77 Due to an issue with fcntl, SQLite is known to misbehave on some NFS mounts.
78 If you see IPython hanging, try setting this to something on a local disk,
79 e.g::
80
81 ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
82
83 """)
84
70
85
71 # The SQLite database
86 # The SQLite database
72 if sqlite3:
87 if sqlite3:
@@ -74,7 +89,7 b' class HistoryAccessor(Configurable):'
74 else:
89 else:
75 db = Instance(DummyDB)
90 db = Instance(DummyDB)
76
91
77 def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **traits):
92 def __init__(self, profile='default', hist_file=u'', config=None, **traits):
78 """Create a new history accessor.
93 """Create a new history accessor.
79
94
80 Parameters
95 Parameters
@@ -84,15 +99,17 b' class HistoryAccessor(Configurable):'
84 hist_file : str
99 hist_file : str
85 Path to an SQLite history database stored by IPython. If specified,
100 Path to an SQLite history database stored by IPython. If specified,
86 hist_file overrides profile.
101 hist_file overrides profile.
87 shell :
88 InteractiveShell object, for use by HistoryManager subclass
89 config :
102 config :
90 Config object. hist_file can also be set through this.
103 Config object. hist_file can also be set through this.
91 """
104 """
92 # We need a pointer back to the shell for various tasks.
105 # We need a pointer back to the shell for various tasks.
93 super(HistoryAccessor, self).__init__(shell=shell, config=config,
106 super(HistoryAccessor, self).__init__(config=config, **traits)
94 hist_file=hist_file, **traits)
107 # defer setting hist_file from kwarg until after init,
95
108 # otherwise the default kwarg value would clobber any value
109 # set by config
110 if hist_file:
111 self.hist_file = hist_file
112
96 if self.hist_file == u'':
113 if self.hist_file == u'':
97 # No one has set the hist_file, yet.
114 # No one has set the hist_file, yet.
98 self.hist_file = self._get_hist_file_name(profile)
115 self.hist_file = self._get_hist_file_name(profile)
@@ -106,8 +123,9 b' class HistoryAccessor(Configurable):'
106 self.init_db()
123 self.init_db()
107 except sqlite3.DatabaseError:
124 except sqlite3.DatabaseError:
108 if os.path.isfile(self.hist_file):
125 if os.path.isfile(self.hist_file):
109 # Try to move the file out of the way.
126 # Try to move the file out of the way
110 newpath = os.path.join(self.shell.profile_dir.location, "hist-corrupt.sqlite")
127 base,ext = os.path.splitext(self.hist_file)
128 newpath = base + '-corrupt' + ext
111 os.rename(self.hist_file, newpath)
129 os.rename(self.hist_file, newpath)
112 print("ERROR! History file wasn't a valid SQLite database.",
130 print("ERROR! History file wasn't a valid SQLite database.",
113 "It was moved to %s" % newpath, "and a new file created.")
131 "It was moved to %s" % newpath, "and a new file created.")
@@ -8,12 +8,15 b''
8 # stdlib
8 # stdlib
9 import os
9 import os
10 import sys
10 import sys
11 import tempfile
11 import unittest
12 import unittest
12 from datetime import datetime
13 from datetime import datetime
14
13 # third party
15 # third party
14 import nose.tools as nt
16 import nose.tools as nt
15
17
16 # our own packages
18 # our own packages
19 from IPython.config.loader import Config
17 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.utils.tempdir import TemporaryDirectory
18 from IPython.core.history import HistoryManager, extract_hist_ranges
21 from IPython.core.history import HistoryManager, extract_hist_ranges
19 from IPython.utils import py3compat
22 from IPython.utils import py3compat
@@ -122,3 +125,10 b' def test_timestamp_type():'
122 ip = get_ipython()
125 ip = get_ipython()
123 info = ip.history_manager.get_session_info()
126 info = ip.history_manager.get_session_info()
124 nt.assert_true(isinstance(info[1], datetime))
127 nt.assert_true(isinstance(info[1], datetime))
128
129 def test_hist_file_config():
130 cfg = Config()
131 cfg.HistoryManager.hist_file = tempfile.mktemp()
132 hm = HistoryManager(shell=get_ipython(), config=cfg)
133 nt.assert_equals(hm.hist_file, cfg.HistoryManager.hist_file)
134
General Comments 0
You need to be logged in to leave comments. Login now