##// END OF EJS Templates
BUG: use a :memory: history DB for testing. Refactor the initialization of the HistoryManager to support this.
Robert Kern -
Show More
@@ -47,15 +47,22 b' class HistoryManager(Configurable):'
47 input_hist_raw = List([""])
47 input_hist_raw = List([""])
48 # A list of directories visited during session
48 # A list of directories visited during session
49 dir_hist = List()
49 dir_hist = List()
50 def _dir_hist_default(self):
51 try:
52 return [os.getcwd()]
53 except OSError:
54 return []
55
50 # A dict of output history, keyed with ints from the shell's
56 # A dict of output history, keyed with ints from the shell's
51 # execution count. If there are several outputs from one command,
57 # execution count. If there are several outputs from one command,
52 # only the last one is stored.
58 # only the last one is stored.
53 output_hist = Dict()
59 output_hist = Dict()
54 # Contains all outputs, in lists of reprs.
60 # Contains all outputs, in lists of reprs.
55 output_hist_reprs = Instance(defaultdict)
61 output_hist_reprs = Instance(defaultdict, args=(list,))
56
62
57 # String holding the path to the history file
63 # String holding the path to the history file
58 hist_file = Unicode()
64 hist_file = Unicode(config=True)
65
59 # The SQLite database
66 # The SQLite database
60 db = Instance(sqlite3.Connection)
67 db = Instance(sqlite3.Connection)
61 # The number of the current session in the history database
68 # The number of the current session in the history database
@@ -73,47 +80,48 b' class HistoryManager(Configurable):'
73 # Variables used to store the three last inputs from the user. On each new
80 # Variables used to store the three last inputs from the user. On each new
74 # history update, we populate the user's namespace with these, shifted as
81 # history update, we populate the user's namespace with these, shifted as
75 # necessary.
82 # necessary.
76 _i00, _i, _ii, _iii = '','','',''
83 _i00 = Unicode(u'')
84 _i = Unicode(u'')
85 _ii = Unicode(u'')
86 _iii = Unicode(u'')
77
87
78 # A set with all forms of the exit command, so that we don't store them in
88 # A set with all forms of the exit command, so that we don't store them in
79 # the history (it's annoying to rewind the first entry and land on an exit
89 # the history (it's annoying to rewind the first entry and land on an exit
80 # call).
90 # call).
81 _exit_commands = None
91 _exit_commands = Instance(set, args=(['Quit', 'quit', 'Exit', 'exit',
82
92 '%Quit', '%quit', '%Exit', '%exit'],))
83 def __init__(self, shell, config=None):
93
94 def __init__(self, shell, config=None, **traits):
84 """Create a new history manager associated with a shell instance.
95 """Create a new history manager associated with a shell instance.
85 """
96 """
86 # We need a pointer back to the shell for various tasks.
97 # We need a pointer back to the shell for various tasks.
87 super(HistoryManager, self).__init__(shell=shell, config=config)
98 super(HistoryManager, self).__init__(shell=shell, config=config,
99 **traits)
88
100
89 # list of visited directories
101 if self.hist_file == u'':
90 try:
102 # No one has set the hist_file, yet.
91 self.dir_hist = [os.getcwd()]
103 if shell.profile:
92 except OSError:
104 histfname = 'history-%s' % shell.profile
93 self.dir_hist = []
105 else:
106 histfname = 'history'
107 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
94
108
95 # Now the history file
96 if shell.profile:
97 histfname = 'history-%s' % shell.profile
98 else:
99 histfname = 'history'
100 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.sqlite')
101 try:
109 try:
102 self.init_db()
110 self.init_db()
103 except sqlite3.DatabaseError:
111 except sqlite3.DatabaseError:
104 newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
112 if os.path.isfile(self.hist_file):
105 os.rename(self.hist_file, newpath)
113 # Try to move the file out of the way.
106 print("ERROR! History file wasn't a valid SQLite database.",
114 newpath = os.path.join(self.shell.ipython_dir, "hist-corrupt.sqlite")
107 "It was moved to %s" % newpath, "and a new file created.")
115 os.rename(self.hist_file, newpath)
108 self.init_db()
116 print("ERROR! History file wasn't a valid SQLite database.",
109
117 "It was moved to %s" % newpath, "and a new file created.")
118 self.init_db()
119 else:
120 # The hist_file is probably :memory: or something else.
121 raise
122
110 self.new_session()
123 self.new_session()
111
112 self._i00, self._i, self._ii, self._iii = '','','',''
113 self.output_hist_reprs = defaultdict(list)
114
124
115 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
116 '%quit', '%Exit', '%exit'])
117
125
118 def init_db(self):
126 def init_db(self):
119 """Connect to the database, and create tables if necessary."""
127 """Connect to the database, and create tables if necessary."""
@@ -23,18 +23,10 b' def setUp():'
23 def test_history():
23 def test_history():
24 ip = get_ipython()
24 ip = get_ipython()
25 with TemporaryDirectory() as tmpdir:
25 with TemporaryDirectory() as tmpdir:
26 #tmpdir = '/software/temp'
26 # Make a new :memory: DB.
27 histfile = os.path.realpath(os.path.join(tmpdir, 'history.sqlite'))
28 # Ensure that we restore the history management that we mess with in
29 # this test doesn't affect the IPython instance used by the test suite
30 # beyond this test.
31 hist_manager_ori = ip.history_manager
27 hist_manager_ori = ip.history_manager
32 try:
28 try:
33 ip.history_manager = HistoryManager(shell=ip)
29 ip.history_manager = HistoryManager(shell=ip, hist_file=':memory:')
34 ip.history_manager.hist_file = histfile
35 ip.history_manager.init_db() # Has to be called after changing file
36 ip.history_manager.reset()
37 print 'test',histfile
38 hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
30 hist = ['a=1', 'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
39 for i, h in enumerate(hist, start=1):
31 for i, h in enumerate(hist, start=1):
40 ip.history_manager.store_inputs(i, h)
32 ip.history_manager.store_inputs(i, h)
@@ -167,6 +167,7 b' def default_config():'
167 config.TerminalInteractiveShell.colors = 'NoColor'
167 config.TerminalInteractiveShell.colors = 'NoColor'
168 config.TerminalTerminalInteractiveShell.term_title = False,
168 config.TerminalTerminalInteractiveShell.term_title = False,
169 config.TerminalInteractiveShell.autocall = 0
169 config.TerminalInteractiveShell.autocall = 0
170 config.HistoryManager.hist_file = u':memory:'
170 return config
171 return config
171
172
172
173
General Comments 0
You need to be logged in to leave comments. Login now