Fix pylint and flake8 for SQL work.

Vanity commit. This also touches up a few comments.
This commit is contained in:
Ryan Roden-Corrent 2017-02-15 12:46:37 -05:00
parent ea9217a61f
commit ffd044b52b
6 changed files with 24 additions and 21 deletions

View File

@ -36,7 +36,7 @@ class Entry:
Attributes: Attributes:
atime: The time the page was accessed. atime: The time the page was accessed.
url: The URL which was accessed as QUrl. url: The URL which was accessed as QUrl.
redirect: If True, don't save this entry to disk redirect: If True, don't show this entry in completion
""" """
def __init__(self, atime, url, title, redirect=False): def __init__(self, atime, url, title, redirect=False):

View File

@ -22,13 +22,13 @@
import re import re
from PyQt5.QtCore import Qt, QModelIndex, QAbstractItemModel from PyQt5.QtCore import Qt, QModelIndex, QAbstractItemModel
from PyQt5.QtSql import QSqlQuery, QSqlQueryModel, QSqlDatabase from PyQt5.QtSql import QSqlQueryModel
from qutebrowser.utils import log from qutebrowser.utils import log
from qutebrowser.misc import sql from qutebrowser.misc import sql
class SqlCompletionCategory(QSqlQueryModel): class _SqlCompletionCategory(QSqlQueryModel):
def __init__(self, name, sort_by, sort_order, limit, select, where, def __init__(self, name, sort_by, sort_order, limit, select, where,
columns_to_filter, parent=None): columns_to_filter, parent=None):
super().__init__(parent=parent) super().__init__(parent=parent)
@ -105,10 +105,10 @@ class SqlCompletionModel(QAbstractItemModel):
Return: A new CompletionCategory. Return: A new CompletionCategory.
""" """
cat = SqlCompletionCategory(name, parent=self, sort_by=sort_by, cat = _SqlCompletionCategory(name, parent=self, sort_by=sort_by,
sort_order=sort_order, limit=limit, sort_order=sort_order, limit=limit,
select=select, where=where, select=select, where=where,
columns_to_filter=self.columns_to_filter) columns_to_filter=self.columns_to_filter)
self._categories.append(cat) self._categories.append(cat)
def data(self, index, role=Qt.DisplayRole): def data(self, index, role=Qt.DisplayRole):

View File

@ -39,13 +39,13 @@ def _delete_url(completion):
category = index.parent() category = index.parent()
index = category.child(index.row(), _URLCOL) index = category.child(index.row(), _URLCOL)
catname = category.data() catname = category.data()
url = index.data()
qtutils.ensure_valid(category) qtutils.ensure_valid(category)
if catname == 'Bookmarks': if catname == 'Bookmarks':
log.completion.debug('Deleting bookmark {}'.format(url)) urlstr = index.data()
log.completion.debug('Deleting bookmark {}'.format(urlstr))
bookmark_manager = objreg.get('bookmark-manager') bookmark_manager = objreg.get('bookmark-manager')
bookmark_manager.delete(url) bookmark_manager.delete(urlstr)
else: else:
assert catname == 'Quickmarks', 'Unknown category {}'.format(catname) assert catname == 'Quickmarks', 'Unknown category {}'.format(catname)
quickmark_manager = objreg.get('quickmark-manager') quickmark_manager = objreg.get('quickmark-manager')

View File

@ -65,6 +65,7 @@ class SqlTable(QObject):
"""Interface to a sql table. """Interface to a sql table.
Attributes: Attributes:
Entry: The class wrapping row data from this table.
_name: Name of the SQL table this wraps. _name: Name of the SQL table this wraps.
_primary_key: The primary key of the table. _primary_key: The primary key of the table.
@ -87,8 +88,9 @@ class SqlTable(QObject):
super().__init__(parent) super().__init__(parent)
self._name = name self._name = name
self._primary_key = primary_key self._primary_key = primary_key
run_query("CREATE TABLE {} ({}, PRIMARY KEY ({}))" run_query("CREATE TABLE {} ({}, PRIMARY KEY ({}))".format(
.format(name, ','.join(fields), primary_key)) name, ','.join(fields), primary_key))
# pylint: disable=invalid-name
self.Entry = collections.namedtuple(name + '_Entry', fields) self.Entry = collections.namedtuple(name + '_Entry', fields)
def __iter__(self): def __iter__(self):
@ -104,8 +106,8 @@ class SqlTable(QObject):
Args: Args:
key: Primary key value to search for. key: Primary key value to search for.
""" """
query = run_query("SELECT * FROM {} where {} = ?" query = run_query("SELECT * FROM {} where {} = ?".format(
.format(self._name, self._primary_key), [key]) self._name, self._primary_key), [key])
return query.next() return query.next()
def __len__(self): def __len__(self):
@ -120,8 +122,8 @@ class SqlTable(QObject):
Args: Args:
key: Primary key value to fetch. key: Primary key value to fetch.
""" """
result = run_query("SELECT * FROM {} where {} = ?" result = run_query("SELECT * FROM {} where {} = ?".format(
.format(self._name, self._primary_key), [key]) self._name, self._primary_key), [key])
result.next() result.next()
rec = result.record() rec = result.record()
return self.Entry(*[rec.value(i) for i in range(rec.count())]) return self.Entry(*[rec.value(i) for i in range(rec.count())])
@ -137,8 +139,8 @@ class SqlTable(QObject):
The number of rows deleted. The number of rows deleted.
""" """
field = field or self._primary_key field = field or self._primary_key
query = run_query("DELETE FROM {} where {} = ?" query = run_query("DELETE FROM {} where {} = ?".format(
.format(self._name, field), [value]) self._name, field), [value])
if not query.numRowsAffected(): if not query.numRowsAffected():
raise KeyError('No row with {} = "{}"'.format(field, value)) raise KeyError('No row with {} = "{}"'.format(field, value))
self.changed.emit() self.changed.emit()
@ -152,8 +154,8 @@ class SqlTable(QObject):
""" """
cmd = "REPLACE" if replace else "INSERT" cmd = "REPLACE" if replace else "INSERT"
paramstr = ','.join(['?'] * len(values)) paramstr = ','.join(['?'] * len(values))
run_query("{} INTO {} values({})" run_query("{} INTO {} values({})".format(cmd, self._name, paramstr),
.format(cmd, self._name, paramstr), values) values)
self.changed.emit() self.changed.emit()
def delete_all(self): def delete_all(self):

View File

@ -36,7 +36,7 @@ class FakeCompletionModel(QStandardItemModel):
def __init__(self, kind, *pos_args, parent=None): def __init__(self, kind, *pos_args, parent=None):
super().__init__(parent) super().__init__(parent)
self.kind = kind self.kind = kind
self.pos_args = [*pos_args] self.pos_args = list(pos_args)
class CompletionWidgetStub(QObject): class CompletionWidgetStub(QObject):

View File

@ -232,6 +232,7 @@ def test_where():
model.new_category('test_where', where='not c') model.new_category('test_where', where='not c')
_check_model(model, [('test_where', [('foo', 'bar', False)])]) _check_model(model, [('test_where', [('foo', 'bar', False)])])
def test_entry(): def test_entry():
table = sql.SqlTable('test_entry', ['a', 'b', 'c'], primary_key='a') table = sql.SqlTable('test_entry', ['a', 'b', 'c'], primary_key='a')
assert hasattr(table.Entry, 'a') assert hasattr(table.Entry, 'a')