From aba61e57a852b2157a5145c6518bb8a4bb030e32 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 17 May 2019 18:43:06 +0300 Subject: [PATCH] flake8 and style fixes and docstrings in helper_search --- src/bitmessageqt/__init__.py | 5 +- src/helper_search.py | 147 ++++++++++++++++++++--------------- 2 files changed, 86 insertions(+), 66 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 0fdfd915..8433498f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1213,8 +1213,7 @@ class MyForm(settingsmixin.SMainWindow): queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False) for row in queryreturn: - toAddress, fromAddress, subject, status, ackdata, lastactiontime = row - self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime) + self.addMessageListItemSent(tableWidget, *row) tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) @@ -1246,7 +1245,7 @@ class MyForm(settingsmixin.SMainWindow): queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly) for row in queryreturn: - msgfolder, msgid, toAddress, fromAddress, subject, received, read = row + toAddress, fromAddress, subject, msgfolder, msgid, received, read = row self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read) tableWidget.horizontalHeader().setSortIndicator( diff --git a/src/helper_search.py b/src/helper_search.py index 69acec43..9fcb88b5 100644 --- a/src/helper_search.py +++ b/src/helper_search.py @@ -1,92 +1,113 @@ -"""Additional SQL helper for searching messages""" +""" +Additional SQL helper for searching messages. +Used by :mod:`.bitmessageqt`. +""" from helper_sql import sqlQuery - -try: - from PyQt4 import QtGui - haveQt = True -except ImportError: - haveQt = False +from tr import _translate -def search_translate(context, text): - """Translation wrapper""" - if haveQt: - return QtGui.QApplication.translate(context, text) - return text.lower() +def search_sql( + xAddress='toaddress', account=None, folder='inbox', where=None, + what=None, unreadOnly=False +): + """ + Search for messages from given account and folder having search term + in one of it's fields. - -def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False): - """Perform a search in mailbox tables""" + :param str xAddress: address field checked + ('fromaddress', 'toaddress' or 'both') + :param account: the account which is checked + :type account: :class:`.bitmessageqt.account.BMAccount` + instance + :param str folder: the folder which is checked + :param str where: message field which is checked ('toaddress', + 'fromaddress', 'subject' or 'message'), by default check any field + :param str what: the search term + :param bool unreadOnly: if True, search only for unread messages + :return: all messages where field contains + :rtype: list[list] + """ # pylint: disable=too-many-arguments, too-many-branches - if what is not None and what != "": - what = "%" + what + "%" - if where == search_translate("MainWindow", "To"): - where = "toaddress" - elif where == search_translate("MainWindow", "From"): - where = "fromaddress" - elif where == search_translate("MainWindow", "Subject"): - where = "subject" - elif where == search_translate("MainWindow", "Message"): - where = "message" + if what: + what = '%' + what + '%' + if where == _translate("MainWindow", "To"): + where = 'toaddress' + elif where == _translate("MainWindow", "From"): + where = 'fromaddress' + elif where == _translate("MainWindow", "Subject"): + where = 'subject' + elif where == _translate("MainWindow", "Message"): + where = 'message' else: - where = "toaddress || fromaddress || subject || message" - else: - what = None + where = 'toaddress || fromaddress || subject || message' - if folder == "sent": - sqlStatementBase = ''' - SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime - FROM sent ''' - else: - sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read - FROM inbox ''' + sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + ( + 'status, ackdata, lastactiontime FROM sent ' if folder == 'sent' + else 'folder, msgid, received, read FROM inbox ' + ) sqlStatementParts = [] sqlArguments = [] if account is not None: if xAddress == 'both': - sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)") + sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)') sqlArguments.append(account) sqlArguments.append(account) else: - sqlStatementParts.append(xAddress + " = ? ") + sqlStatementParts.append(xAddress + ' = ? ') sqlArguments.append(account) if folder is not None: - if folder == "new": - folder = "inbox" + if folder == 'new': + folder = 'inbox' unreadOnly = True - sqlStatementParts.append("folder = ? ") + sqlStatementParts.append('folder = ? ') sqlArguments.append(folder) else: - sqlStatementParts.append("folder != ?") - sqlArguments.append("trash") - if what is not None: - sqlStatementParts.append("%s LIKE ?" % (where)) + sqlStatementParts.append('folder != ?') + sqlArguments.append('trash') + if what: + sqlStatementParts.append('%s LIKE ?' % (where)) sqlArguments.append(what) if unreadOnly: - sqlStatementParts.append("read = 0") + sqlStatementParts.append('read = 0') if sqlStatementParts: - sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts) - if folder == "sent": - sqlStatementBase += " ORDER BY lastactiontime" + sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts) + if folder == 'sent': + sqlStatementBase += ' ORDER BY lastactiontime' return sqlQuery(sqlStatementBase, sqlArguments) -def check_match(toAddress, fromAddress, subject, message, where=None, what=None): - """Check if a single message matches a filter (used when new messages are added to messagelists)""" +def check_match( + toAddress, fromAddress, subject, message, where=None, what=None): + """ + Check if a single message matches a filter (used when new messages + are added to messagelists) + """ # pylint: disable=too-many-arguments - if what is not None and what != "": - if where in (search_translate("MainWindow", "To"), search_translate("MainWindow", "All")): - if what.lower() not in toAddress.lower(): - return False - elif where in (search_translate("MainWindow", "From"), search_translate("MainWindow", "All")): - if what.lower() not in fromAddress.lower(): - return False - elif where in (search_translate("MainWindow", "Subject"), search_translate("MainWindow", "All")): - if what.lower() not in subject.lower(): - return False - elif where in (search_translate("MainWindow", "Message"), search_translate("MainWindow", "All")): - if what.lower() not in message.lower(): - return False + if not what: + return True + + if where in ( + _translate("MainWindow", "To"), _translate("MainWindow", "All") + ): + if what.lower() not in toAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "From"), _translate("MainWindow", "All") + ): + if what.lower() not in fromAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "Subject"), + _translate("MainWindow", "All") + ): + if what.lower() not in subject.lower(): + return False + elif where in ( + _translate("MainWindow", "Message"), + _translate("MainWindow", "All") + ): + if what.lower() not in message.lower(): + return False return True