Move _import_history to history.py.

Also adjusts the history import test to operate at a higher level and
ensure the old text file is removed (or isn't, in the case of an error).
This commit is contained in:
Ryan Roden-Corrent 2017-06-07 08:10:44 -04:00
parent ea0b3eee05
commit 309b6ba32c
4 changed files with 42 additions and 31 deletions

View File

@ -161,7 +161,7 @@ def init(args, crash_handler):
QDesktopServices.setUrlHandler('https', open_desktopservices_url)
QDesktopServices.setUrlHandler('qute', open_desktopservices_url)
_import_history()
objreg.get('history').import_txt()
log.init.debug("Init done!")
crash_handler.raise_crashdlg()
@ -479,27 +479,6 @@ def _init_modules(args, crash_handler):
browsertab.init()
def _import_history():
"""Import a history text file into sqlite if it exists.
In older versions of qutebrowser, history was stored in a text format.
This converts that file into the new sqlite format and removes it.
"""
path = os.path.join(standarddir.data(), 'history')
if not os.path.isfile(path):
return
def action():
with debug.log_time(log.init, 'Converting old history file to sqlite'):
objreg.get('web-history').read(path)
message.info('History import complete. Removing {}'.format(path))
os.remove(path)
# delay to give message time to appear before locking down for import
message.info('Converting {} to sqlite...'.format(path))
QTimer.singleShot(100, action)
class Quitter:
"""Utility class to quit/restart the QApplication.

View File

@ -22,10 +22,11 @@
import os
import time
from PyQt5.QtCore import pyqtSlot, QUrl
from PyQt5.QtCore import pyqtSlot, QUrl, QTimer
from qutebrowser.commands import cmdutils
from qutebrowser.utils import utils, objreg, log, qtutils, usertypes, message
from qutebrowser.utils import (utils, objreg, log, qtutils, usertypes, message,
debug, standarddir)
from qutebrowser.misc import objects, sql
@ -217,7 +218,28 @@ class WebHistory(sql.SqlTable):
return (url, title, float(atime), bool(redirect))
def read(self, path):
def import_txt(self):
"""Import a history text file into sqlite if it exists.
In older versions of qutebrowser, history was stored in a text format.
This converts that file into the new sqlite format and removes it.
"""
path = os.path.join(standarddir.data(), 'history')
if not os.path.isfile(path):
return
def action():
with debug.log_time(log.init, 'Import old history file to sqlite'):
self._read(path)
message.info('History import complete. Removing {}'
.format(path))
os.remove(path)
# delay to give message time to appear before locking down for import
message.info('Converting {} to sqlite...'.format(path))
QTimer.singleShot(100, action)
def _read(self, path):
"""Import a text file into the sql database."""
with open(path, 'r', encoding='utf-8') as f:
rows = []

View File

@ -385,6 +385,10 @@ class InstaTimer(QObject):
def setInterval(self, interval):
pass
@staticmethod
def singleShot(_interval, fun):
fun()
class FakeConfigType:

View File

@ -230,8 +230,9 @@ def test_init(backend, qapp, tmpdir, monkeypatch, cleanup_init):
assert default_interface is None
def test_read(hist, tmpdir):
histfile = tmpdir / 'history'
def test_read(hist, data_tmpdir, monkeypatch, stubs):
monkeypatch.setattr(history, 'QTimer', stubs.InstaTimer)
histfile = data_tmpdir / 'history'
# empty line is deliberate, to test skipping empty lines
histfile.write('''12345 http://example.com/ title
12346 http://qutebrowser.org/
@ -239,7 +240,7 @@ def test_read(hist, tmpdir):
68891-r http://example.com/path/other ''')
hist.read(str(histfile))
hist.import_txt()
assert list(hist) == [
('http://example.com/', 'title', 12345, False),
@ -248,6 +249,8 @@ def test_read(hist, tmpdir):
('http://example.com/path/other', '', 68891, True)
]
assert not histfile.exists()
@pytest.mark.parametrize('line', [
'xyz http://example.com/bad-timestamp',
@ -255,12 +258,15 @@ def test_read(hist, tmpdir):
'http://example.com/no-timestamp',
'68891-r-r http://example.com/double-flag',
])
def test_read_invalid(hist, tmpdir, line):
histfile = tmpdir / 'history'
def test_read_invalid(hist, data_tmpdir, line, monkeypatch, stubs):
monkeypatch.setattr(history, 'QTimer', stubs.InstaTimer)
histfile = data_tmpdir / 'history'
histfile.write(line)
with pytest.raises(Exception):
hist.read(str(histfile))
hist.import_txt()
assert histfile.exists()
def test_debug_dump_history(hist, tmpdir):