cq: Format with black and isort

This commit is contained in:
Philipp Hörist
2025-01-25 19:15:37 +01:00
parent e6e71d82bf
commit 841b1fb25e
44 changed files with 1641 additions and 1660 deletions
+91 -104
View File
@@ -17,23 +17,33 @@
from __future__ import annotations
from typing import Any
from typing import Callable
from typing import cast
from typing import Union
import logging
import subprocess
from functools import partial
from typing import Any, Callable, Union, cast
from gajim.common import app, ged
from gajim.common.const import PROPAGATE_EVENT, STOP_EVENT
from gajim.common.events import MessageReceived, Notification, PresenceReceived
from nbxmpp.protocol import JID
from gajim.common import app
from gajim.common import ged
from gajim.common.const import PROPAGATE_EVENT
from gajim.common.const import STOP_EVENT
from gajim.common.events import MessageReceived
from gajim.common.events import Notification
from gajim.common.events import PresenceReceived
from gajim.common.helpers import play_sound_file
from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _
from nbxmpp.protocol import JID
from triggers.gtk.config import ConfigDialog
from triggers.util import RuleResult, log_result
from triggers.util import log_result
from triggers.util import RuleResult
log = logging.getLogger('gajim.p.triggers')
log = logging.getLogger("gajim.p.triggers")
ProcessableEventsT = Union[MessageReceived, Notification, PresenceReceived]
RuleT = dict[str, Any]
@@ -42,36 +52,37 @@ RuleT = dict[str, Any]
class Triggers(GajimPlugin):
def init(self) -> None:
self.description = _(
'Configure Gajims behaviour with triggers for each contact')
"Configure Gajims behaviour with triggers for each contact"
)
self.config_dialog = partial(ConfigDialog, self)
self.config_default_values = {}
self.events_handlers = {
'notification': (ged.PREGUI, self._on_notification),
'message-received': (ged.PREGUI2, self._on_message_received),
'gc-message-received': (ged.PREGUI2, self._on_message_received),
"notification": (ged.PREGUI, self._on_notification),
"message-received": (ged.PREGUI2, self._on_message_received),
"gc-message-received": (ged.PREGUI2, self._on_message_received),
# 'presence-received': (ged.PREGUI, self._on_presence_received),
}
def _on_notification(self, event: Notification) -> bool:
log.info('Process %s, %s', event.name, event.type)
result = self._check_all(event,
self._check_rule_apply_notification,
self._apply_rule)
log.info('Result: %s', result)
log.info("Process %s, %s", event.name, event.type)
result = self._check_all(
event, self._check_rule_apply_notification, self._apply_rule
)
log.info("Result: %s", result)
return self._excecute_notification_rules(result, event)
def _on_message_received(self, event: MessageReceived) -> bool:
log.info('Process %s', event.name)
log.info("Process %s", event.name)
message = event.message
if message.text is None:
log.info('Discard event because it has no message text')
log.info("Discard event because it has no message text")
return PROPAGATE_EVENT
result = self._check_all(event,
self._check_rule_apply_msg_received,
self._apply_rule)
log.info('Result: %s', result)
result = self._check_all(
event, self._check_rule_apply_msg_received, self._apply_rule
)
log.info("Result: %s", result)
return self._excecute_message_rules(result)
def _on_presence_received(self, event: PresenceReceived) -> None:
@@ -86,11 +97,12 @@ class Triggers(GajimPlugin):
check_func = self._check_rule_apply_status_changed
self._check_all(event, check_func, self._apply_rule)
def _check_all(self,
event: ProcessableEventsT,
check_func: Callable[..., bool],
apply_func: Callable[..., Any]
) -> RuleResult:
def _check_all(
self,
event: ProcessableEventsT,
check_func: Callable[..., bool],
apply_func: Callable[..., Any],
) -> RuleResult:
result = RuleResult()
@@ -101,7 +113,7 @@ class Triggers(GajimPlugin):
rule = cast(RuleT, self.config[str(num)])
if check_func(event, rule):
apply_func(result, rule)
if 'one_shot' in rule and rule['one_shot']:
if "one_shot" in rule and rule["one_shot"]:
to_remove.append(num)
decal = 0
@@ -121,47 +133,38 @@ class Triggers(GajimPlugin):
return result
@log_result
def _check_rule_apply_msg_received(self,
event: MessageReceived,
rule: RuleT
) -> bool:
def _check_rule_apply_msg_received(
self, event: MessageReceived, rule: RuleT
) -> bool:
return self._check_rule_all('message_received', event, rule)
return self._check_rule_all("message_received", event, rule)
@log_result
def _check_rule_apply_connected(self,
event: PresenceReceived,
rule: RuleT
) -> bool:
def _check_rule_apply_connected(self, event: PresenceReceived, rule: RuleT) -> bool:
return self._check_rule_all('contact_connected', event, rule)
return self._check_rule_all("contact_connected", event, rule)
@log_result
def _check_rule_apply_disconnected(self,
event: PresenceReceived,
rule: RuleT
) -> bool:
def _check_rule_apply_disconnected(
self, event: PresenceReceived, rule: RuleT
) -> bool:
return self._check_rule_all('contact_disconnected', event, rule)
return self._check_rule_all("contact_disconnected", event, rule)
@log_result
def _check_rule_apply_status_changed(self,
event: PresenceReceived,
rule: RuleT
) -> bool:
def _check_rule_apply_status_changed(
self, event: PresenceReceived, rule: RuleT
) -> bool:
return self._check_rule_all('contact_status_change', event, rule)
return self._check_rule_all("contact_status_change", event, rule)
@log_result
def _check_rule_apply_notification(self,
event: Notification,
rule: RuleT
) -> bool:
def _check_rule_apply_notification(self, event: Notification, rule: RuleT) -> bool:
# Check notification type
notif_type = ''
if event.type == 'incoming-message':
notif_type = 'message_received'
notif_type = ""
if event.type == "incoming-message":
notif_type = "message_received"
# if event.type == 'pres':
# # TODO:
# if (event.base_event.old_show < 2 and
@@ -175,14 +178,12 @@ class Triggers(GajimPlugin):
return self._check_rule_all(notif_type, event, rule)
def _check_rule_all(self,
notif_type: str,
event: ProcessableEventsT,
rule: RuleT
) -> bool:
def _check_rule_all(
self, notif_type: str, event: ProcessableEventsT, rule: RuleT
) -> bool:
# Check notification type
if rule['event'] != notif_type:
if rule["event"] != notif_type:
return False
# notification type is ok. Now check recipient
@@ -205,21 +206,17 @@ class Triggers(GajimPlugin):
return True
@log_result
def _check_rule_recipients(self,
event: ProcessableEventsT,
rule: RuleT
) -> bool:
def _check_rule_recipients(self, event: ProcessableEventsT, rule: RuleT) -> bool:
rule_recipients = [t.strip() for t in rule['recipients'].split(',')]
if rule['recipient_type'] == 'groupchat':
rule_recipients = [t.strip() for t in rule["recipients"].split(",")]
if rule["recipient_type"] == "groupchat":
if event.jid in rule_recipients:
return True
return False
if (rule['recipient_type'] == 'contact' and event.jid not in
rule_recipients):
if rule["recipient_type"] == "contact" and event.jid not in rule_recipients:
return False
client = app.get_client(event.account)
contact = client.get_module('Contacts').get_contact(event.jid)
contact = client.get_module("Contacts").get_contact(event.jid)
if contact.is_groupchat or not contact.is_in_roster:
return False
@@ -229,84 +226,74 @@ class Triggers(GajimPlugin):
if group in rule_recipients:
group_found = True
break
if rule['recipient_type'] == 'group' and not group_found:
if rule["recipient_type"] == "group" and not group_found:
return False
return True
@log_result
def _check_rule_status(self,
event: ProcessableEventsT,
rule: RuleT
) -> bool:
def _check_rule_status(self, event: ProcessableEventsT, rule: RuleT) -> bool:
rule_statuses = rule['status'].split()
rule_statuses = rule["status"].split()
client = app.get_client(event.account)
if rule['status'] != 'all' and client.status not in rule_statuses:
if rule["status"] != "all" and client.status not in rule_statuses:
return False
return True
@log_result
def _check_rule_tab_opened(self,
event: ProcessableEventsT,
rule: RuleT
) -> bool:
def _check_rule_tab_opened(self, event: ProcessableEventsT, rule: RuleT) -> bool:
if rule['tab_opened'] == 'both':
if rule["tab_opened"] == "both":
return True
tab_opened = False
assert isinstance(event.jid, JID)
if app.window.chat_exists(event.account, event.jid):
tab_opened = True
if tab_opened and rule['tab_opened'] == 'no':
if tab_opened and rule["tab_opened"] == "no":
return False
elif not tab_opened and rule['tab_opened'] == 'yes':
elif not tab_opened and rule["tab_opened"] == "yes":
return False
return True
@log_result
def _check_rule_has_focus(self,
event: ProcessableEventsT,
rule: RuleT
) -> bool:
def _check_rule_has_focus(self, event: ProcessableEventsT, rule: RuleT) -> bool:
if rule['has_focus'] == 'both':
if rule["has_focus"] == "both":
return True
if rule['tab_opened'] == 'no':
if rule["tab_opened"] == "no":
# Does not apply in this case
return True
assert isinstance(event.jid, JID)
chat_active = app.window.is_chat_active(event.account, event.jid)
if chat_active and rule['has_focus'] == 'no':
if chat_active and rule["has_focus"] == "no":
return False
elif not chat_active and rule['has_focus'] == 'yes':
elif not chat_active and rule["has_focus"] == "yes":
return False
return True
def _apply_rule(self, result: RuleResult, rule: RuleT) -> None:
if rule['sound'] == 'no':
if rule["sound"] == "no":
result.sound = False
result.sound_file = None
elif rule['sound'] == 'yes':
elif rule["sound"] == "yes":
result.sound = False
result.sound_file = rule['sound_file']
result.sound_file = rule["sound_file"]
if rule['run_command']:
result.command = rule['command']
if rule["run_command"]:
result.command = rule["command"]
if rule['popup'] == 'no':
if rule["popup"] == "no":
result.show_notification = False
elif rule['popup'] == 'yes':
elif rule["popup"] == "yes":
result.show_notification = True
def _excecute_notification_rules(self,
result: RuleResult,
event: Notification
) -> bool:
def _excecute_notification_rules(
self, result: RuleResult, event: Notification
) -> bool:
if result.sound is False:
event.sound = None
@@ -324,7 +311,7 @@ class Triggers(GajimPlugin):
if result.command is not None:
try:
subprocess.Popen(f'{result.command} &', shell=True).wait()
subprocess.Popen(f"{result.command} &", shell=True).wait()
except Exception:
pass