Show More
@@ -16,8 +16,17 b' 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, Bool, Dict, Instance, Integer, List, Unicode, Union, TraitError, |
|
19 | Any, | |
20 | default, observe |
|
20 | Bool, | |
|
21 | Dict, | |||
|
22 | Instance, | |||
|
23 | Integer, | |||
|
24 | List, | |||
|
25 | Unicode, | |||
|
26 | Union, | |||
|
27 | TraitError, | |||
|
28 | default, | |||
|
29 | observe, | |||
21 | ) |
|
30 | ) | |
22 |
|
31 | |||
23 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
@@ -100,6 +109,7 b' def catch_corrupt_db(f, self, *a, **kw):' | |||||
100 | # Failed with :memory:, something serious is wrong |
|
109 | # Failed with :memory:, something serious is wrong | |
101 | raise |
|
110 | raise | |
102 |
|
111 | |||
|
112 | ||||
103 | class HistoryAccessorBase(LoggingConfigurable): |
|
113 | class HistoryAccessorBase(LoggingConfigurable): | |
104 | """An abstract class for History Accessors """ |
|
114 | """An abstract class for History Accessors """ | |
105 |
|
115 | |||
@@ -129,7 +139,8 b' class HistoryAccessor(HistoryAccessorBase):' | |||||
129 | _corrupt_db_limit = 2 |
|
139 | _corrupt_db_limit = 2 | |
130 |
|
140 | |||
131 | # String holding the path to the history file |
|
141 | # String holding the path to the history file | |
132 |
hist_file = Union( |
|
142 | hist_file = Union( | |
|
143 | [Instance(Path), Unicode()], | |||
133 | help="""Path to file to use for SQLite history database. |
|
144 | help="""Path to file to use for SQLite history database. | |
134 |
|
145 | |||
135 | By default, IPython will put the history database in the IPython |
|
146 | By default, IPython will put the history database in the IPython | |
@@ -145,7 +156,7 b' class HistoryAccessor(HistoryAccessorBase):' | |||||
145 | you can also use the specific value `:memory:` (including the colon |
|
156 | you can also use the specific value `:memory:` (including the colon | |
146 | 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. | |
147 |
|
158 | |||
148 | """ |
|
159 | """, | |
149 | ).tag(config=True) |
|
160 | ).tag(config=True) | |
150 |
|
161 | |||
151 | enabled = Bool(True, |
|
162 | enabled = Bool(True, | |
@@ -178,7 +189,7 b' class HistoryAccessor(HistoryAccessorBase):' | |||||
178 | (self.__class__.__name__, new) |
|
189 | (self.__class__.__name__, new) | |
179 | raise TraitError(msg) |
|
190 | raise TraitError(msg) | |
180 |
|
191 | |||
181 |
def __init__(self, profile= |
|
192 | def __init__(self, profile="default", hist_file="", **traits): | |
182 | """Create a new history accessor. |
|
193 | """Create a new history accessor. | |
183 |
|
194 | |||
184 | Parameters |
|
195 | Parameters | |
@@ -200,7 +211,7 b' class HistoryAccessor(HistoryAccessorBase):' | |||||
200 | self.hist_file = hist_file |
|
211 | self.hist_file = hist_file | |
201 |
|
212 | |||
202 | try: |
|
213 | try: | |
203 |
self.hist_file |
|
214 | self.hist_file | |
204 | except TraitError: |
|
215 | except TraitError: | |
205 | # No one has set the hist_file, yet. |
|
216 | # No one has set the hist_file, yet. | |
206 | self.hist_file = self._get_hist_file_name(profile) |
|
217 | self.hist_file = self._get_hist_file_name(profile) | |
@@ -218,7 +229,7 b' class HistoryAccessor(HistoryAccessorBase):' | |||||
218 | profile : str |
|
229 | profile : str | |
219 | The name of a profile which has a history file. |
|
230 | The name of a profile which has a history file. | |
220 | """ |
|
231 | """ | |
221 |
return Path(locate_profile(profile)) / |
|
232 | return Path(locate_profile(profile)) / "history.sqlite" | |
222 |
|
233 | |||
223 | @catch_corrupt_db |
|
234 | @catch_corrupt_db | |
224 | def init_db(self): |
|
235 | def init_db(self): | |
@@ -535,7 +546,7 b' class HistoryManager(HistoryAccessor):' | |||||
535 | The profile parameter is ignored, but must exist for compatibility with |
|
546 | The profile parameter is ignored, but must exist for compatibility with | |
536 | the parent class.""" |
|
547 | the parent class.""" | |
537 | profile_dir = self.shell.profile_dir.location |
|
548 | profile_dir = self.shell.profile_dir.location | |
538 |
return Path(profile_dir)/ |
|
549 | return Path(profile_dir) / "history.sqlite" | |
539 |
|
550 | |||
540 | @only_when_enabled |
|
551 | @only_when_enabled | |
541 | def new_session(self, conn=None): |
|
552 | def new_session(self, conn=None): | |
@@ -801,8 +812,9 b' class HistorySavingThread(threading.Thread):' | |||||
801 | def run(self): |
|
812 | def run(self): | |
802 | # We need a separate db connection per thread: |
|
813 | # We need a separate db connection per thread: | |
803 | try: |
|
814 | try: | |
804 |
self.db = sqlite3.connect( |
|
815 | self.db = sqlite3.connect( | |
805 |
|
|
816 | str(self.history_manager.hist_file), | |
|
817 | **self.history_manager.connection_options | |||
806 | ) |
|
818 | ) | |
807 | while True: |
|
819 | while True: | |
808 | self.history_manager.save_flag.wait() |
|
820 | self.history_manager.save_flag.wait() |
@@ -31,7 +31,7 b' def test_history():' | |||||
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 / |
|
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='€Æ¾÷ß'"] | |
@@ -127,7 +127,7 b' def test_history():' | |||||
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(), | |
@@ -198,7 +198,7 b' def test_histmanager_disabled():' | |||||
198 | ip = get_ipython() |
|
198 | ip = get_ipython() | |
199 | with TemporaryDirectory() as tmpdir: |
|
199 | with TemporaryDirectory() as tmpdir: | |
200 | hist_manager_ori = ip.history_manager |
|
200 | hist_manager_ori = ip.history_manager | |
201 |
hist_file = Path(tmpdir) / |
|
201 | hist_file = Path(tmpdir) / "history.sqlite" | |
202 | cfg.HistoryManager.hist_file = hist_file |
|
202 | cfg.HistoryManager.hist_file = hist_file | |
203 | try: |
|
203 | try: | |
204 | ip.history_manager = HistoryManager(shell=ip, config=cfg) |
|
204 | ip.history_manager = HistoryManager(shell=ip, config=cfg) |
General Comments 0
You need to be logged in to leave comments.
Login now