##// END OF EJS Templates
Simplify magic_history display code, allow get_hist_search to include output in return values, and add some unit tests.
Thomas Kluyver -
Show More
@@ -138,7 +138,7 b' class HistoryManager(Configurable):'
138 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
138 return ((ses, lin, (inp, out)) for ses, lin, inp, out in hist)
139 return hist
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 """Search the database using unix glob-style matching (wildcards * and
142 """Search the database using unix glob-style matching (wildcards * and
143 ?, escape using \).
143 ?, escape using \).
144
144
@@ -147,8 +147,17 b' class HistoryManager(Configurable):'
147 An iterator over tuples: (session, line_number, command)
147 An iterator over tuples: (session, line_number, command)
148 """
148 """
149 toget = "source_raw" if raw else "source"
149 toget = "source_raw" if raw else "source"
150 return self.db.execute("SELECT session, line, " +toget+ \
150 tosearch = toget
151 " FROM history WHERE " +toget+ " GLOB ?", (pattern,))
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 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
162 def _get_hist_session(self, start=1, stop=None, raw=True, output=False):
154 """Get input and output history from the current session. Called by
163 """Get input and output history from the current session. Called by
@@ -456,24 +465,11 b" def magic_history(self, parameter_s = ''):"
456 default_length = 40
465 default_length = 40
457 pattern = None
466 pattern = None
458
467
459 # Glob search:
468 if 'g' in opts: # Glob search
460 if 'g' in opts:
461 pattern = "*" + args + "*" if args else "*"
469 pattern = "*" + args + "*" if args else "*"
462
470 hist = history_manager.get_hist_search(pattern, raw=raw,
463 # Display:
471 output=get_output)
464 matches_current_session = []
472 elif 'l' in opts: # Get 'tail'
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'
477 try:
473 try:
478 n = int(args)
474 n = int(args)
479 except ValueError, IndexError:
475 except ValueError, IndexError:
@@ -484,12 +480,10 b" def magic_history(self, parameter_s = ''):"
484 hist = history_manager.get_hist_from_rangestr(args, raw, get_output)
480 hist = history_manager.get_hist_from_rangestr(args, raw, get_output)
485 else: # Just get history for the current session
481 else: # Just get history for the current session
486 hist = history_manager.get_history(raw=raw, output=get_output)
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 for session, lineno, inline in hist:
488 for session, lineno, inline in hist:
495 # Print user history with tabs expanded to 4 spaces. The GUI clients
489 # Print user history with tabs expanded to 4 spaces. The GUI clients
@@ -498,12 +492,9 b" def magic_history(self, parameter_s = ''):"
498 if get_output:
492 if get_output:
499 inline, output = inline
493 inline, output = inline
500 inline = inline.expandtabs(4).rstrip()
494 inline = inline.expandtabs(4).rstrip()
501
502 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
503 continue
504
495
505 multiline = "\n" in inline
496 multiline = "\n" in inline
506 line_sep = '\n' if multiline else ''
497 line_sep = '\n' if multiline else ' '
507 if print_nums:
498 if print_nums:
508 print('%s:%s' % (_format_lineno(session, lineno).rjust(width),
499 print('%s:%s' % (_format_lineno(session, lineno).rjust(width),
509 line_sep), file=outfile, end='')
500 line_sep), file=outfile, end='')
@@ -35,6 +35,10 b' def test_history():'
35 for i, h in enumerate(hist, start=1):
35 for i, h in enumerate(hist, start=1):
36 ip.history_manager.store_inputs(i, h)
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 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
42 nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
39
43
40 # Check lines were written to DB
44 # Check lines were written to DB
@@ -44,7 +48,7 b' def test_history():'
44 # New session
48 # New session
45 ip.history_manager.reset()
49 ip.history_manager.reset()
46 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
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 ip.history_manager.store_inputs(i, cmd)
52 ip.history_manager.store_inputs(i, cmd)
49 gothist = ip.history_manager.get_history(start=1, stop=4)
53 gothist = ip.history_manager.get_history(start=1, stop=4)
50 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
54 nt.assert_equal(list(gothist), zip([0,0,0],[1,2,3], newcmds))
@@ -52,6 +56,20 b' def test_history():'
52 gothist = ip.history_manager.get_history(-1, 1, 4)
56 gothist = ip.history_manager.get_history(-1, 1, 4)
53 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
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 # Cross testing: check that magic %save can get previous session.
73 # Cross testing: check that magic %save can get previous session.
56 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
74 testfilename = os.path.realpath(os.path.join(tmpdir, "test.py"))
57 ip.magic_save(testfilename + " ~1/1-3")
75 ip.magic_save(testfilename + " ~1/1-3")
General Comments 0
You need to be logged in to leave comments. Login now