Merge pull request #1358 from coffeedogs/final_code_quality_1

Changes based on style and lint checks. (final_code_quality_1)
This commit is contained in:
coffeedogs 2018-10-31 12:45:20 +00:00 committed by GitHub
commit 2d4cb1e2ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,16 @@
"""
src/bitmessageqt/foldertree.py
==============================
"""
# pylint: disable=too-many-arguments,bad-super-call,attribute-defined-outside-init
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from tr import _translate
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from helper_sql import * from helper_sql import sqlExecute, sqlQuery
from utils import avatarize
from settingsmixin import SettingsMixin from settingsmixin import SettingsMixin
from tr import _translate
from utils import avatarize
# for pylupdate # for pylupdate
_translate("MainWindow", "inbox") _translate("MainWindow", "inbox")
@ -14,6 +20,7 @@ _translate("MainWindow", "trash")
class AccountMixin(object): class AccountMixin(object):
"""UI-related functionality for accounts"""
ALL = 0 ALL = 0
NORMAL = 1 NORMAL = 1
CHAN = 2 CHAN = 2
@ -22,38 +29,42 @@ class AccountMixin(object):
BROADCAST = 5 BROADCAST = 5
def accountColor(self): def accountColor(self):
"""QT UI color for an account"""
if not self.isEnabled: if not self.isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
elif self.type == self.CHAN: elif self.type == self.CHAN:
return QtGui.QColor(216, 119, 0) return QtGui.QColor(216, 119, 0)
elif self.type in [self.MAILINGLIST, self.SUBSCRIPTION]: elif self.type in [self.MAILINGLIST, self.SUBSCRIPTION]:
return QtGui.QColor(137, 04, 177) return QtGui.QColor(137, 4, 177)
else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def folderColor(self): def folderColor(self):
"""QT UI color for a folder"""
if not self.parent().isEnabled: if not self.parent().isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
def accountBrush(self): def accountBrush(self):
"""Account brush (for QT UI)"""
brush = QtGui.QBrush(self.accountColor()) brush = QtGui.QBrush(self.accountColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
return brush return brush
def folderBrush(self): def folderBrush(self):
"""Folder brush (for QT UI)"""
brush = QtGui.QBrush(self.folderColor()) brush = QtGui.QBrush(self.folderColor())
brush.setStyle(QtCore.Qt.NoBrush) brush.setStyle(QtCore.Qt.NoBrush)
return brush return brush
def setAddress(self, address): def setAddress(self, address):
"""Set bitmessage address of the object"""
if address is None: if address is None:
self.address = None self.address = None
else: else:
self.address = str(address) self.address = str(address)
def setUnreadCount(self, cnt): def setUnreadCount(self, cnt):
"""Set number of unread messages"""
try: try:
if self.unreadCount == int(cnt): if self.unreadCount == int(cnt):
return return
@ -64,6 +75,7 @@ class AccountMixin(object):
self.emitDataChanged() self.emitDataChanged()
def setEnabled(self, enabled): def setEnabled(self, enabled):
"""Set account enabled (QT UI)"""
self.isEnabled = enabled self.isEnabled = enabled
try: try:
self.setExpanded(enabled) self.setExpanded(enabled)
@ -77,6 +89,7 @@ class AccountMixin(object):
self.emitDataChanged() self.emitDataChanged()
def setType(self): def setType(self):
"""Set account type (QT UI)"""
self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable) self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable)
if self.address is None: if self.address is None:
self.type = self.ALL self.type = self.ALL
@ -92,6 +105,7 @@ class AccountMixin(object):
self.type = self.NORMAL self.type = self.NORMAL
def defaultLabel(self): def defaultLabel(self):
"""Default label (in case no label is set manually)"""
queryreturn = None queryreturn = None
retval = None retval = None
if self.type in ( if self.type in (
@ -100,7 +114,7 @@ class AccountMixin(object):
try: try:
retval = unicode( retval = unicode(
BMConfigParser().get(self.address, 'label'), 'utf-8') BMConfigParser().get(self.address, 'label'), 'utf-8')
except Exception as e: except Exception:
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select label from addressbook where address=?''', self.address) '''select label from addressbook where address=?''', self.address)
elif self.type == AccountMixin.SUBSCRIPTION: elif self.type == AccountMixin.SUBSCRIPTION:
@ -120,6 +134,7 @@ class AccountMixin(object):
class BMTreeWidgetItem(QtGui.QTreeWidgetItem, AccountMixin): class BMTreeWidgetItem(QtGui.QTreeWidgetItem, AccountMixin):
"""A common abstract class for Tree widget item""" """A common abstract class for Tree widget item"""
def __init__(self, parent, pos, address, unreadCount): def __init__(self, parent, pos, address, unreadCount):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
self.setAddress(address) self.setAddress(address)
@ -127,9 +142,10 @@ class BMTreeWidgetItem(QtGui.QTreeWidgetItem, AccountMixin):
self._setup(parent, pos) self._setup(parent, pos)
def _getAddressBracket(self, unreadCount=False): def _getAddressBracket(self, unreadCount=False):
return (" (" + str(self.unreadCount) + ")") if unreadCount else "" return " (" + str(self.unreadCount) + ")" if unreadCount else ""
def data(self, column, role): def data(self, column, role):
"""Override internal QT method for returning object data"""
if column == 0: if column == 0:
if role == QtCore.Qt.DisplayRole: if role == QtCore.Qt.DisplayRole:
return self._getLabel() + self._getAddressBracket( return self._getLabel() + self._getAddressBracket(
@ -146,6 +162,7 @@ class BMTreeWidgetItem(QtGui.QTreeWidgetItem, AccountMixin):
class Ui_FolderWidget(BMTreeWidgetItem): class Ui_FolderWidget(BMTreeWidgetItem):
"""Item in the account/folder tree representing a folder"""
folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4} folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4}
def __init__( def __init__(
@ -161,9 +178,11 @@ class Ui_FolderWidget(BMTreeWidgetItem):
return _translate("MainWindow", self.folderName) return _translate("MainWindow", self.folderName)
def setFolderName(self, fname): def setFolderName(self, fname):
"""Set folder name (for QT UI)"""
self.folderName = str(fname) self.folderName = str(fname)
def data(self, column, role): def data(self, column, role):
"""Override internal QT method for returning object data"""
if column == 0 and role == QtCore.Qt.ForegroundRole: if column == 0 and role == QtCore.Qt.ForegroundRole:
return self.folderBrush() return self.folderBrush()
return super(Ui_FolderWidget, self).data(column, role) return super(Ui_FolderWidget, self).data(column, role)
@ -183,15 +202,14 @@ class Ui_FolderWidget(BMTreeWidgetItem):
self.treeWidget().header().sortIndicatorOrder() self.treeWidget().header().sortIndicatorOrder()
if x == y: if x == y:
return self.folderName < other.folderName return self.folderName < other.folderName
else: return x >= y if reverse else x < y
return (x >= y if reverse else x < y)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin): class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
def __init__( """Item in the account/folder tree representing an account"""
self, parent, pos=0, address=None, unreadCount=0, enabled=True): def __init__(self, parent, pos=0, address=None, unreadCount=0, enabled=True):
super(Ui_AddressWidget, self).__init__( super(Ui_AddressWidget, self).__init__(
parent, pos, address, unreadCount) parent, pos, address, unreadCount)
self.setEnabled(enabled) self.setEnabled(enabled)
@ -220,6 +238,7 @@ class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
return ret return ret
def data(self, column, role): def data(self, column, role):
"""Override internal QT method for returning object data"""
if column == 0: if column == 0:
if role == QtCore.Qt.DecorationRole: if role == QtCore.Qt.DecorationRole:
return avatarize( return avatarize(
@ -229,6 +248,7 @@ class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
return super(Ui_AddressWidget, self).data(column, role) return super(Ui_AddressWidget, self).data(column, role)
def setData(self, column, role, value): def setData(self, column, role, value):
"""Save account label (if you edit in the the UI, this will be triggered and will save it to keys.dat)"""
if role == QtCore.Qt.EditRole \ if role == QtCore.Qt.EditRole \
and self.type != AccountMixin.SUBSCRIPTION: and self.type != AccountMixin.SUBSCRIPTION:
BMConfigParser().set( BMConfigParser().set(
@ -241,6 +261,7 @@ class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
return super(Ui_AddressWidget, self).setData(column, role, value) return super(Ui_AddressWidget, self).setData(column, role, value)
def setAddress(self, address): def setAddress(self, address):
"""Set address to object (for QT UI)"""
super(Ui_AddressWidget, self).setAddress(address) super(Ui_AddressWidget, self).setAddress(address)
self.setData(0, QtCore.Qt.UserRole, self.address) self.setData(0, QtCore.Qt.UserRole, self.address)
@ -249,6 +270,7 @@ class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
# label (or address) alphabetically, disabled at the end # label (or address) alphabetically, disabled at the end
def __lt__(self, other): def __lt__(self, other):
# pylint: disable=protected-access
if isinstance(other, Ui_AddressWidget): if isinstance(other, Ui_AddressWidget):
reverse = QtCore.Qt.DescendingOrder == \ reverse = QtCore.Qt.DescendingOrder == \
self.treeWidget().header().sortIndicatorOrder() self.treeWidget().header().sortIndicatorOrder()
@ -265,9 +287,9 @@ class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
class Ui_SubscriptionWidget(Ui_AddressWidget): class Ui_SubscriptionWidget(Ui_AddressWidget):
def __init__( """Special treating of subscription addresses"""
self, parent, pos=0, address="", unreadCount=0, label="", # pylint: disable=unused-argument
enabled=True): def __init__(self, parent, pos=0, address="", unreadCount=0, label="", enabled=True):
super(Ui_SubscriptionWidget, self).__init__( super(Ui_SubscriptionWidget, self).__init__(
parent, pos, address, unreadCount, enabled) parent, pos, address, unreadCount, enabled)
@ -281,10 +303,12 @@ class Ui_SubscriptionWidget(Ui_AddressWidget):
return unicode(self.address, 'utf-8') return unicode(self.address, 'utf-8')
def setType(self): def setType(self):
"""Set account type"""
super(Ui_SubscriptionWidget, self).setType() # sets it editable super(Ui_SubscriptionWidget, self).setType() # sets it editable
self.type = AccountMixin.SUBSCRIPTION # overrides type self.type = AccountMixin.SUBSCRIPTION # overrides type
def setData(self, column, role, value): def setData(self, column, role, value):
"""Save subscription label to database"""
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
if isinstance(value, QtCore.QVariant): if isinstance(value, QtCore.QVariant):
label = str( label = str(
@ -299,6 +323,7 @@ class Ui_SubscriptionWidget(Ui_AddressWidget):
class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin): class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin):
"""A common abstract class for Table widget item""" """A common abstract class for Table widget item"""
def __init__(self, parent=None, label=None, unread=False): def __init__(self, parent=None, label=None, unread=False):
super(QtGui.QTableWidgetItem, self).__init__() super(QtGui.QTableWidgetItem, self).__init__()
self.setLabel(label) self.setLabel(label)
@ -308,12 +333,15 @@ class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin):
parent.append(self) parent.append(self)
def setLabel(self, label): def setLabel(self, label):
"""Set object label"""
self.label = label self.label = label
def setUnread(self, unread): def setUnread(self, unread):
"""Set/unset read state of an item"""
self.unread = unread self.unread = unread
def data(self, role): def data(self, role):
"""Return object data (QT UI)"""
if role in ( if role in (
QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole
): ):
@ -327,10 +355,12 @@ class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin):
class BMAddressWidget(BMTableWidgetItem, AccountMixin): class BMAddressWidget(BMTableWidgetItem, AccountMixin):
"""A common class for Table widget item with account""" """A common class for Table widget item with account"""
def _setup(self): def _setup(self):
self.setEnabled(True) self.setEnabled(True)
def data(self, role): def data(self, role):
"""Return object data (QT UI)"""
if role == QtCore.Qt.ToolTipRole: if role == QtCore.Qt.ToolTipRole:
return self.label + " (" + self.address + ")" return self.label + " (" + self.address + ")"
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
@ -343,6 +373,7 @@ class BMAddressWidget(BMTableWidgetItem, AccountMixin):
class MessageList_AddressWidget(BMAddressWidget): class MessageList_AddressWidget(BMAddressWidget):
"""Address item in a messagelist"""
def __init__(self, parent, address=None, label=None, unread=False): def __init__(self, parent, address=None, label=None, unread=False):
self.setAddress(address) self.setAddress(address)
super(MessageList_AddressWidget, self).__init__(parent, label, unread) super(MessageList_AddressWidget, self).__init__(parent, label, unread)
@ -353,6 +384,7 @@ class MessageList_AddressWidget(BMAddressWidget):
self.setType() self.setType()
def setLabel(self, label=None): def setLabel(self, label=None):
"""Set label"""
super(MessageList_AddressWidget, self).setLabel(label) super(MessageList_AddressWidget, self).setLabel(label)
if label is not None: if label is not None:
return return
@ -378,11 +410,13 @@ class MessageList_AddressWidget(BMAddressWidget):
self.label = newLabel self.label = newLabel
def data(self, role): def data(self, role):
"""Return object data (QT UI)"""
if role == QtCore.Qt.UserRole: if role == QtCore.Qt.UserRole:
return self.address return self.address
return super(MessageList_AddressWidget, self).data(role) return super(MessageList_AddressWidget, self).data(role)
def setData(self, role, value): def setData(self, role, value):
"""Set object data"""
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
self.setLabel() self.setLabel()
return super(MessageList_AddressWidget, self).setData(role, value) return super(MessageList_AddressWidget, self).setData(role, value)
@ -395,6 +429,7 @@ class MessageList_AddressWidget(BMAddressWidget):
class MessageList_SubjectWidget(BMTableWidgetItem): class MessageList_SubjectWidget(BMTableWidgetItem):
"""Message list subject item"""
def __init__(self, parent, subject=None, label=None, unread=False): def __init__(self, parent, subject=None, label=None, unread=False):
self.setSubject(subject) self.setSubject(subject)
super(MessageList_SubjectWidget, self).__init__(parent, label, unread) super(MessageList_SubjectWidget, self).__init__(parent, label, unread)
@ -403,9 +438,11 @@ class MessageList_SubjectWidget(BMTableWidgetItem):
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
def setSubject(self, subject): def setSubject(self, subject):
"""Set subject"""
self.subject = subject self.subject = subject
def data(self, role): def data(self, role):
"""Return object data (QT UI)"""
if role == QtCore.Qt.UserRole: if role == QtCore.Qt.UserRole:
return self.subject return self.subject
return super(MessageList_SubjectWidget, self).data(role) return super(MessageList_SubjectWidget, self).data(role)
@ -418,16 +455,20 @@ class MessageList_SubjectWidget(BMTableWidgetItem):
class Ui_AddressBookWidgetItem(BMAddressWidget): class Ui_AddressBookWidgetItem(BMAddressWidget):
def __init__(self, label=None, type=AccountMixin.NORMAL): """Addressbook item"""
self.type = type # pylint: disable=unused-argument
def __init__(self, label=None, acc_type=AccountMixin.NORMAL):
self.type = acc_type
super(Ui_AddressBookWidgetItem, self).__init__(label=label) super(Ui_AddressBookWidgetItem, self).__init__(label=label)
def data(self, role): def data(self, role):
"""Return object data"""
if role == QtCore.Qt.UserRole: if role == QtCore.Qt.UserRole:
return self.type return self.type
return super(Ui_AddressBookWidgetItem, self).data(role) return super(Ui_AddressBookWidgetItem, self).data(role)
def setData(self, role, value): def setData(self, role, value):
"""Set data"""
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
self.label = str( self.label = str(
value.toString().toUtf8() value.toString().toUtf8()
@ -455,49 +496,57 @@ class Ui_AddressBookWidgetItem(BMAddressWidget):
if self.type == other.type: if self.type == other.type:
return self.label.lower() < other.label.lower() return self.label.lower() < other.label.lower()
else: return not reverse if self.type < other.type else reverse
return (not reverse if self.type < other.type else reverse)
return super(QtGui.QTableWidgetItem, self).__lt__(other) return super(QtGui.QTableWidgetItem, self).__lt__(other)
class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
def __init__(self, address, label, type): """Addressbook label item"""
super(Ui_AddressBookWidgetItemLabel, self).__init__(label, type) def __init__(self, address, label, acc_type):
super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type)
self.address = address self.address = address
def data(self, role): def data(self, role):
"""Return object data"""
self.label = self.defaultLabel() self.label = self.defaultLabel()
return super(Ui_AddressBookWidgetItemLabel, self).data(role) return super(Ui_AddressBookWidgetItemLabel, self).data(role)
class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
def __init__(self, address, label, type): """Addressbook address item"""
super(Ui_AddressBookWidgetItemAddress, self).__init__(address, type) def __init__(self, address, label, acc_type):
super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type)
self.address = address self.address = address
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
def data(self, role): def data(self, role):
"""Return object data"""
if role == QtCore.Qt.ToolTipRole: if role == QtCore.Qt.ToolTipRole:
return self.address return self.address
if role == QtCore.Qt.DecorationRole: if role == QtCore.Qt.DecorationRole:
return return None
return super(Ui_AddressBookWidgetItemAddress, self).data(role) return super(Ui_AddressBookWidgetItemAddress, self).data(role)
class AddressBookCompleter(QtGui.QCompleter): class AddressBookCompleter(QtGui.QCompleter):
"""Addressbook completer"""
def __init__(self): def __init__(self):
super(AddressBookCompleter, self).__init__() super(AddressBookCompleter, self).__init__()
self.cursorPos = -1 self.cursorPos = -1
def onCursorPositionChanged(self, oldPos, newPos): def onCursorPositionChanged(self, oldPos, newPos): # pylint: disable=unused-argument
"""Callback for cursor position change"""
if oldPos != self.cursorPos: if oldPos != self.cursorPos:
self.cursorPos = -1 self.cursorPos = -1
def splitPath(self, path): def splitPath(self, path):
"""Split on semicolon"""
text = unicode(path.toUtf8(), 'utf-8') text = unicode(path.toUtf8(), 'utf-8')
return [text[:self.widget().cursorPosition()].split(';')[-1].strip()] return [text[:self.widget().cursorPosition()].split(';')[-1].strip()]
def pathFromIndex(self, index): def pathFromIndex(self, index):
"""Perform autocompletion (reimplemented QCompleter method)"""
autoString = unicode( autoString = unicode(
index.data(QtCore.Qt.EditRole).toString().toUtf8(), 'utf-8') index.data(QtCore.Qt.EditRole).toString().toUtf8(), 'utf-8')
text = unicode(self.widget().text().toUtf8(), 'utf-8') text = unicode(self.widget().text().toUtf8(), 'utf-8')