##// END OF EJS Templates
Merge pull request #679 from minrk/hist...
Fernando Perez -
r4603:9dfe8022 merge
parent child Browse files
Show More
@@ -129,7 +129,8 b' class HistoryManager(Configurable):'
129
129
130 def init_db(self):
130 def init_db(self):
131 """Connect to the database, and create tables if necessary."""
131 """Connect to the database, and create tables if necessary."""
132 self.db = sqlite3.connect(self.hist_file)
132 # use detect_types so that timestamps return datetime objects
133 self.db = sqlite3.connect(self.hist_file, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
133 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
134 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
134 primary key autoincrement, start timestamp,
135 primary key autoincrement, start timestamp,
135 end timestamp, num_cmds integer, remark text)""")
136 end timestamp, num_cmds integer, remark text)""")
@@ -213,6 +214,33 b' class HistoryManager(Configurable):'
213 return cur
214 return cur
214
215
215
216
217 def get_session_info(self, session=0):
218 """get info about a session
219
220 Parameters
221 ----------
222
223 session : int
224 Session number to retrieve. The current session is 0, and negative
225 numbers count back from current session, so -1 is previous session.
226
227 Returns
228 -------
229
230 (session_id [int], start [datetime], end [datetime], num_cmds [int], remark [unicode])
231
232 Sessions that are running or did not exit cleanly will have `end=None`
233 and `num_cmds=None`.
234
235 """
236
237 if session <= 0:
238 session += self.session_number
239
240 query = "SELECT * from sessions where session == ?"
241 return self.db.execute(query, (session,)).fetchone()
242
243
216 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
244 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
217 """Get the last n lines from the history database.
245 """Get the last n lines from the history database.
218
246
@@ -2552,6 +2552,11 b' class InteractiveShell(SingletonConfigurable, Magic):'
2552 code that has the appropriate information, rather than trying to
2552 code that has the appropriate information, rather than trying to
2553 clutter
2553 clutter
2554 """
2554 """
2555 # Close the history session (this stores the end time and line count)
2556 # this must be *before* the tempfile cleanup, in case of temporary
2557 # history db
2558 self.history_manager.end_session()
2559
2555 # Cleanup all tempfiles left around
2560 # Cleanup all tempfiles left around
2556 for tfile in self.tempfiles:
2561 for tfile in self.tempfiles:
2557 try:
2562 try:
@@ -2559,9 +2564,6 b' class InteractiveShell(SingletonConfigurable, Magic):'
2559 except OSError:
2564 except OSError:
2560 pass
2565 pass
2561
2566
2562 # Close the history session (this stores the end time and line count)
2563 self.history_manager.end_session()
2564
2565 # Clear all user namespaces to release all references cleanly.
2567 # Clear all user namespaces to release all references cleanly.
2566 self.reset(new_session=False)
2568 self.reset(new_session=False)
2567
2569
@@ -9,7 +9,7 b''
9 import os
9 import os
10 import sys
10 import sys
11 import unittest
11 import unittest
12
12 from datetime import datetime
13 # third party
13 # third party
14 import nose.tools as nt
14 import nose.tools as nt
15
15
@@ -107,3 +107,8 b' def test_magic_rerun():'
107 nt.assert_equal(ip.user_ns["a"], 11)
107 nt.assert_equal(ip.user_ns["a"], 11)
108 ip.run_cell("%rerun")
108 ip.run_cell("%rerun")
109 nt.assert_equal(ip.user_ns["a"], 12)
109 nt.assert_equal(ip.user_ns["a"], 12)
110
111 def test_timestamp_type():
112 ip = get_ipython()
113 info = ip.history_manager.get_session_info()
114 nt.assert_true(isinstance(info[1], datetime))
@@ -194,6 +194,9 b' def start_ipython():'
194
194
195 # A few more tweaks needed for playing nicely with doctests...
195 # A few more tweaks needed for playing nicely with doctests...
196
196
197 # remove history file
198 shell.tempfiles.append(config.HistoryManager.hist_file)
199
197 # These traps are normally only active for interactive use, set them
200 # These traps are normally only active for interactive use, set them
198 # permanently since we'll be mocking interactive sessions.
201 # permanently since we'll be mocking interactive sessions.
199 shell.builtin_trap.activate()
202 shell.builtin_trap.activate()
@@ -31,6 +31,7 b' from __future__ import absolute_import'
31 import os
31 import os
32 import re
32 import re
33 import sys
33 import sys
34 import tempfile
34
35
35 from contextlib import contextmanager
36 from contextlib import contextmanager
36
37
@@ -170,7 +171,7 b' def default_config():'
170 config.TerminalInteractiveShell.colors = 'NoColor'
171 config.TerminalInteractiveShell.colors = 'NoColor'
171 config.TerminalTerminalInteractiveShell.term_title = False,
172 config.TerminalTerminalInteractiveShell.term_title = False,
172 config.TerminalInteractiveShell.autocall = 0
173 config.TerminalInteractiveShell.autocall = 0
173 config.HistoryManager.hist_file = u'test_hist.sqlite'
174 config.HistoryManager.hist_file = tempfile.mktemp(u'test_hist.sqlite')
174 config.HistoryManager.db_cache_size = 10000
175 config.HistoryManager.db_cache_size = 10000
175 return config
176 return config
176
177
General Comments 0
You need to be logged in to leave comments. Login now