[anti_spam] Type annotations, linting

This commit is contained in:
wurstsalat
2022-11-29 19:30:31 +01:00
parent fae39c2c29
commit 171a6f5cf9
4 changed files with 21 additions and 13 deletions

View File

@@ -1 +1 @@
from .anti_spam import AntiSpamPlugin from .anti_spam import AntiSpamPlugin # type: ignore

View File

@@ -11,7 +11,6 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>. # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
#
''' '''
:author: Yann Leboulanger <asterix@lagaule.org> :author: Yann Leboulanger <asterix@lagaule.org>
@@ -30,7 +29,7 @@ from anti_spam.config_dialog import AntiSpamConfigDialog
class AntiSpamPlugin(GajimPlugin): class AntiSpamPlugin(GajimPlugin):
def init(self): def init(self) -> None:
self.description = _('Allows you to block various kinds of incoming ' self.description = _('Allows you to block various kinds of incoming '
'messages (Spam, XHTML formatting, etc.)') 'messages (Spam, XHTML formatting, etc.)')
self.config_dialog = partial(AntiSpamConfigDialog, self) self.config_dialog = partial(AntiSpamConfigDialog, self)

View File

@@ -17,6 +17,7 @@ from __future__ import annotations
from typing import Any from typing import Any
from typing import cast from typing import cast
from typing import TYPE_CHECKING
from gi.repository import Gtk from gi.repository import Gtk
@@ -27,9 +28,12 @@ from gajim.gui.const import SettingType
from gajim.plugins.plugins_i18n import _ from gajim.plugins.plugins_i18n import _
if TYPE_CHECKING:
from .anti_spam import AntiSpamPlugin
class AntiSpamConfigDialog(SettingsDialog): class AntiSpamConfigDialog(SettingsDialog):
def __init__(self, plugin, parent): def __init__(self, plugin: AntiSpamPlugin, parent: Gtk.Window) -> None:
self.plugin = plugin self.plugin = plugin
msgtxt_limit = cast(int, self.plugin.config['msgtxt_limit']) msgtxt_limit = cast(int, self.plugin.config['msgtxt_limit'])
max_length = '' if msgtxt_limit == 0 else msgtxt_limit max_length = '' if msgtxt_limit == 0 else msgtxt_limit
@@ -91,8 +95,12 @@ class AntiSpamConfigDialog(SettingsDialog):
'in group chats')), 'in group chats')),
] ]
SettingsDialog.__init__(self, parent, _('Anti Spam Configuration'), SettingsDialog.__init__(self,
Gtk.DialogFlags.MODAL, settings, None) parent,
_('Anti Spam Configuration'),
Gtk.DialogFlags.MODAL,
settings,
'')
def _on_setting(self, value: Any, data: Any) -> None: def _on_setting(self, value: Any, data: Any) -> None:
self.plugin.config[data] = value self.plugin.config[data] = value

View File

@@ -11,7 +11,6 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>. # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import annotations from __future__ import annotations
@@ -28,7 +27,7 @@ from nbxmpp.structs import StanzaHandler
from gajim.common import app from gajim.common import app
from gajim.common import ged from gajim.common import ged
from gajim.common import types from gajim.common.client import Client
from gajim.common.events import MessageSent from gajim.common.events import MessageSent
from gajim.common.modules.base import BaseModule from gajim.common.modules.base import BaseModule
@@ -38,7 +37,7 @@ zeroconf = False
class AntiSpam(BaseModule): class AntiSpam(BaseModule):
def __init__(self, client: types.Client) -> None: def __init__(self, client: Client) -> None:
BaseModule.__init__(self, client, plugin=True) BaseModule.__init__(self, client, plugin=True)
self.handlers = [ self.handlers = [
@@ -68,10 +67,11 @@ class AntiSpam(BaseModule):
self._contacted_jids.add(event.jid) self._contacted_jids.add(event.jid)
def _message_received(self, def _message_received(self,
_con: types.xmppClient, _con: Client,
_stanza: Message, _stanza: Message,
properties: MessageProperties properties: MessageProperties
) -> None: ) -> None:
if properties.is_sent_carbon: if properties.is_sent_carbon:
# Another device already sent a message # Another device already sent a message
assert properties.jid assert properties.jid
@@ -135,7 +135,7 @@ class AntiSpam(BaseModule):
if is_muc_pm or roster_item is None: if is_muc_pm or roster_item is None:
assert properties.body assert properties.body
if answer in properties.body.split('\n'): if answer in properties.body.split('\n'):
if msg_from not in whitelist: if str(msg_from) not in whitelist:
whitelist.append(str(msg_from)) whitelist.append(str(msg_from))
# We need to explicitly save, because 'append' does not # We need to explicitly save, because 'append' does not
# implement the __setitem__ method # implement the __setitem__ method
@@ -152,10 +152,11 @@ class AntiSpam(BaseModule):
self._log.info('Anti spam question sent to %s', jid) self._log.info('Anti spam question sent to %s', jid)
def _subscribe_received(self, def _subscribe_received(self,
_con: types.xmppClient, _con: Client,
_stanza: Presence, _stanza: Presence,
properties: PresenceProperties properties: PresenceProperties
) -> None: ) -> None:
msg_from = properties.jid msg_from = properties.jid
block_sub = self._config['block_subscription_requests'] block_sub = self._config['block_subscription_requests']
roster_item = self._client.get_module('Roster').get_item(msg_from) roster_item = self._client.get_module('Roster').get_item(msg_from)
@@ -166,5 +167,5 @@ class AntiSpam(BaseModule):
raise NodeProcessed raise NodeProcessed
def get_instance(*args: Any, **kwargs: Any) -> None: def get_instance(*args: Any, **kwargs: Any) -> tuple[AntiSpam, str]:
return AntiSpam(*args, **kwargs), 'AntiSpam' return AntiSpam(*args, **kwargs), 'AntiSpam'