Show More
@@ -138,7 +138,7 b' class HistoryManager(Configurable):' | |||
|
138 | 138 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist) |
|
139 | 139 | return hist |
|
140 | 140 | |
|
141 | def get_hist_search(self, pattern="*", raw=True): | |
|
141 | def get_hist_search(self, pattern="*", raw=True, output=False): | |
|
142 | 142 | """Search the database using unix glob-style matching (wildcards * and |
|
143 | 143 | ?, escape using \). |
|
144 | 144 | |
@@ -147,8 +147,17 b' class HistoryManager(Configurable):' | |||
|
147 | 147 | An iterator over tuples: (session, line_number, command) |
|
148 | 148 | """ |
|
149 | 149 | toget = "source_raw" if raw else "source" |
|
150 | return self.db.execute("SELECT session, line, " +toget+ \ | |
|
151 | " FROM history WHERE " +toget+ " GLOB ?", (pattern,)) | |
|
150 | tosearch = toget | |
|
151 | sqlfrom = "history" | |
|
152 | if output: | |
|
153 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" | |
|
154 | toget = "history.%s, output_history.output" % toget | |
|
155 | tosearch = "history." + tosearch | |
|
156 | hist = self.db.execute("SELECT session, line, " +toget+ \ | |
|
157 | " FROM "+sqlfrom+" WHERE " +tosearch+ " GLOB ?", (pattern,)) | |
|
158 | if output: | |
|
159 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist) | |
|
160 | return hist | |
|
152 | 161 | |
|
153 | 162 | def _get_hist_session(self, start=1, stop=None, raw=True, output=False): |
|
154 | 163 | """Get input and output history from the current session. Called by |
@@ -456,24 +465,11 b" def magic_history(self, parameter_s = ''):" | |||
|
456 | 465 | default_length = 40 |
|
457 | 466 | pattern = None |
|
458 | 467 | |
|
459 | # Glob search: | |
|
460 | if 'g' in opts: | |
|
468 | if 'g' in opts: # Glob search | |
|
461 | 469 | pattern = "*" + args + "*" if args else "*" |
|
462 | ||
|
463 | # Display: | |
|
464 | matches_current_session = [] | |
|
465 | for session, line, s in history_manager.get_hist_search(pattern, raw): | |
|
466 | if session == history_manager.session_number: | |
|
467 | matches_current_session.append((line, s)) | |
|
468 | continue | |
|
469 | print("%d/%d: %s" %(session, line, s.expandtabs(4)), file=outfile) | |
|
470 | if matches_current_session: | |
|
471 | print("=== Current session: ===", file=outfile) | |
|
472 | for line, s in matches_current_session: | |
|
473 | print("%d: %s" %(line, s.expandtabs(4)), file=outfile) | |
|
474 | return | |
|
475 | ||
|
476 | if 'l' in opts: # Get 'tail' | |
|
470 | hist = history_manager.get_hist_search(pattern, raw=raw, | |
|
471 | output=get_output) | |
|
472 | elif 'l' in opts: # Get 'tail' | |
|
477 | 473 | try: |
|
478 | 474 | n = int(args) |
|
479 | 475 | except ValueError, IndexError: |
@@ -484,12 +480,10 b" def magic_history(self, parameter_s = ''):" | |||
|
484 | 480 | hist = history_manager.get_hist_from_rangestr(args, raw, get_output) |
|
485 | 481 | else: # Just get history for the current session |
|
486 | 482 | hist = history_manager.get_history(raw=raw, output=get_output) |
|
487 | # Pull hist into a list, so we can get the widest number in it. | |
|
488 | hist = list(hist) | |
|
489 | if not hist: | |
|
490 | return | |
|
491 | 483 | |
|
492 | width = max(len(_format_lineno(s, l)) for s, l, _ in hist) | |
|
484 | # We could be displaying the entire history, so let's not try to pull it | |
|
485 | # into a list in memory. Anything that needs more space will just misalign. | |
|
486 | width = 4 | |
|
493 | 487 | |
|
494 | 488 | for session, lineno, inline in hist: |
|
495 | 489 | # Print user history with tabs expanded to 4 spaces. The GUI clients |
@@ -499,9 +493,6 b" def magic_history(self, parameter_s = ''):" | |||
|
499 | 493 | inline, output = inline |
|
500 | 494 | inline = inline.expandtabs(4).rstrip() |
|
501 | 495 | |
|
502 | if pattern is not None and not fnmatch.fnmatch(inline, pattern): | |
|
503 | continue | |
|
504 | ||
|
505 | 496 | multiline = "\n" in inline |
|
506 | 497 | line_sep = '\n' if multiline else '' |
|
507 | 498 | if print_nums: |
@@ -35,6 +35,10 b' def test_history():' | |||
|
35 | 35 | for i, h in enumerate(hist, start=1): |
|
36 | 36 | ip.history_manager.store_inputs(i, h) |
|
37 | 37 | |
|
38 | ip.history_manager.db_log_output = True | |
|
39 | # Doesn't match the input, but we'll just check it's stored. | |
|
40 | ip.history_manager.store_output(3, "spam") | |
|
41 | ||
|
38 | 42 | nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist) |
|
39 | 43 | |
|
40 | 44 | # Check lines were written to DB |
@@ -44,7 +48,7 b' def test_history():' | |||
|
44 | 48 | # New session |
|
45 | 49 | ip.history_manager.reset() |
|
46 | 50 | newcmds = ["z=5","class X(object):\n pass", "k='p'"] |
|
47 | for i, cmd in enumerate(newcmds): | |
|
51 | for i, cmd in enumerate(newcmds, start=1): | |
|
48 | 52 | ip.history_manager.store_inputs(i, cmd) |
|
49 | 53 | gothist = ip.history_manager.get_history(start=1, stop=4) |
|
50 | 54 | nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds)) |
@@ -52,6 +56,20 b' def test_history():' | |||
|
52 | 56 | gothist = ip.history_manager.get_history(-1, 1, 4) |
|
53 | 57 | nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist)) |
|
54 | 58 | |
|
59 | # Check get_hist_tail | |
|
60 | gothist = ip.history_manager.get_hist_tail(4, output=True) | |
|
61 | expected = [(1, 3, (hist[-1], "spam")), | |
|
62 | (2, 1, (newcmds[0], None)), | |
|
63 | (2, 2, (newcmds[1], None)), | |
|
64 | (2, 3, (newcmds[2], None)),] | |
|
65 | nt.assert_equal(list(gothist), expected) | |
|
66 | ||
|
67 | # Check get_hist_search | |
|
68 | gothist = ip.history_manager.get_hist_search("*test*") | |
|
69 | nt.assert_equal(list(gothist), [(1,2,hist[1])] ) | |
|
70 | gothist = ip.history_manager.get_hist_search("b*", output=True) | |
|
71 | nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] ) | |
|
72 | ||
|
55 | 73 | # Cross testing: check that magic %save can get previous session. |
|
56 | 74 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) |
|
57 | 75 | ip.magic_save(testfilename + " ~1/1-3") |
General Comments 0
You need to be logged in to leave comments.
Login now