##// END OF EJS Templates
Empty histrange means history of current session...
Blazej Michalik -
Show More
@@ -1,897 +1,907 b''
1 """ History related magics and functionality """
1 """ History related magics and functionality """
2
2
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6
6
7 import atexit
7 import atexit
8 import datetime
8 import datetime
9 from pathlib import Path
9 from pathlib import Path
10 import re
10 import re
11 import sqlite3
11 import sqlite3
12 import threading
12 import threading
13
13
14 from traitlets.config.configurable import LoggingConfigurable
14 from traitlets.config.configurable import LoggingConfigurable
15 from decorator import decorator
15 from decorator import decorator
16 from IPython.utils.decorators import undoc
16 from IPython.utils.decorators import undoc
17 from IPython.paths import locate_profile
17 from IPython.paths import locate_profile
18 from traitlets import (
18 from traitlets import (
19 Any,
19 Any,
20 Bool,
20 Bool,
21 Dict,
21 Dict,
22 Instance,
22 Instance,
23 Integer,
23 Integer,
24 List,
24 List,
25 Unicode,
25 Unicode,
26 Union,
26 Union,
27 TraitError,
27 TraitError,
28 default,
28 default,
29 observe,
29 observe,
30 )
30 )
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Classes and functions
33 # Classes and functions
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 @undoc
36 @undoc
37 class DummyDB(object):
37 class DummyDB(object):
38 """Dummy DB that will act as a black hole for history.
38 """Dummy DB that will act as a black hole for history.
39
39
40 Only used in the absence of sqlite"""
40 Only used in the absence of sqlite"""
41 def execute(*args, **kwargs):
41 def execute(*args, **kwargs):
42 return []
42 return []
43
43
44 def commit(self, *args, **kwargs):
44 def commit(self, *args, **kwargs):
45 pass
45 pass
46
46
47 def __enter__(self, *args, **kwargs):
47 def __enter__(self, *args, **kwargs):
48 pass
48 pass
49
49
50 def __exit__(self, *args, **kwargs):
50 def __exit__(self, *args, **kwargs):
51 pass
51 pass
52
52
53
53
54 @decorator
54 @decorator
55 def only_when_enabled(f, self, *a, **kw):
55 def only_when_enabled(f, self, *a, **kw):
56 """Decorator: return an empty list in the absence of sqlite."""
56 """Decorator: return an empty list in the absence of sqlite."""
57 if not self.enabled:
57 if not self.enabled:
58 return []
58 return []
59 else:
59 else:
60 return f(self, *a, **kw)
60 return f(self, *a, **kw)
61
61
62
62
63 # use 16kB as threshold for whether a corrupt history db should be saved
63 # use 16kB as threshold for whether a corrupt history db should be saved
64 # that should be at least 100 entries or so
64 # that should be at least 100 entries or so
65 _SAVE_DB_SIZE = 16384
65 _SAVE_DB_SIZE = 16384
66
66
67 @decorator
67 @decorator
68 def catch_corrupt_db(f, self, *a, **kw):
68 def catch_corrupt_db(f, self, *a, **kw):
69 """A decorator which wraps HistoryAccessor method calls to catch errors from
69 """A decorator which wraps HistoryAccessor method calls to catch errors from
70 a corrupt SQLite database, move the old database out of the way, and create
70 a corrupt SQLite database, move the old database out of the way, and create
71 a new one.
71 a new one.
72
72
73 We avoid clobbering larger databases because this may be triggered due to filesystem issues,
73 We avoid clobbering larger databases because this may be triggered due to filesystem issues,
74 not just a corrupt file.
74 not just a corrupt file.
75 """
75 """
76 try:
76 try:
77 return f(self, *a, **kw)
77 return f(self, *a, **kw)
78 except (sqlite3.DatabaseError, sqlite3.OperationalError) as e:
78 except (sqlite3.DatabaseError, sqlite3.OperationalError) as e:
79 self._corrupt_db_counter += 1
79 self._corrupt_db_counter += 1
80 self.log.error("Failed to open SQLite history %s (%s).", self.hist_file, e)
80 self.log.error("Failed to open SQLite history %s (%s).", self.hist_file, e)
81 if self.hist_file != ':memory:':
81 if self.hist_file != ':memory:':
82 if self._corrupt_db_counter > self._corrupt_db_limit:
82 if self._corrupt_db_counter > self._corrupt_db_limit:
83 self.hist_file = ':memory:'
83 self.hist_file = ':memory:'
84 self.log.error("Failed to load history too many times, history will not be saved.")
84 self.log.error("Failed to load history too many times, history will not be saved.")
85 elif self.hist_file.is_file():
85 elif self.hist_file.is_file():
86 # move the file out of the way
86 # move the file out of the way
87 base = str(self.hist_file.parent / self.hist_file.stem)
87 base = str(self.hist_file.parent / self.hist_file.stem)
88 ext = self.hist_file.suffix
88 ext = self.hist_file.suffix
89 size = self.hist_file.stat().st_size
89 size = self.hist_file.stat().st_size
90 if size >= _SAVE_DB_SIZE:
90 if size >= _SAVE_DB_SIZE:
91 # if there's significant content, avoid clobbering
91 # if there's significant content, avoid clobbering
92 now = datetime.datetime.now().isoformat().replace(':', '.')
92 now = datetime.datetime.now().isoformat().replace(':', '.')
93 newpath = base + '-corrupt-' + now + ext
93 newpath = base + '-corrupt-' + now + ext
94 # don't clobber previous corrupt backups
94 # don't clobber previous corrupt backups
95 for i in range(100):
95 for i in range(100):
96 if not Path(newpath).exists():
96 if not Path(newpath).exists():
97 break
97 break
98 else:
98 else:
99 newpath = base + '-corrupt-' + now + (u'-%i' % i) + ext
99 newpath = base + '-corrupt-' + now + (u'-%i' % i) + ext
100 else:
100 else:
101 # not much content, possibly empty; don't worry about clobbering
101 # not much content, possibly empty; don't worry about clobbering
102 # maybe we should just delete it?
102 # maybe we should just delete it?
103 newpath = base + '-corrupt' + ext
103 newpath = base + '-corrupt' + ext
104 self.hist_file.rename(newpath)
104 self.hist_file.rename(newpath)
105 self.log.error("History file was moved to %s and a new file created.", newpath)
105 self.log.error("History file was moved to %s and a new file created.", newpath)
106 self.init_db()
106 self.init_db()
107 return []
107 return []
108 else:
108 else:
109 # Failed with :memory:, something serious is wrong
109 # Failed with :memory:, something serious is wrong
110 raise
110 raise
111
111
112
112
113 class HistoryAccessorBase(LoggingConfigurable):
113 class HistoryAccessorBase(LoggingConfigurable):
114 """An abstract class for History Accessors """
114 """An abstract class for History Accessors """
115
115
116 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
116 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
117 raise NotImplementedError
117 raise NotImplementedError
118
118
119 def search(self, pattern="*", raw=True, search_raw=True,
119 def search(self, pattern="*", raw=True, search_raw=True,
120 output=False, n=None, unique=False):
120 output=False, n=None, unique=False):
121 raise NotImplementedError
121 raise NotImplementedError
122
122
123 def get_range(self, session, start=1, stop=None, raw=True,output=False):
123 def get_range(self, session, start=1, stop=None, raw=True,output=False):
124 raise NotImplementedError
124 raise NotImplementedError
125
125
126 def get_range_by_str(self, rangestr, raw=True, output=False):
126 def get_range_by_str(self, rangestr, raw=True, output=False):
127 raise NotImplementedError
127 raise NotImplementedError
128
128
129
129
130 class HistoryAccessor(HistoryAccessorBase):
130 class HistoryAccessor(HistoryAccessorBase):
131 """Access the history database without adding to it.
131 """Access the history database without adding to it.
132
132
133 This is intended for use by standalone history tools. IPython shells use
133 This is intended for use by standalone history tools. IPython shells use
134 HistoryManager, below, which is a subclass of this."""
134 HistoryManager, below, which is a subclass of this."""
135
135
136 # counter for init_db retries, so we don't keep trying over and over
136 # counter for init_db retries, so we don't keep trying over and over
137 _corrupt_db_counter = 0
137 _corrupt_db_counter = 0
138 # after two failures, fallback on :memory:
138 # after two failures, fallback on :memory:
139 _corrupt_db_limit = 2
139 _corrupt_db_limit = 2
140
140
141 # String holding the path to the history file
141 # String holding the path to the history file
142 hist_file = Union(
142 hist_file = Union(
143 [Instance(Path), Unicode()],
143 [Instance(Path), Unicode()],
144 help="""Path to file to use for SQLite history database.
144 help="""Path to file to use for SQLite history database.
145
145
146 By default, IPython will put the history database in the IPython
146 By default, IPython will put the history database in the IPython
147 profile directory. If you would rather share one history among
147 profile directory. If you would rather share one history among
148 profiles, you can set this value in each, so that they are consistent.
148 profiles, you can set this value in each, so that they are consistent.
149
149
150 Due to an issue with fcntl, SQLite is known to misbehave on some NFS
150 Due to an issue with fcntl, SQLite is known to misbehave on some NFS
151 mounts. If you see IPython hanging, try setting this to something on a
151 mounts. If you see IPython hanging, try setting this to something on a
152 local disk, e.g::
152 local disk, e.g::
153
153
154 ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
154 ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
155
155
156 you can also use the specific value `:memory:` (including the colon
156 you can also use the specific value `:memory:` (including the colon
157 at both end but not the back ticks), to avoid creating an history file.
157 at both end but not the back ticks), to avoid creating an history file.
158
158
159 """,
159 """,
160 ).tag(config=True)
160 ).tag(config=True)
161
161
162 enabled = Bool(True,
162 enabled = Bool(True,
163 help="""enable the SQLite history
163 help="""enable the SQLite history
164
164
165 set enabled=False to disable the SQLite history,
165 set enabled=False to disable the SQLite history,
166 in which case there will be no stored history, no SQLite connection,
166 in which case there will be no stored history, no SQLite connection,
167 and no background saving thread. This may be necessary in some
167 and no background saving thread. This may be necessary in some
168 threaded environments where IPython is embedded.
168 threaded environments where IPython is embedded.
169 """
169 """
170 ).tag(config=True)
170 ).tag(config=True)
171
171
172 connection_options = Dict(
172 connection_options = Dict(
173 help="""Options for configuring the SQLite connection
173 help="""Options for configuring the SQLite connection
174
174
175 These options are passed as keyword args to sqlite3.connect
175 These options are passed as keyword args to sqlite3.connect
176 when establishing database connections.
176 when establishing database connections.
177 """
177 """
178 ).tag(config=True)
178 ).tag(config=True)
179
179
180 # The SQLite database
180 # The SQLite database
181 db = Any()
181 db = Any()
182 @observe('db')
182 @observe('db')
183 def _db_changed(self, change):
183 def _db_changed(self, change):
184 """validate the db, since it can be an Instance of two different types"""
184 """validate the db, since it can be an Instance of two different types"""
185 new = change['new']
185 new = change['new']
186 connection_types = (DummyDB, sqlite3.Connection)
186 connection_types = (DummyDB, sqlite3.Connection)
187 if not isinstance(new, connection_types):
187 if not isinstance(new, connection_types):
188 msg = "%s.db must be sqlite3 Connection or DummyDB, not %r" % \
188 msg = "%s.db must be sqlite3 Connection or DummyDB, not %r" % \
189 (self.__class__.__name__, new)
189 (self.__class__.__name__, new)
190 raise TraitError(msg)
190 raise TraitError(msg)
191
191
192 def __init__(self, profile="default", hist_file="", **traits):
192 def __init__(self, profile="default", hist_file="", **traits):
193 """Create a new history accessor.
193 """Create a new history accessor.
194
194
195 Parameters
195 Parameters
196 ----------
196 ----------
197 profile : str
197 profile : str
198 The name of the profile from which to open history.
198 The name of the profile from which to open history.
199 hist_file : str
199 hist_file : str
200 Path to an SQLite history database stored by IPython. If specified,
200 Path to an SQLite history database stored by IPython. If specified,
201 hist_file overrides profile.
201 hist_file overrides profile.
202 config : :class:`~traitlets.config.loader.Config`
202 config : :class:`~traitlets.config.loader.Config`
203 Config object. hist_file can also be set through this.
203 Config object. hist_file can also be set through this.
204 """
204 """
205 # We need a pointer back to the shell for various tasks.
205 # We need a pointer back to the shell for various tasks.
206 super(HistoryAccessor, self).__init__(**traits)
206 super(HistoryAccessor, self).__init__(**traits)
207 # defer setting hist_file from kwarg until after init,
207 # defer setting hist_file from kwarg until after init,
208 # otherwise the default kwarg value would clobber any value
208 # otherwise the default kwarg value would clobber any value
209 # set by config
209 # set by config
210 if hist_file:
210 if hist_file:
211 self.hist_file = hist_file
211 self.hist_file = hist_file
212
212
213 try:
213 try:
214 self.hist_file
214 self.hist_file
215 except TraitError:
215 except TraitError:
216 # No one has set the hist_file, yet.
216 # No one has set the hist_file, yet.
217 self.hist_file = self._get_hist_file_name(profile)
217 self.hist_file = self._get_hist_file_name(profile)
218
218
219 self.init_db()
219 self.init_db()
220
220
221 def _get_hist_file_name(self, profile='default'):
221 def _get_hist_file_name(self, profile='default'):
222 """Find the history file for the given profile name.
222 """Find the history file for the given profile name.
223
223
224 This is overridden by the HistoryManager subclass, to use the shell's
224 This is overridden by the HistoryManager subclass, to use the shell's
225 active profile.
225 active profile.
226
226
227 Parameters
227 Parameters
228 ----------
228 ----------
229 profile : str
229 profile : str
230 The name of a profile which has a history file.
230 The name of a profile which has a history file.
231 """
231 """
232 return Path(locate_profile(profile)) / "history.sqlite"
232 return Path(locate_profile(profile)) / "history.sqlite"
233
233
234 @catch_corrupt_db
234 @catch_corrupt_db
235 def init_db(self):
235 def init_db(self):
236 """Connect to the database, and create tables if necessary."""
236 """Connect to the database, and create tables if necessary."""
237 if not self.enabled:
237 if not self.enabled:
238 self.db = DummyDB()
238 self.db = DummyDB()
239 return
239 return
240
240
241 # use detect_types so that timestamps return datetime objects
241 # use detect_types so that timestamps return datetime objects
242 kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
242 kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
243 kwargs.update(self.connection_options)
243 kwargs.update(self.connection_options)
244 self.db = sqlite3.connect(str(self.hist_file), **kwargs)
244 self.db = sqlite3.connect(str(self.hist_file), **kwargs)
245 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
245 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
246 primary key autoincrement, start timestamp,
246 primary key autoincrement, start timestamp,
247 end timestamp, num_cmds integer, remark text)""")
247 end timestamp, num_cmds integer, remark text)""")
248 self.db.execute("""CREATE TABLE IF NOT EXISTS history
248 self.db.execute("""CREATE TABLE IF NOT EXISTS history
249 (session integer, line integer, source text, source_raw text,
249 (session integer, line integer, source text, source_raw text,
250 PRIMARY KEY (session, line))""")
250 PRIMARY KEY (session, line))""")
251 # Output history is optional, but ensure the table's there so it can be
251 # Output history is optional, but ensure the table's there so it can be
252 # enabled later.
252 # enabled later.
253 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
253 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
254 (session integer, line integer, output text,
254 (session integer, line integer, output text,
255 PRIMARY KEY (session, line))""")
255 PRIMARY KEY (session, line))""")
256 self.db.commit()
256 self.db.commit()
257 # success! reset corrupt db count
257 # success! reset corrupt db count
258 self._corrupt_db_counter = 0
258 self._corrupt_db_counter = 0
259
259
260 def writeout_cache(self):
260 def writeout_cache(self):
261 """Overridden by HistoryManager to dump the cache before certain
261 """Overridden by HistoryManager to dump the cache before certain
262 database lookups."""
262 database lookups."""
263 pass
263 pass
264
264
265 ## -------------------------------
265 ## -------------------------------
266 ## Methods for retrieving history:
266 ## Methods for retrieving history:
267 ## -------------------------------
267 ## -------------------------------
268 def _run_sql(self, sql, params, raw=True, output=False):
268 def _run_sql(self, sql, params, raw=True, output=False):
269 """Prepares and runs an SQL query for the history database.
269 """Prepares and runs an SQL query for the history database.
270
270
271 Parameters
271 Parameters
272 ----------
272 ----------
273 sql : str
273 sql : str
274 Any filtering expressions to go after SELECT ... FROM ...
274 Any filtering expressions to go after SELECT ... FROM ...
275 params : tuple
275 params : tuple
276 Parameters passed to the SQL query (to replace "?")
276 Parameters passed to the SQL query (to replace "?")
277 raw, output : bool
277 raw, output : bool
278 See :meth:`get_range`
278 See :meth:`get_range`
279
279
280 Returns
280 Returns
281 -------
281 -------
282 Tuples as :meth:`get_range`
282 Tuples as :meth:`get_range`
283 """
283 """
284 toget = 'source_raw' if raw else 'source'
284 toget = 'source_raw' if raw else 'source'
285 sqlfrom = "history"
285 sqlfrom = "history"
286 if output:
286 if output:
287 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
287 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
288 toget = "history.%s, output_history.output" % toget
288 toget = "history.%s, output_history.output" % toget
289 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
289 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
290 (toget, sqlfrom) + sql, params)
290 (toget, sqlfrom) + sql, params)
291 if output: # Regroup into 3-tuples, and parse JSON
291 if output: # Regroup into 3-tuples, and parse JSON
292 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
292 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
293 return cur
293 return cur
294
294
295 @only_when_enabled
295 @only_when_enabled
296 @catch_corrupt_db
296 @catch_corrupt_db
297 def get_session_info(self, session):
297 def get_session_info(self, session):
298 """Get info about a session.
298 """Get info about a session.
299
299
300 Parameters
300 Parameters
301 ----------
301 ----------
302
302
303 session : int
303 session : int
304 Session number to retrieve.
304 Session number to retrieve.
305
305
306 Returns
306 Returns
307 -------
307 -------
308
308
309 session_id : int
309 session_id : int
310 Session ID number
310 Session ID number
311 start : datetime
311 start : datetime
312 Timestamp for the start of the session.
312 Timestamp for the start of the session.
313 end : datetime
313 end : datetime
314 Timestamp for the end of the session, or None if IPython crashed.
314 Timestamp for the end of the session, or None if IPython crashed.
315 num_cmds : int
315 num_cmds : int
316 Number of commands run, or None if IPython crashed.
316 Number of commands run, or None if IPython crashed.
317 remark : unicode
317 remark : unicode
318 A manually set description.
318 A manually set description.
319 """
319 """
320 query = "SELECT * from sessions where session == ?"
320 query = "SELECT * from sessions where session == ?"
321 return self.db.execute(query, (session,)).fetchone()
321 return self.db.execute(query, (session,)).fetchone()
322
322
323 @catch_corrupt_db
323 @catch_corrupt_db
324 def get_last_session_id(self):
324 def get_last_session_id(self):
325 """Get the last session ID currently in the database.
325 """Get the last session ID currently in the database.
326
326
327 Within IPython, this should be the same as the value stored in
327 Within IPython, this should be the same as the value stored in
328 :attr:`HistoryManager.session_number`.
328 :attr:`HistoryManager.session_number`.
329 """
329 """
330 for record in self.get_tail(n=1, include_latest=True):
330 for record in self.get_tail(n=1, include_latest=True):
331 return record[0]
331 return record[0]
332
332
333 @catch_corrupt_db
333 @catch_corrupt_db
334 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
334 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
335 """Get the last n lines from the history database.
335 """Get the last n lines from the history database.
336
336
337 Parameters
337 Parameters
338 ----------
338 ----------
339 n : int
339 n : int
340 The number of lines to get
340 The number of lines to get
341 raw, output : bool
341 raw, output : bool
342 See :meth:`get_range`
342 See :meth:`get_range`
343 include_latest : bool
343 include_latest : bool
344 If False (default), n+1 lines are fetched, and the latest one
344 If False (default), n+1 lines are fetched, and the latest one
345 is discarded. This is intended to be used where the function
345 is discarded. This is intended to be used where the function
346 is called by a user command, which it should not return.
346 is called by a user command, which it should not return.
347
347
348 Returns
348 Returns
349 -------
349 -------
350 Tuples as :meth:`get_range`
350 Tuples as :meth:`get_range`
351 """
351 """
352 self.writeout_cache()
352 self.writeout_cache()
353 if not include_latest:
353 if not include_latest:
354 n += 1
354 n += 1
355 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
355 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
356 (n,), raw=raw, output=output)
356 (n,), raw=raw, output=output)
357 if not include_latest:
357 if not include_latest:
358 return reversed(list(cur)[1:])
358 return reversed(list(cur)[1:])
359 return reversed(list(cur))
359 return reversed(list(cur))
360
360
361 @catch_corrupt_db
361 @catch_corrupt_db
362 def search(self, pattern="*", raw=True, search_raw=True,
362 def search(self, pattern="*", raw=True, search_raw=True,
363 output=False, n=None, unique=False):
363 output=False, n=None, unique=False):
364 """Search the database using unix glob-style matching (wildcards
364 """Search the database using unix glob-style matching (wildcards
365 * and ?).
365 * and ?).
366
366
367 Parameters
367 Parameters
368 ----------
368 ----------
369 pattern : str
369 pattern : str
370 The wildcarded pattern to match when searching
370 The wildcarded pattern to match when searching
371 search_raw : bool
371 search_raw : bool
372 If True, search the raw input, otherwise, the parsed input
372 If True, search the raw input, otherwise, the parsed input
373 raw, output : bool
373 raw, output : bool
374 See :meth:`get_range`
374 See :meth:`get_range`
375 n : None or int
375 n : None or int
376 If an integer is given, it defines the limit of
376 If an integer is given, it defines the limit of
377 returned entries.
377 returned entries.
378 unique : bool
378 unique : bool
379 When it is true, return only unique entries.
379 When it is true, return only unique entries.
380
380
381 Returns
381 Returns
382 -------
382 -------
383 Tuples as :meth:`get_range`
383 Tuples as :meth:`get_range`
384 """
384 """
385 tosearch = "source_raw" if search_raw else "source"
385 tosearch = "source_raw" if search_raw else "source"
386 if output:
386 if output:
387 tosearch = "history." + tosearch
387 tosearch = "history." + tosearch
388 self.writeout_cache()
388 self.writeout_cache()
389 sqlform = "WHERE %s GLOB ?" % tosearch
389 sqlform = "WHERE %s GLOB ?" % tosearch
390 params = (pattern,)
390 params = (pattern,)
391 if unique:
391 if unique:
392 sqlform += ' GROUP BY {0}'.format(tosearch)
392 sqlform += ' GROUP BY {0}'.format(tosearch)
393 if n is not None:
393 if n is not None:
394 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
394 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
395 params += (n,)
395 params += (n,)
396 elif unique:
396 elif unique:
397 sqlform += " ORDER BY session, line"
397 sqlform += " ORDER BY session, line"
398 cur = self._run_sql(sqlform, params, raw=raw, output=output)
398 cur = self._run_sql(sqlform, params, raw=raw, output=output)
399 if n is not None:
399 if n is not None:
400 return reversed(list(cur))
400 return reversed(list(cur))
401 return cur
401 return cur
402
402
403 @catch_corrupt_db
403 @catch_corrupt_db
404 def get_range(self, session, start=1, stop=None, raw=True,output=False):
404 def get_range(self, session, start=1, stop=None, raw=True,output=False):
405 """Retrieve input by session.
405 """Retrieve input by session.
406
406
407 Parameters
407 Parameters
408 ----------
408 ----------
409 session : int
409 session : int
410 Session number to retrieve.
410 Session number to retrieve.
411 start : int
411 start : int
412 First line to retrieve.
412 First line to retrieve.
413 stop : int
413 stop : int
414 End of line range (excluded from output itself). If None, retrieve
414 End of line range (excluded from output itself). If None, retrieve
415 to the end of the session.
415 to the end of the session.
416 raw : bool
416 raw : bool
417 If True, return untranslated input
417 If True, return untranslated input
418 output : bool
418 output : bool
419 If True, attempt to include output. This will be 'real' Python
419 If True, attempt to include output. This will be 'real' Python
420 objects for the current session, or text reprs from previous
420 objects for the current session, or text reprs from previous
421 sessions if db_log_output was enabled at the time. Where no output
421 sessions if db_log_output was enabled at the time. Where no output
422 is found, None is used.
422 is found, None is used.
423
423
424 Returns
424 Returns
425 -------
425 -------
426 entries
426 entries
427 An iterator over the desired lines. Each line is a 3-tuple, either
427 An iterator over the desired lines. Each line is a 3-tuple, either
428 (session, line, input) if output is False, or
428 (session, line, input) if output is False, or
429 (session, line, (input, output)) if output is True.
429 (session, line, (input, output)) if output is True.
430 """
430 """
431 if stop:
431 if stop:
432 lineclause = "line >= ? AND line < ?"
432 lineclause = "line >= ? AND line < ?"
433 params = (session, start, stop)
433 params = (session, start, stop)
434 else:
434 else:
435 lineclause = "line>=?"
435 lineclause = "line>=?"
436 params = (session, start)
436 params = (session, start)
437
437
438 return self._run_sql("WHERE session==? AND %s" % lineclause,
438 return self._run_sql("WHERE session==? AND %s" % lineclause,
439 params, raw=raw, output=output)
439 params, raw=raw, output=output)
440
440
441 def get_range_by_str(self, rangestr, raw=True, output=False):
441 def get_range_by_str(self, rangestr, raw=True, output=False):
442 """Get lines of history from a string of ranges, as used by magic
442 """Get lines of history from a string of ranges, as used by magic
443 commands %hist, %save, %macro, etc.
443 commands %hist, %save, %macro, etc.
444
444
445 Parameters
445 Parameters
446 ----------
446 ----------
447 rangestr : str
447 rangestr : str
448 A string specifying ranges, e.g. "5 ~2/1-4". See
448 A string specifying ranges, e.g. "5 ~2/1-4". If empty string is used,
449 :func:`magic_history` for full details.
449 this will return everything from current session's history.
450
451 See the documentation of :func:`%history` for the full details.
452
450 raw, output : bool
453 raw, output : bool
451 As :meth:`get_range`
454 As :meth:`get_range`
452
455
453 Returns
456 Returns
454 -------
457 -------
455 Tuples as :meth:`get_range`
458 Tuples as :meth:`get_range`
456 """
459 """
457 for sess, s, e in extract_hist_ranges(rangestr):
460 for sess, s, e in extract_hist_ranges(rangestr):
458 for line in self.get_range(sess, s, e, raw=raw, output=output):
461 for line in self.get_range(sess, s, e, raw=raw, output=output):
459 yield line
462 yield line
460
463
461
464
462 class HistoryManager(HistoryAccessor):
465 class HistoryManager(HistoryAccessor):
463 """A class to organize all history-related functionality in one place.
466 """A class to organize all history-related functionality in one place.
464 """
467 """
465 # Public interface
468 # Public interface
466
469
467 # An instance of the IPython shell we are attached to
470 # An instance of the IPython shell we are attached to
468 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
471 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
469 allow_none=True)
472 allow_none=True)
470 # Lists to hold processed and raw history. These start with a blank entry
473 # Lists to hold processed and raw history. These start with a blank entry
471 # so that we can index them starting from 1
474 # so that we can index them starting from 1
472 input_hist_parsed = List([""])
475 input_hist_parsed = List([""])
473 input_hist_raw = List([""])
476 input_hist_raw = List([""])
474 # A list of directories visited during session
477 # A list of directories visited during session
475 dir_hist = List()
478 dir_hist = List()
476 @default('dir_hist')
479 @default('dir_hist')
477 def _dir_hist_default(self):
480 def _dir_hist_default(self):
478 try:
481 try:
479 return [Path.cwd()]
482 return [Path.cwd()]
480 except OSError:
483 except OSError:
481 return []
484 return []
482
485
483 # A dict of output history, keyed with ints from the shell's
486 # A dict of output history, keyed with ints from the shell's
484 # execution count.
487 # execution count.
485 output_hist = Dict()
488 output_hist = Dict()
486 # The text/plain repr of outputs.
489 # The text/plain repr of outputs.
487 output_hist_reprs = Dict()
490 output_hist_reprs = Dict()
488
491
489 # The number of the current session in the history database
492 # The number of the current session in the history database
490 session_number = Integer()
493 session_number = Integer()
491
494
492 db_log_output = Bool(False,
495 db_log_output = Bool(False,
493 help="Should the history database include output? (default: no)"
496 help="Should the history database include output? (default: no)"
494 ).tag(config=True)
497 ).tag(config=True)
495 db_cache_size = Integer(0,
498 db_cache_size = Integer(0,
496 help="Write to database every x commands (higher values save disk access & power).\n"
499 help="Write to database every x commands (higher values save disk access & power).\n"
497 "Values of 1 or less effectively disable caching."
500 "Values of 1 or less effectively disable caching."
498 ).tag(config=True)
501 ).tag(config=True)
499 # The input and output caches
502 # The input and output caches
500 db_input_cache = List()
503 db_input_cache = List()
501 db_output_cache = List()
504 db_output_cache = List()
502
505
503 # History saving in separate thread
506 # History saving in separate thread
504 save_thread = Instance('IPython.core.history.HistorySavingThread',
507 save_thread = Instance('IPython.core.history.HistorySavingThread',
505 allow_none=True)
508 allow_none=True)
506 save_flag = Instance(threading.Event, allow_none=True)
509 save_flag = Instance(threading.Event, allow_none=True)
507
510
508 # Private interface
511 # Private interface
509 # Variables used to store the three last inputs from the user. On each new
512 # Variables used to store the three last inputs from the user. On each new
510 # history update, we populate the user's namespace with these, shifted as
513 # history update, we populate the user's namespace with these, shifted as
511 # necessary.
514 # necessary.
512 _i00 = Unicode(u'')
515 _i00 = Unicode(u'')
513 _i = Unicode(u'')
516 _i = Unicode(u'')
514 _ii = Unicode(u'')
517 _ii = Unicode(u'')
515 _iii = Unicode(u'')
518 _iii = Unicode(u'')
516
519
517 # A regex matching all forms of the exit command, so that we don't store
520 # A regex matching all forms of the exit command, so that we don't store
518 # them in the history (it's annoying to rewind the first entry and land on
521 # them in the history (it's annoying to rewind the first entry and land on
519 # an exit call).
522 # an exit call).
520 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
523 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
521
524
522 def __init__(self, shell=None, config=None, **traits):
525 def __init__(self, shell=None, config=None, **traits):
523 """Create a new history manager associated with a shell instance.
526 """Create a new history manager associated with a shell instance.
524 """
527 """
525 # We need a pointer back to the shell for various tasks.
528 # We need a pointer back to the shell for various tasks.
526 super(HistoryManager, self).__init__(shell=shell, config=config,
529 super(HistoryManager, self).__init__(shell=shell, config=config,
527 **traits)
530 **traits)
528 self.save_flag = threading.Event()
531 self.save_flag = threading.Event()
529 self.db_input_cache_lock = threading.Lock()
532 self.db_input_cache_lock = threading.Lock()
530 self.db_output_cache_lock = threading.Lock()
533 self.db_output_cache_lock = threading.Lock()
531
534
532 try:
535 try:
533 self.new_session()
536 self.new_session()
534 except sqlite3.OperationalError:
537 except sqlite3.OperationalError:
535 self.log.error("Failed to create history session in %s. History will not be saved.",
538 self.log.error("Failed to create history session in %s. History will not be saved.",
536 self.hist_file, exc_info=True)
539 self.hist_file, exc_info=True)
537 self.hist_file = ':memory:'
540 self.hist_file = ':memory:'
538
541
539 if self.enabled and self.hist_file != ':memory:':
542 if self.enabled and self.hist_file != ':memory:':
540 self.save_thread = HistorySavingThread(self)
543 self.save_thread = HistorySavingThread(self)
541 self.save_thread.start()
544 self.save_thread.start()
542
545
543 def _get_hist_file_name(self, profile=None):
546 def _get_hist_file_name(self, profile=None):
544 """Get default history file name based on the Shell's profile.
547 """Get default history file name based on the Shell's profile.
545
548
546 The profile parameter is ignored, but must exist for compatibility with
549 The profile parameter is ignored, but must exist for compatibility with
547 the parent class."""
550 the parent class."""
548 profile_dir = self.shell.profile_dir.location
551 profile_dir = self.shell.profile_dir.location
549 return Path(profile_dir) / "history.sqlite"
552 return Path(profile_dir) / "history.sqlite"
550
553
551 @only_when_enabled
554 @only_when_enabled
552 def new_session(self, conn=None):
555 def new_session(self, conn=None):
553 """Get a new session number."""
556 """Get a new session number."""
554 if conn is None:
557 if conn is None:
555 conn = self.db
558 conn = self.db
556
559
557 with conn:
560 with conn:
558 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
561 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
559 NULL, "") """, (datetime.datetime.now(),))
562 NULL, "") """, (datetime.datetime.now(),))
560 self.session_number = cur.lastrowid
563 self.session_number = cur.lastrowid
561
564
562 def end_session(self):
565 def end_session(self):
563 """Close the database session, filling in the end time and line count."""
566 """Close the database session, filling in the end time and line count."""
564 self.writeout_cache()
567 self.writeout_cache()
565 with self.db:
568 with self.db:
566 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
569 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
567 session==?""", (datetime.datetime.now(),
570 session==?""", (datetime.datetime.now(),
568 len(self.input_hist_parsed)-1, self.session_number))
571 len(self.input_hist_parsed)-1, self.session_number))
569 self.session_number = 0
572 self.session_number = 0
570
573
571 def name_session(self, name):
574 def name_session(self, name):
572 """Give the current session a name in the history database."""
575 """Give the current session a name in the history database."""
573 with self.db:
576 with self.db:
574 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
577 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
575 (name, self.session_number))
578 (name, self.session_number))
576
579
577 def reset(self, new_session=True):
580 def reset(self, new_session=True):
578 """Clear the session history, releasing all object references, and
581 """Clear the session history, releasing all object references, and
579 optionally open a new session."""
582 optionally open a new session."""
580 self.output_hist.clear()
583 self.output_hist.clear()
581 # The directory history can't be completely empty
584 # The directory history can't be completely empty
582 self.dir_hist[:] = [Path.cwd()]
585 self.dir_hist[:] = [Path.cwd()]
583
586
584 if new_session:
587 if new_session:
585 if self.session_number:
588 if self.session_number:
586 self.end_session()
589 self.end_session()
587 self.input_hist_parsed[:] = [""]
590 self.input_hist_parsed[:] = [""]
588 self.input_hist_raw[:] = [""]
591 self.input_hist_raw[:] = [""]
589 self.new_session()
592 self.new_session()
590
593
591 # ------------------------------
594 # ------------------------------
592 # Methods for retrieving history
595 # Methods for retrieving history
593 # ------------------------------
596 # ------------------------------
594 def get_session_info(self, session=0):
597 def get_session_info(self, session=0):
595 """Get info about a session.
598 """Get info about a session.
596
599
597 Parameters
600 Parameters
598 ----------
601 ----------
599
602
600 session : int
603 session : int
601 Session number to retrieve. The current session is 0, and negative
604 Session number to retrieve. The current session is 0, and negative
602 numbers count back from current session, so -1 is the previous session.
605 numbers count back from current session, so -1 is the previous session.
603
606
604 Returns
607 Returns
605 -------
608 -------
606
609
607 session_id : int
610 session_id : int
608 Session ID number
611 Session ID number
609 start : datetime
612 start : datetime
610 Timestamp for the start of the session.
613 Timestamp for the start of the session.
611 end : datetime
614 end : datetime
612 Timestamp for the end of the session, or None if IPython crashed.
615 Timestamp for the end of the session, or None if IPython crashed.
613 num_cmds : int
616 num_cmds : int
614 Number of commands run, or None if IPython crashed.
617 Number of commands run, or None if IPython crashed.
615 remark : unicode
618 remark : unicode
616 A manually set description.
619 A manually set description.
617 """
620 """
618 if session <= 0:
621 if session <= 0:
619 session += self.session_number
622 session += self.session_number
620
623
621 return super(HistoryManager, self).get_session_info(session=session)
624 return super(HistoryManager, self).get_session_info(session=session)
622
625
623 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
626 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
624 """Get input and output history from the current session. Called by
627 """Get input and output history from the current session. Called by
625 get_range, and takes similar parameters."""
628 get_range, and takes similar parameters."""
626 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
629 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
627
630
628 n = len(input_hist)
631 n = len(input_hist)
629 if start < 0:
632 if start < 0:
630 start += n
633 start += n
631 if not stop or (stop > n):
634 if not stop or (stop > n):
632 stop = n
635 stop = n
633 elif stop < 0:
636 elif stop < 0:
634 stop += n
637 stop += n
635
638
636 for i in range(start, stop):
639 for i in range(start, stop):
637 if output:
640 if output:
638 line = (input_hist[i], self.output_hist_reprs.get(i))
641 line = (input_hist[i], self.output_hist_reprs.get(i))
639 else:
642 else:
640 line = input_hist[i]
643 line = input_hist[i]
641 yield (0, i, line)
644 yield (0, i, line)
642
645
643 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
646 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
644 """Retrieve input by session.
647 """Retrieve input by session.
645
648
646 Parameters
649 Parameters
647 ----------
650 ----------
648 session : int
651 session : int
649 Session number to retrieve. The current session is 0, and negative
652 Session number to retrieve. The current session is 0, and negative
650 numbers count back from current session, so -1 is previous session.
653 numbers count back from current session, so -1 is previous session.
651 start : int
654 start : int
652 First line to retrieve.
655 First line to retrieve.
653 stop : int
656 stop : int
654 End of line range (excluded from output itself). If None, retrieve
657 End of line range (excluded from output itself). If None, retrieve
655 to the end of the session.
658 to the end of the session.
656 raw : bool
659 raw : bool
657 If True, return untranslated input
660 If True, return untranslated input
658 output : bool
661 output : bool
659 If True, attempt to include output. This will be 'real' Python
662 If True, attempt to include output. This will be 'real' Python
660 objects for the current session, or text reprs from previous
663 objects for the current session, or text reprs from previous
661 sessions if db_log_output was enabled at the time. Where no output
664 sessions if db_log_output was enabled at the time. Where no output
662 is found, None is used.
665 is found, None is used.
663
666
664 Returns
667 Returns
665 -------
668 -------
666 entries
669 entries
667 An iterator over the desired lines. Each line is a 3-tuple, either
670 An iterator over the desired lines. Each line is a 3-tuple, either
668 (session, line, input) if output is False, or
671 (session, line, input) if output is False, or
669 (session, line, (input, output)) if output is True.
672 (session, line, (input, output)) if output is True.
670 """
673 """
671 if session <= 0:
674 if session <= 0:
672 session += self.session_number
675 session += self.session_number
673 if session==self.session_number: # Current session
676 if session==self.session_number: # Current session
674 return self._get_range_session(start, stop, raw, output)
677 return self._get_range_session(start, stop, raw, output)
675 return super(HistoryManager, self).get_range(session, start, stop, raw,
678 return super(HistoryManager, self).get_range(session, start, stop, raw,
676 output)
679 output)
677
680
678 ## ----------------------------
681 ## ----------------------------
679 ## Methods for storing history:
682 ## Methods for storing history:
680 ## ----------------------------
683 ## ----------------------------
681 def store_inputs(self, line_num, source, source_raw=None):
684 def store_inputs(self, line_num, source, source_raw=None):
682 """Store source and raw input in history and create input cache
685 """Store source and raw input in history and create input cache
683 variables ``_i*``.
686 variables ``_i*``.
684
687
685 Parameters
688 Parameters
686 ----------
689 ----------
687 line_num : int
690 line_num : int
688 The prompt number of this input.
691 The prompt number of this input.
689
692
690 source : str
693 source : str
691 Python input.
694 Python input.
692
695
693 source_raw : str, optional
696 source_raw : str, optional
694 If given, this is the raw input without any IPython transformations
697 If given, this is the raw input without any IPython transformations
695 applied to it. If not given, ``source`` is used.
698 applied to it. If not given, ``source`` is used.
696 """
699 """
697 if source_raw is None:
700 if source_raw is None:
698 source_raw = source
701 source_raw = source
699 source = source.rstrip('\n')
702 source = source.rstrip('\n')
700 source_raw = source_raw.rstrip('\n')
703 source_raw = source_raw.rstrip('\n')
701
704
702 # do not store exit/quit commands
705 # do not store exit/quit commands
703 if self._exit_re.match(source_raw.strip()):
706 if self._exit_re.match(source_raw.strip()):
704 return
707 return
705
708
706 self.input_hist_parsed.append(source)
709 self.input_hist_parsed.append(source)
707 self.input_hist_raw.append(source_raw)
710 self.input_hist_raw.append(source_raw)
708
711
709 with self.db_input_cache_lock:
712 with self.db_input_cache_lock:
710 self.db_input_cache.append((line_num, source, source_raw))
713 self.db_input_cache.append((line_num, source, source_raw))
711 # Trigger to flush cache and write to DB.
714 # Trigger to flush cache and write to DB.
712 if len(self.db_input_cache) >= self.db_cache_size:
715 if len(self.db_input_cache) >= self.db_cache_size:
713 self.save_flag.set()
716 self.save_flag.set()
714
717
715 # update the auto _i variables
718 # update the auto _i variables
716 self._iii = self._ii
719 self._iii = self._ii
717 self._ii = self._i
720 self._ii = self._i
718 self._i = self._i00
721 self._i = self._i00
719 self._i00 = source_raw
722 self._i00 = source_raw
720
723
721 # hackish access to user namespace to create _i1,_i2... dynamically
724 # hackish access to user namespace to create _i1,_i2... dynamically
722 new_i = '_i%s' % line_num
725 new_i = '_i%s' % line_num
723 to_main = {'_i': self._i,
726 to_main = {'_i': self._i,
724 '_ii': self._ii,
727 '_ii': self._ii,
725 '_iii': self._iii,
728 '_iii': self._iii,
726 new_i : self._i00 }
729 new_i : self._i00 }
727
730
728 if self.shell is not None:
731 if self.shell is not None:
729 self.shell.push(to_main, interactive=False)
732 self.shell.push(to_main, interactive=False)
730
733
731 def store_output(self, line_num):
734 def store_output(self, line_num):
732 """If database output logging is enabled, this saves all the
735 """If database output logging is enabled, this saves all the
733 outputs from the indicated prompt number to the database. It's
736 outputs from the indicated prompt number to the database. It's
734 called by run_cell after code has been executed.
737 called by run_cell after code has been executed.
735
738
736 Parameters
739 Parameters
737 ----------
740 ----------
738 line_num : int
741 line_num : int
739 The line number from which to save outputs
742 The line number from which to save outputs
740 """
743 """
741 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
744 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
742 return
745 return
743 output = self.output_hist_reprs[line_num]
746 output = self.output_hist_reprs[line_num]
744
747
745 with self.db_output_cache_lock:
748 with self.db_output_cache_lock:
746 self.db_output_cache.append((line_num, output))
749 self.db_output_cache.append((line_num, output))
747 if self.db_cache_size <= 1:
750 if self.db_cache_size <= 1:
748 self.save_flag.set()
751 self.save_flag.set()
749
752
750 def _writeout_input_cache(self, conn):
753 def _writeout_input_cache(self, conn):
751 with conn:
754 with conn:
752 for line in self.db_input_cache:
755 for line in self.db_input_cache:
753 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
756 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
754 (self.session_number,)+line)
757 (self.session_number,)+line)
755
758
756 def _writeout_output_cache(self, conn):
759 def _writeout_output_cache(self, conn):
757 with conn:
760 with conn:
758 for line in self.db_output_cache:
761 for line in self.db_output_cache:
759 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
762 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
760 (self.session_number,)+line)
763 (self.session_number,)+line)
761
764
762 @only_when_enabled
765 @only_when_enabled
763 def writeout_cache(self, conn=None):
766 def writeout_cache(self, conn=None):
764 """Write any entries in the cache to the database."""
767 """Write any entries in the cache to the database."""
765 if conn is None:
768 if conn is None:
766 conn = self.db
769 conn = self.db
767
770
768 with self.db_input_cache_lock:
771 with self.db_input_cache_lock:
769 try:
772 try:
770 self._writeout_input_cache(conn)
773 self._writeout_input_cache(conn)
771 except sqlite3.IntegrityError:
774 except sqlite3.IntegrityError:
772 self.new_session(conn)
775 self.new_session(conn)
773 print("ERROR! Session/line number was not unique in",
776 print("ERROR! Session/line number was not unique in",
774 "database. History logging moved to new session",
777 "database. History logging moved to new session",
775 self.session_number)
778 self.session_number)
776 try:
779 try:
777 # Try writing to the new session. If this fails, don't
780 # Try writing to the new session. If this fails, don't
778 # recurse
781 # recurse
779 self._writeout_input_cache(conn)
782 self._writeout_input_cache(conn)
780 except sqlite3.IntegrityError:
783 except sqlite3.IntegrityError:
781 pass
784 pass
782 finally:
785 finally:
783 self.db_input_cache = []
786 self.db_input_cache = []
784
787
785 with self.db_output_cache_lock:
788 with self.db_output_cache_lock:
786 try:
789 try:
787 self._writeout_output_cache(conn)
790 self._writeout_output_cache(conn)
788 except sqlite3.IntegrityError:
791 except sqlite3.IntegrityError:
789 print("!! Session/line number for output was not unique",
792 print("!! Session/line number for output was not unique",
790 "in database. Output will not be stored.")
793 "in database. Output will not be stored.")
791 finally:
794 finally:
792 self.db_output_cache = []
795 self.db_output_cache = []
793
796
794
797
795 class HistorySavingThread(threading.Thread):
798 class HistorySavingThread(threading.Thread):
796 """This thread takes care of writing history to the database, so that
799 """This thread takes care of writing history to the database, so that
797 the UI isn't held up while that happens.
800 the UI isn't held up while that happens.
798
801
799 It waits for the HistoryManager's save_flag to be set, then writes out
802 It waits for the HistoryManager's save_flag to be set, then writes out
800 the history cache. The main thread is responsible for setting the flag when
803 the history cache. The main thread is responsible for setting the flag when
801 the cache size reaches a defined threshold."""
804 the cache size reaches a defined threshold."""
802 daemon = True
805 daemon = True
803 stop_now = False
806 stop_now = False
804 enabled = True
807 enabled = True
805 def __init__(self, history_manager):
808 def __init__(self, history_manager):
806 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
809 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
807 self.history_manager = history_manager
810 self.history_manager = history_manager
808 self.enabled = history_manager.enabled
811 self.enabled = history_manager.enabled
809 atexit.register(self.stop)
812 atexit.register(self.stop)
810
813
811 @only_when_enabled
814 @only_when_enabled
812 def run(self):
815 def run(self):
813 # We need a separate db connection per thread:
816 # We need a separate db connection per thread:
814 try:
817 try:
815 self.db = sqlite3.connect(
818 self.db = sqlite3.connect(
816 str(self.history_manager.hist_file),
819 str(self.history_manager.hist_file),
817 **self.history_manager.connection_options
820 **self.history_manager.connection_options
818 )
821 )
819 while True:
822 while True:
820 self.history_manager.save_flag.wait()
823 self.history_manager.save_flag.wait()
821 if self.stop_now:
824 if self.stop_now:
822 self.db.close()
825 self.db.close()
823 return
826 return
824 self.history_manager.save_flag.clear()
827 self.history_manager.save_flag.clear()
825 self.history_manager.writeout_cache(self.db)
828 self.history_manager.writeout_cache(self.db)
826 except Exception as e:
829 except Exception as e:
827 print(("The history saving thread hit an unexpected error (%s)."
830 print(("The history saving thread hit an unexpected error (%s)."
828 "History will not be written to the database.") % repr(e))
831 "History will not be written to the database.") % repr(e))
829
832
830 def stop(self):
833 def stop(self):
831 """This can be called from the main thread to safely stop this thread.
834 """This can be called from the main thread to safely stop this thread.
832
835
833 Note that it does not attempt to write out remaining history before
836 Note that it does not attempt to write out remaining history before
834 exiting. That should be done by calling the HistoryManager's
837 exiting. That should be done by calling the HistoryManager's
835 end_session method."""
838 end_session method."""
836 self.stop_now = True
839 self.stop_now = True
837 self.history_manager.save_flag.set()
840 self.history_manager.save_flag.set()
838 self.join()
841 self.join()
839
842
840
843
841 # To match, e.g. ~5/8-~2/3
844 # To match, e.g. ~5/8-~2/3
842 range_re = re.compile(r"""
845 range_re = re.compile(r"""
843 ((?P<startsess>~?\d+)/)?
846 ((?P<startsess>~?\d+)/)?
844 (?P<start>\d+)?
847 (?P<start>\d+)?
845 ((?P<sep>[\-:])
848 ((?P<sep>[\-:])
846 ((?P<endsess>~?\d+)/)?
849 ((?P<endsess>~?\d+)/)?
847 (?P<end>\d+))?
850 (?P<end>\d+))?
848 $""", re.VERBOSE)
851 $""", re.VERBOSE)
849
852
850
853
851 def extract_hist_ranges(ranges_str):
854 def extract_hist_ranges(ranges_str):
852 """Turn a string of history ranges into 3-tuples of (session, start, stop).
855 """Turn a string of history ranges into 3-tuples of (session, start, stop).
853
856
857 Empty string results in a `[(0, 1, None)]`, i.e. "everything from current
858 session".
859
854 Examples
860 Examples
855 --------
861 --------
856 >>> list(extract_hist_ranges("~8/5-~7/4 2"))
862 >>> list(extract_hist_ranges("~8/5-~7/4 2"))
857 [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
863 [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
858 """
864 """
865 if ranges_str == '':
866 yield (0, 1, None) # Everything from current session
867 return
868
859 for range_str in ranges_str.split():
869 for range_str in ranges_str.split():
860 rmatch = range_re.match(range_str)
870 rmatch = range_re.match(range_str)
861 if not rmatch:
871 if not rmatch:
862 continue
872 continue
863 start = rmatch.group("start")
873 start = rmatch.group("start")
864 if start:
874 if start:
865 start = int(start)
875 start = int(start)
866 end = rmatch.group("end")
876 end = rmatch.group("end")
867 # If no end specified, get (a, a + 1)
877 # If no end specified, get (a, a + 1)
868 end = int(end) if end else start + 1
878 end = int(end) if end else start + 1
869 else: # start not specified
879 else: # start not specified
870 if not rmatch.group('startsess'): # no startsess
880 if not rmatch.group('startsess'): # no startsess
871 continue
881 continue
872 start = 1
882 start = 1
873 end = None # provide the entire session hist
883 end = None # provide the entire session hist
874
884
875 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
885 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
876 end += 1
886 end += 1
877 startsess = rmatch.group("startsess") or "0"
887 startsess = rmatch.group("startsess") or "0"
878 endsess = rmatch.group("endsess") or startsess
888 endsess = rmatch.group("endsess") or startsess
879 startsess = int(startsess.replace("~","-"))
889 startsess = int(startsess.replace("~","-"))
880 endsess = int(endsess.replace("~","-"))
890 endsess = int(endsess.replace("~","-"))
881 assert endsess >= startsess, "start session must be earlier than end session"
891 assert endsess >= startsess, "start session must be earlier than end session"
882
892
883 if endsess == startsess:
893 if endsess == startsess:
884 yield (startsess, start, end)
894 yield (startsess, start, end)
885 continue
895 continue
886 # Multiple sessions in one range:
896 # Multiple sessions in one range:
887 yield (startsess, start, None)
897 yield (startsess, start, None)
888 for sess in range(startsess+1, endsess):
898 for sess in range(startsess+1, endsess):
889 yield (sess, 1, None)
899 yield (sess, 1, None)
890 yield (endsess, 1, end)
900 yield (endsess, 1, end)
891
901
892
902
893 def _format_lineno(session, line):
903 def _format_lineno(session, line):
894 """Helper function to format line numbers properly."""
904 """Helper function to format line numbers properly."""
895 if session == 0:
905 if session == 0:
896 return str(line)
906 return str(line)
897 return "%s#%s" % (session, line)
907 return "%s#%s" % (session, line)
@@ -1,215 +1,223 b''
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for the IPython tab-completion machinery.
2 """Tests for the IPython tab-completion machinery.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Module imports
5 # Module imports
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7
7
8 # stdlib
8 # stdlib
9 import io
9 import io
10 from pathlib import Path
10 from pathlib import Path
11 import sys
11 import sys
12 import tempfile
12 import tempfile
13 from datetime import datetime
13 from datetime import datetime
14 import sqlite3
14 import sqlite3
15
15
16 # third party
16 # third party
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 # our own packages
19 # our own packages
20 from traitlets.config.loader import Config
20 from traitlets.config.loader import Config
21 from IPython.utils.tempdir import TemporaryDirectory
21 from IPython.utils.tempdir import TemporaryDirectory
22 from IPython.core.history import HistoryManager, extract_hist_ranges
22 from IPython.core.history import HistoryManager, extract_hist_ranges
23 from IPython.testing.decorators import skipif
23 from IPython.testing.decorators import skipif
24
24
25 def test_proper_default_encoding():
25 def test_proper_default_encoding():
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
26 nt.assert_equal(sys.getdefaultencoding(), "utf-8")
27
27
28 @skipif(sqlite3.sqlite_version_info > (3,24,0))
28 @skipif(sqlite3.sqlite_version_info > (3,24,0))
29 def test_history():
29 def test_history():
30 ip = get_ipython()
30 ip = get_ipython()
31 with TemporaryDirectory() as tmpdir:
31 with TemporaryDirectory() as tmpdir:
32 tmp_path = Path(tmpdir)
32 tmp_path = Path(tmpdir)
33 hist_manager_ori = ip.history_manager
33 hist_manager_ori = ip.history_manager
34 hist_file = tmp_path / "history.sqlite"
34 hist_file = tmp_path / "history.sqlite"
35 try:
35 try:
36 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
36 ip.history_manager = HistoryManager(shell=ip, hist_file=hist_file)
37 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
37 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
38 for i, h in enumerate(hist, start=1):
38 for i, h in enumerate(hist, start=1):
39 ip.history_manager.store_inputs(i, h)
39 ip.history_manager.store_inputs(i, h)
40
40
41 ip.history_manager.db_log_output = True
41 ip.history_manager.db_log_output = True
42 # Doesn't match the input, but we'll just check it's stored.
42 # Doesn't match the input, but we'll just check it's stored.
43 ip.history_manager.output_hist_reprs[3] = "spam"
43 ip.history_manager.output_hist_reprs[3] = "spam"
44 ip.history_manager.store_output(3)
44 ip.history_manager.store_output(3)
45
45
46 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
46 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
47
47
48 # Detailed tests for _get_range_session
48 # Detailed tests for _get_range_session
49 grs = ip.history_manager._get_range_session
49 grs = ip.history_manager._get_range_session
50 nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1])))
50 nt.assert_equal(list(grs(start=2,stop=-1)), list(zip([0], [2], hist[1:-1])))
51 nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:])))
51 nt.assert_equal(list(grs(start=-2)), list(zip([0,0], [2,3], hist[-2:])))
52 nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))))
52 nt.assert_equal(list(grs(output=True)), list(zip([0,0,0], [1,2,3], zip(hist, [None,None,'spam']))))
53
53
54 # Check whether specifying a range beyond the end of the current
54 # Check whether specifying a range beyond the end of the current
55 # session results in an error (gh-804)
55 # session results in an error (gh-804)
56 ip.magic('%hist 2-500')
56 ip.magic('%hist 2-500')
57
57
58 # Check that we can write non-ascii characters to a file
58 # Check that we can write non-ascii characters to a file
59 ip.magic("%%hist -f %s" % (tmp_path / "test1"))
59 ip.magic("%%hist -f %s" % (tmp_path / "test1"))
60 ip.magic("%%hist -pf %s" % (tmp_path / "test2"))
60 ip.magic("%%hist -pf %s" % (tmp_path / "test2"))
61 ip.magic("%%hist -nf %s" % (tmp_path / "test3"))
61 ip.magic("%%hist -nf %s" % (tmp_path / "test3"))
62 ip.magic("%%save %s 1-10" % (tmp_path / "test4"))
62 ip.magic("%%save %s 1-10" % (tmp_path / "test4"))
63
63
64 # New session
64 # New session
65 ip.history_manager.reset()
65 ip.history_manager.reset()
66 newcmds = [u"z=5",
66 newcmds = [u"z=5",
67 u"class X(object):\n pass",
67 u"class X(object):\n pass",
68 u"k='p'",
68 u"k='p'",
69 u"z=5"]
69 u"z=5"]
70 for i, cmd in enumerate(newcmds, start=1):
70 for i, cmd in enumerate(newcmds, start=1):
71 ip.history_manager.store_inputs(i, cmd)
71 ip.history_manager.store_inputs(i, cmd)
72 gothist = ip.history_manager.get_range(start=1, stop=4)
72 gothist = ip.history_manager.get_range(start=1, stop=4)
73 nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds)))
73 nt.assert_equal(list(gothist), list(zip([0,0,0],[1,2,3], newcmds)))
74 # Previous session:
74 # Previous session:
75 gothist = ip.history_manager.get_range(-1, 1, 4)
75 gothist = ip.history_manager.get_range(-1, 1, 4)
76 nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist)))
76 nt.assert_equal(list(gothist), list(zip([1,1,1],[1,2,3], hist)))
77
77
78 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
78 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
79
79
80 # Check get_hist_tail
80 # Check get_hist_tail
81 gothist = ip.history_manager.get_tail(5, output=True,
81 gothist = ip.history_manager.get_tail(5, output=True,
82 include_latest=True)
82 include_latest=True)
83 expected = [(1, 3, (hist[-1], "spam"))] \
83 expected = [(1, 3, (hist[-1], "spam"))] \
84 + [(s, n, (c, None)) for (s, n, c) in newhist]
84 + [(s, n, (c, None)) for (s, n, c) in newhist]
85 nt.assert_equal(list(gothist), expected)
85 nt.assert_equal(list(gothist), expected)
86
86
87 gothist = ip.history_manager.get_tail(2)
87 gothist = ip.history_manager.get_tail(2)
88 expected = newhist[-3:-1]
88 expected = newhist[-3:-1]
89 nt.assert_equal(list(gothist), expected)
89 nt.assert_equal(list(gothist), expected)
90
90
91 # Check get_hist_search
91 # Check get_hist_search
92
92
93 gothist = ip.history_manager.search("*test*")
93 gothist = ip.history_manager.search("*test*")
94 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
94 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
95
95
96 gothist = ip.history_manager.search("*=*")
96 gothist = ip.history_manager.search("*=*")
97 nt.assert_equal(list(gothist),
97 nt.assert_equal(list(gothist),
98 [(1, 1, hist[0]),
98 [(1, 1, hist[0]),
99 (1, 2, hist[1]),
99 (1, 2, hist[1]),
100 (1, 3, hist[2]),
100 (1, 3, hist[2]),
101 newhist[0],
101 newhist[0],
102 newhist[2],
102 newhist[2],
103 newhist[3]])
103 newhist[3]])
104
104
105 gothist = ip.history_manager.search("*=*", n=4)
105 gothist = ip.history_manager.search("*=*", n=4)
106 nt.assert_equal(list(gothist),
106 nt.assert_equal(list(gothist),
107 [(1, 3, hist[2]),
107 [(1, 3, hist[2]),
108 newhist[0],
108 newhist[0],
109 newhist[2],
109 newhist[2],
110 newhist[3]])
110 newhist[3]])
111
111
112 gothist = ip.history_manager.search("*=*", unique=True)
112 gothist = ip.history_manager.search("*=*", unique=True)
113 nt.assert_equal(list(gothist),
113 nt.assert_equal(list(gothist),
114 [(1, 1, hist[0]),
114 [(1, 1, hist[0]),
115 (1, 2, hist[1]),
115 (1, 2, hist[1]),
116 (1, 3, hist[2]),
116 (1, 3, hist[2]),
117 newhist[2],
117 newhist[2],
118 newhist[3]])
118 newhist[3]])
119
119
120 gothist = ip.history_manager.search("*=*", unique=True, n=3)
120 gothist = ip.history_manager.search("*=*", unique=True, n=3)
121 nt.assert_equal(list(gothist),
121 nt.assert_equal(list(gothist),
122 [(1, 3, hist[2]),
122 [(1, 3, hist[2]),
123 newhist[2],
123 newhist[2],
124 newhist[3]])
124 newhist[3]])
125
125
126 gothist = ip.history_manager.search("b*", output=True)
126 gothist = ip.history_manager.search("b*", output=True)
127 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
127 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
128
128
129 # Cross testing: check that magic %save can get previous session.
129 # Cross testing: check that magic %save can get previous session.
130 testfilename = (tmp_path / "test.py").resolve()
130 testfilename = (tmp_path / "test.py").resolve()
131 ip.magic("save " + str(testfilename) + " ~1/1-3")
131 ip.magic("save " + str(testfilename) + " ~1/1-3")
132 with io.open(testfilename, encoding='utf-8') as testfile:
132 with io.open(testfilename, encoding='utf-8') as testfile:
133 nt.assert_equal(testfile.read(),
133 nt.assert_equal(testfile.read(),
134 u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")
134 u"# coding: utf-8\n" + u"\n".join(hist)+u"\n")
135
135
136 # Duplicate line numbers - check that it doesn't crash, and
136 # Duplicate line numbers - check that it doesn't crash, and
137 # gets a new session
137 # gets a new session
138 ip.history_manager.store_inputs(1, "rogue")
138 ip.history_manager.store_inputs(1, "rogue")
139 ip.history_manager.writeout_cache()
139 ip.history_manager.writeout_cache()
140 nt.assert_equal(ip.history_manager.session_number, 3)
140 nt.assert_equal(ip.history_manager.session_number, 3)
141 finally:
141 finally:
142 # Ensure saving thread is shut down before we try to clean up the files
142 # Ensure saving thread is shut down before we try to clean up the files
143 ip.history_manager.save_thread.stop()
143 ip.history_manager.save_thread.stop()
144 # Forcibly close database rather than relying on garbage collection
144 # Forcibly close database rather than relying on garbage collection
145 ip.history_manager.db.close()
145 ip.history_manager.db.close()
146 # Restore history manager
146 # Restore history manager
147 ip.history_manager = hist_manager_ori
147 ip.history_manager = hist_manager_ori
148
148
149
149
150 def test_extract_hist_ranges():
150 def test_extract_hist_ranges():
151 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
151 instr = "1 2/3 ~4/5-6 ~4/7-~4/9 ~9/2-~7/5 ~10/"
152 expected = [(0, 1, 2), # 0 == current session
152 expected = [(0, 1, 2), # 0 == current session
153 (2, 3, 4),
153 (2, 3, 4),
154 (-4, 5, 7),
154 (-4, 5, 7),
155 (-4, 7, 10),
155 (-4, 7, 10),
156 (-9, 2, None), # None == to end
156 (-9, 2, None), # None == to end
157 (-8, 1, None),
157 (-8, 1, None),
158 (-7, 1, 6),
158 (-7, 1, 6),
159 (-10, 1, None)]
159 (-10, 1, None)]
160 actual = list(extract_hist_ranges(instr))
160 actual = list(extract_hist_ranges(instr))
161 nt.assert_equal(actual, expected)
161 nt.assert_equal(actual, expected)
162
162
163
164 def test_extract_hist_ranges_empty_str():
165 instr = ''
166 expected = [(0, 1, None)] # 0 == current session, None == to end
167 actual = list(extract_hist_ranges(instr))
168 nt.assert_equal(actual, expected)
169
170
163 def test_magic_rerun():
171 def test_magic_rerun():
164 """Simple test for %rerun (no args -> rerun last line)"""
172 """Simple test for %rerun (no args -> rerun last line)"""
165 ip = get_ipython()
173 ip = get_ipython()
166 ip.run_cell("a = 10", store_history=True)
174 ip.run_cell("a = 10", store_history=True)
167 ip.run_cell("a += 1", store_history=True)
175 ip.run_cell("a += 1", store_history=True)
168 nt.assert_equal(ip.user_ns["a"], 11)
176 nt.assert_equal(ip.user_ns["a"], 11)
169 ip.run_cell("%rerun", store_history=True)
177 ip.run_cell("%rerun", store_history=True)
170 nt.assert_equal(ip.user_ns["a"], 12)
178 nt.assert_equal(ip.user_ns["a"], 12)
171
179
172 def test_timestamp_type():
180 def test_timestamp_type():
173 ip = get_ipython()
181 ip = get_ipython()
174 info = ip.history_manager.get_session_info()
182 info = ip.history_manager.get_session_info()
175 nt.assert_true(isinstance(info[1], datetime))
183 nt.assert_true(isinstance(info[1], datetime))
176
184
177 def test_hist_file_config():
185 def test_hist_file_config():
178 cfg = Config()
186 cfg = Config()
179 tfile = tempfile.NamedTemporaryFile(delete=False)
187 tfile = tempfile.NamedTemporaryFile(delete=False)
180 cfg.HistoryManager.hist_file = Path(tfile.name)
188 cfg.HistoryManager.hist_file = Path(tfile.name)
181 try:
189 try:
182 hm = HistoryManager(shell=get_ipython(), config=cfg)
190 hm = HistoryManager(shell=get_ipython(), config=cfg)
183 nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file)
191 nt.assert_equal(hm.hist_file, cfg.HistoryManager.hist_file)
184 finally:
192 finally:
185 try:
193 try:
186 Path(tfile.name).unlink()
194 Path(tfile.name).unlink()
187 except OSError:
195 except OSError:
188 # same catch as in testing.tools.TempFileMixin
196 # same catch as in testing.tools.TempFileMixin
189 # On Windows, even though we close the file, we still can't
197 # On Windows, even though we close the file, we still can't
190 # delete it. I have no clue why
198 # delete it. I have no clue why
191 pass
199 pass
192
200
193 def test_histmanager_disabled():
201 def test_histmanager_disabled():
194 """Ensure that disabling the history manager doesn't create a database."""
202 """Ensure that disabling the history manager doesn't create a database."""
195 cfg = Config()
203 cfg = Config()
196 cfg.HistoryAccessor.enabled = False
204 cfg.HistoryAccessor.enabled = False
197
205
198 ip = get_ipython()
206 ip = get_ipython()
199 with TemporaryDirectory() as tmpdir:
207 with TemporaryDirectory() as tmpdir:
200 hist_manager_ori = ip.history_manager
208 hist_manager_ori = ip.history_manager
201 hist_file = Path(tmpdir) / "history.sqlite"
209 hist_file = Path(tmpdir) / "history.sqlite"
202 cfg.HistoryManager.hist_file = hist_file
210 cfg.HistoryManager.hist_file = hist_file
203 try:
211 try:
204 ip.history_manager = HistoryManager(shell=ip, config=cfg)
212 ip.history_manager = HistoryManager(shell=ip, config=cfg)
205 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
213 hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
206 for i, h in enumerate(hist, start=1):
214 for i, h in enumerate(hist, start=1):
207 ip.history_manager.store_inputs(i, h)
215 ip.history_manager.store_inputs(i, h)
208 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
216 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
209 ip.history_manager.reset()
217 ip.history_manager.reset()
210 ip.history_manager.end_session()
218 ip.history_manager.end_session()
211 finally:
219 finally:
212 ip.history_manager = hist_manager_ori
220 ip.history_manager = hist_manager_ori
213
221
214 # hist_file should not be created
222 # hist_file should not be created
215 nt.assert_false(hist_file.exists())
223 nt.assert_false(hist_file.exists())
General Comments 0
You need to be logged in to leave comments. Login now