Remove code rendered dead by sql implementation.
Vulture exposed the following dead code: - AppendLineParse was only used for reading the history text file, which is now a sql database (and the import code for the old text file is simpler and does not need a complex line parser) - async_read_done is no longer used as importing the history text file is synchronous (and should only happen once) - config._init_key_config is unused as it was moved to keyconf.init
This commit is contained in:
parent
a8ed9f1c2f
commit
784d9bb043
@ -21,7 +21,7 @@
|
||||
|
||||
import time
|
||||
|
||||
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl
|
||||
from PyQt5.QtCore import pyqtSlot, QUrl
|
||||
|
||||
from qutebrowser.commands import cmdutils
|
||||
from qutebrowser.utils import utils, objreg, log, qtutils, usertypes, message
|
||||
@ -74,8 +74,6 @@ class WebHistory(sql.SqlTable):
|
||||
|
||||
"""The global history of visited pages."""
|
||||
|
||||
async_read_done = pyqtSignal()
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__("History", ['url', 'title', 'atime', 'redirect'],
|
||||
parent=parent)
|
||||
|
@ -174,38 +174,6 @@ def _init_main_config(parent=None):
|
||||
return
|
||||
|
||||
|
||||
def _init_key_config(parent):
|
||||
"""Initialize the key config.
|
||||
|
||||
Args:
|
||||
parent: The parent to use for the KeyConfigParser.
|
||||
"""
|
||||
from qutebrowser.config.parsers import keyconf
|
||||
args = objreg.get('args')
|
||||
try:
|
||||
key_config = keyconf.KeyConfigParser(standarddir.config(), 'keys.conf',
|
||||
args.relaxed_config,
|
||||
parent=parent)
|
||||
except (keyconf.KeyConfigError, cmdexc.CommandError,
|
||||
UnicodeDecodeError) as e:
|
||||
log.init.exception(e)
|
||||
errstr = "Error while reading key config:\n"
|
||||
if e.lineno is not None:
|
||||
errstr += "In line {}: ".format(e.lineno)
|
||||
error.handle_fatal_exc(e, args, "Error while reading key config!",
|
||||
pre_text=errstr)
|
||||
# We didn't really initialize much so far, so we just quit hard.
|
||||
sys.exit(usertypes.Exit.err_key_config)
|
||||
else:
|
||||
objreg.register('key-config', key_config)
|
||||
save_manager = objreg.get('save-manager')
|
||||
filename = os.path.join(standarddir.config(), 'keys.conf')
|
||||
save_manager.add_saveable(
|
||||
'key-config', key_config.save, key_config.config_dirty,
|
||||
config_opt=('general', 'auto-save-config'), filename=filename,
|
||||
dirty=key_config.is_dirty)
|
||||
|
||||
|
||||
def _init_misc():
|
||||
"""Initialize misc. config-related files."""
|
||||
save_manager = objreg.get('save-manager')
|
||||
|
@ -21,7 +21,6 @@
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import itertools
|
||||
import contextlib
|
||||
|
||||
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
|
||||
@ -96,7 +95,7 @@ class BaseLineParser(QObject):
|
||||
"""
|
||||
assert self._configfile is not None
|
||||
if self._opened:
|
||||
raise IOError("Refusing to double-open AppendLineParser.")
|
||||
raise IOError("Refusing to double-open LineParser.")
|
||||
self._opened = True
|
||||
try:
|
||||
if self._binary:
|
||||
@ -133,73 +132,6 @@ class BaseLineParser(QObject):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class AppendLineParser(BaseLineParser):
|
||||
|
||||
"""LineParser which reads lazily and appends data to existing one.
|
||||
|
||||
Attributes:
|
||||
_new_data: The data which was added in this session.
|
||||
"""
|
||||
|
||||
def __init__(self, configdir, fname, *, parent=None):
|
||||
super().__init__(configdir, fname, binary=False, parent=parent)
|
||||
self.new_data = []
|
||||
self._fileobj = None
|
||||
|
||||
def __iter__(self):
|
||||
if self._fileobj is None:
|
||||
raise ValueError("Iterating without open() being called!")
|
||||
file_iter = (line.rstrip('\n') for line in self._fileobj)
|
||||
return itertools.chain(file_iter, iter(self.new_data))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open(self):
|
||||
"""Open the on-disk history file. Needed for __iter__."""
|
||||
try:
|
||||
with self._open('r') as f:
|
||||
self._fileobj = f
|
||||
yield
|
||||
except FileNotFoundError:
|
||||
self._fileobj = []
|
||||
yield
|
||||
finally:
|
||||
self._fileobj = None
|
||||
|
||||
def get_recent(self, count=4096):
|
||||
"""Get the last count bytes from the underlying file."""
|
||||
with self._open('r') as f:
|
||||
f.seek(0, os.SEEK_END)
|
||||
size = f.tell()
|
||||
try:
|
||||
if size - count > 0:
|
||||
offset = size - count
|
||||
else:
|
||||
offset = 0
|
||||
f.seek(offset)
|
||||
data = f.readlines()
|
||||
finally:
|
||||
f.seek(0, os.SEEK_END)
|
||||
return data
|
||||
|
||||
def save(self):
|
||||
do_save = self._prepare_save()
|
||||
if not do_save:
|
||||
return
|
||||
with self._open('a') as f:
|
||||
self._write(f, self.new_data)
|
||||
self.new_data = []
|
||||
self._after_save()
|
||||
|
||||
def clear(self):
|
||||
do_save = self._prepare_save()
|
||||
if not do_save:
|
||||
return
|
||||
with self._open('w'):
|
||||
pass
|
||||
self.new_data = []
|
||||
self._after_save()
|
||||
|
||||
|
||||
class LineParser(BaseLineParser):
|
||||
|
||||
"""Parser for configuration files which are simply line-based.
|
||||
@ -240,7 +172,7 @@ class LineParser(BaseLineParser):
|
||||
def save(self):
|
||||
"""Save the config file."""
|
||||
if self._opened:
|
||||
raise IOError("Refusing to double-open AppendLineParser.")
|
||||
raise IOError("Refusing to double-open LineParser.")
|
||||
do_save = self._prepare_save()
|
||||
if not do_save:
|
||||
return
|
||||
|
@ -125,83 +125,3 @@ class TestLineParser:
|
||||
lineparser._prepare_save = lambda: False
|
||||
lineparser.save()
|
||||
assert (tmpdir / 'file').read() == 'pristine\n'
|
||||
|
||||
|
||||
class TestAppendLineParser:
|
||||
|
||||
BASE_DATA = ['old data 1', 'old data 2']
|
||||
|
||||
@pytest.fixture
|
||||
def lineparser(self, tmpdir):
|
||||
"""Fixture to get an AppendLineParser for tests."""
|
||||
lp = lineparsermod.AppendLineParser(str(tmpdir), 'file')
|
||||
lp.new_data = self.BASE_DATA
|
||||
lp.save()
|
||||
return lp
|
||||
|
||||
def _get_expected(self, new_data):
|
||||
"""Get the expected data with newlines."""
|
||||
return '\n'.join(self.BASE_DATA + new_data) + '\n'
|
||||
|
||||
def test_save(self, tmpdir, lineparser):
|
||||
"""Test save()."""
|
||||
new_data = ['new data 1', 'new data 2']
|
||||
lineparser.new_data = new_data
|
||||
lineparser.save()
|
||||
assert (tmpdir / 'file').read() == self._get_expected(new_data)
|
||||
|
||||
def test_clear(self, tmpdir, lineparser):
|
||||
"""Check if calling clear() empties both pending and persisted data."""
|
||||
lineparser.new_data = ['one', 'two']
|
||||
lineparser.save()
|
||||
assert (tmpdir / 'file').read() == "old data 1\nold data 2\none\ntwo\n"
|
||||
|
||||
lineparser.new_data = ['one', 'two']
|
||||
lineparser.clear()
|
||||
lineparser.save()
|
||||
assert not lineparser.new_data
|
||||
assert (tmpdir / 'file').read() == ""
|
||||
|
||||
def test_iter_without_open(self, lineparser):
|
||||
"""Test __iter__ without having called open()."""
|
||||
with pytest.raises(ValueError):
|
||||
iter(lineparser)
|
||||
|
||||
def test_iter(self, lineparser):
|
||||
"""Test __iter__."""
|
||||
new_data = ['new data 1', 'new data 2']
|
||||
lineparser.new_data = new_data
|
||||
with lineparser.open():
|
||||
assert list(lineparser) == self.BASE_DATA + new_data
|
||||
|
||||
def test_iter_not_found(self, mocker):
|
||||
"""Test __iter__ with no file."""
|
||||
open_mock = mocker.patch(
|
||||
'qutebrowser.misc.lineparser.AppendLineParser._open')
|
||||
open_mock.side_effect = FileNotFoundError
|
||||
new_data = ['new data 1', 'new data 2']
|
||||
linep = lineparsermod.AppendLineParser('foo', 'bar')
|
||||
linep.new_data = new_data
|
||||
with linep.open():
|
||||
assert list(linep) == new_data
|
||||
|
||||
def test_get_recent_none(self, tmpdir):
|
||||
"""Test get_recent with no data."""
|
||||
(tmpdir / 'file2').ensure()
|
||||
linep = lineparsermod.AppendLineParser(str(tmpdir), 'file2')
|
||||
assert linep.get_recent() == []
|
||||
|
||||
def test_get_recent_little(self, lineparser):
|
||||
"""Test get_recent with little data."""
|
||||
data = [e + '\n' for e in self.BASE_DATA]
|
||||
assert lineparser.get_recent() == data
|
||||
|
||||
def test_get_recent_much(self, lineparser):
|
||||
"""Test get_recent with much data."""
|
||||
size = 64
|
||||
new_data = ['new data {}'.format(i) for i in range(size)]
|
||||
lineparser.new_data = new_data
|
||||
lineparser.save()
|
||||
data = os.linesep.join(self.BASE_DATA + new_data) + os.linesep
|
||||
data = [e + '\n' for e in data[-size:].splitlines()]
|
||||
assert lineparser.get_recent(size) == data
|
||||
|
Loading…
Reference in New Issue
Block a user