rename hooks to use "ac_" (account) and "dc_" (global)

This commit is contained in:
holger krekel
2020-03-29 07:34:48 +02:00
parent 7e1470ea46
commit ca88c5b41c
11 changed files with 67 additions and 67 deletions

View File

@@ -5,7 +5,7 @@
- introduced PerAccount and Global hooks that plugins can implement
- introduced `member_added()` and `member_removed()` plugin events.
- introduced `ac_member_added()` and `ac_member_removed()` plugin events.
- introduced two documented examples for an echo and a group-membership
tracking plugin.

View File

@@ -6,7 +6,7 @@ from deltachat import account_hookimpl, run_cmdline
class EchoPlugin:
@account_hookimpl
def process_incoming_message(self, message):
def ac_incoming_message(self, message):
print("process_incoming message", message)
if message.text.strip() == "/quit":
message.account.shutdown()
@@ -18,8 +18,8 @@ class EchoPlugin:
message.chat.send_text("echoing from {}:\n{}".format(addr, text))
@account_hookimpl
def process_message_delivered(self, message):
print("process_message_delivered", message)
def ac_message_delivered(self, message):
print("ac_message_delivered", message)
def main(argv=None):

View File

@@ -6,7 +6,7 @@ from deltachat import account_hookimpl, run_cmdline
class GroupTrackingPlugin:
@account_hookimpl
def process_incoming_message(self, message):
def ac_incoming_message(self, message):
print("process_incoming message", message)
if message.text.strip() == "/quit":
message.account.shutdown()
@@ -18,18 +18,18 @@ class GroupTrackingPlugin:
message.chat.send_text("echoing from {}:\n{}".format(addr, text))
@account_hookimpl
def configure_completed(self, success):
print("*** configure_completed:", success)
def ac_configure_completed(self, success):
print("*** ac_configure_completed:", success)
@account_hookimpl
def member_added(self, chat, contact):
print("*** member_added", contact.addr, "from", chat)
def ac_member_added(self, chat, contact):
print("*** ac_member_added", contact.addr, "from", chat)
for member in chat.get_contacts():
print("chat member: {}".format(member.addr))
@account_hookimpl
def member_removed(self, chat, contact):
print("*** member_removed", contact.addr, "from", chat)
def ac_member_removed(self, chat, contact):
print("*** ac_member_removed", contact.addr, "from", chat)
def main(argv=None):

View File

@@ -38,7 +38,7 @@ def test_group_tracking_plugin(acfactory, lp):
ac1, ac2 = acfactory.get_two_online_accounts(quiet=True)
botproc.fnmatch_lines("""
*configure_completed: True*
*ac_configure_completed: True*
""")
ac1.add_account_plugin(FFIEventLogger(ac1, "ac1"))
ac2.add_account_plugin(FFIEventLogger(ac2, "ac2"))
@@ -50,7 +50,7 @@ def test_group_tracking_plugin(acfactory, lp):
ch.send_text("hello")
botproc.fnmatch_lines("""
*member_added {}*
*ac_member_added {}*
""".format(ac1.get_config("addr")))
lp.sec("adding third member {}".format(ac2.get_config("addr")))
@@ -62,11 +62,11 @@ def test_group_tracking_plugin(acfactory, lp):
lp.sec("now looking at what the bot received")
botproc.fnmatch_lines("""
*member_added {}*
*ac_member_added {}*
""".format(contact3.addr))
lp.sec("contact successfully added, now removing")
ch.remove_contact(contact3)
botproc.fnmatch_lines("""
*member_removed {}*
*ac_member_removed {}*
""".format(contact3.addr))

View File

@@ -60,10 +60,10 @@ class Account(object):
raise ValueError("Could not dc_open: {}".format(db_path))
self._configkeys = self.get_config("sys.config_keys").split()
atexit.register(self.shutdown)
hook.account_init(account=self)
hook.dc_account_init(account=self)
@hookspec.account_hookimpl
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
name, kwargs = self._map_ffi_event(ffi_event)
if name is not None:
ev = HookEvent(self, name=name, kwargs=kwargs)
@@ -72,8 +72,8 @@ class Account(object):
# def __del__(self):
# self.shutdown()
def log_line(self, msg):
self._pm.hook.log_line(message=msg)
def ac_log_line(self, msg):
self._pm.hook.ac_log_line(message=msg)
def _check_config_key(self, name):
if name not in self._configkeys:
@@ -579,7 +579,7 @@ class Account(object):
atexit.unregister(self.shutdown)
self._shutdown_event.set()
hook = hookspec.Global._get_plugin_manager().hook
hook.account_after_shutdown(account=self, dc_context=dc_context)
hook.dc_account_after_shutdown(account=self, dc_context=dc_context)
def _handle_current_events(self):
""" handle all currently queued events and then return. """
@@ -611,26 +611,26 @@ class Account(object):
data1 = ffi_event.data1
if data1 == 0 or data1 == 1000:
success = data1 == 1000
return "configure_completed", dict(success=success)
return "ac_configure_completed", dict(success=success)
elif name == "DC_EVENT_INCOMING_MSG":
msg = self.get_message_by_id(ffi_event.data2)
return "process_incoming_message", dict(message=msg)
return "ac_incoming_message", dict(message=msg)
elif name == "DC_EVENT_MSGS_CHANGED":
if ffi_event.data2 != 0:
msg = self.get_message_by_id(ffi_event.data2)
if msg.is_in_fresh():
return "process_incoming_message", dict(message=msg)
return "ac_incoming_message", dict(message=msg)
elif name == "DC_EVENT_MSG_DELIVERED":
msg = self.get_message_by_id(ffi_event.data2)
return "process_message_delivered", dict(message=msg)
return "ac_message_delivered", dict(message=msg)
elif name == "DC_EVENT_MEMBER_ADDED":
chat = self.get_chat_by_id(ffi_event.data1)
contact = self.get_contact_by_id(ffi_event.data2)
return "member_added", dict(chat=chat, contact=contact)
return "ac_member_added", dict(chat=chat, contact=contact)
elif name == "DC_EVENT_MEMBER_REMOVED":
chat = self.get_chat_by_id(ffi_event.data1)
contact = self.get_contact_by_id(ffi_event.data2)
return "member_removed", dict(chat=chat, contact=contact)
return "ac_member_removed", dict(chat=chat, contact=contact)
return None, {}

View File

@@ -7,19 +7,19 @@ from .hookspec import account_hookimpl, global_hookimpl
@global_hookimpl
def account_init(account):
def dc_account_init(account):
# send all FFI events for this account to a plugin hook
def _ll_event(ctx, evt_name, data1, data2):
assert ctx == account._dc_context
ffi_event = FFIEvent(name=evt_name, data1=data1, data2=data2)
account._pm.hook.process_ffi_event(
account._pm.hook.ac_process_ffi_event(
account=account, ffi_event=ffi_event
)
deltachat.set_context_callback(account._dc_context, _ll_event)
@global_hookimpl
def account_after_shutdown(dc_context):
def dc_account_after_shutdown(dc_context):
deltachat.clear_context_callback(dc_context)
@@ -50,17 +50,17 @@ class FFIEventLogger:
self.init_time = time.time()
@account_hookimpl
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
self._log_event(ffi_event)
def _log_event(self, ffi_event):
# don't show events that are anyway empty impls now
if ffi_event.name == "DC_EVENT_GET_STRING":
return
self.account.log_line(str(ffi_event))
self.account.ac_log_line(str(ffi_event))
@account_hookimpl
def log_line(self, message):
def ac_log_line(self, message):
t = threading.currentThread()
tname = getattr(t, "name", t)
if tname == "MainThread":
@@ -81,7 +81,7 @@ class FFIEventTracker:
self._event_queue = Queue()
@account_hookimpl
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
self._event_queue.put(ffi_event)
def set_timeout(self, timeout):
@@ -110,7 +110,7 @@ class FFIEventTracker:
assert not rex.match(ev.name), "event found {}".format(ev)
def get_matching(self, event_name_regex, check_error=True, timeout=None):
self.account.log_line("-- waiting for event with regex: {} --".format(event_name_regex))
self.account.ac_log_line("-- waiting for event with regex: {} --".format(event_name_regex))
rex = re.compile("(?:{}).*".format(event_name_regex))
while 1:
ev = self.get(timeout=timeout, check_error=check_error)

View File

@@ -3,29 +3,29 @@
import pluggy
_account_name = "deltachat-account"
account_hookspec = pluggy.HookspecMarker(_account_name)
account_hookimpl = pluggy.HookimplMarker(_account_name)
account_spec_name = "deltachat-account"
account_hookspec = pluggy.HookspecMarker(account_spec_name)
account_hookimpl = pluggy.HookimplMarker(account_spec_name)
_global_name = "deltachat-global"
global_hookspec = pluggy.HookspecMarker(_global_name)
global_hookimpl = pluggy.HookimplMarker(_global_name)
global_spec_name = "deltachat-global"
global_hookspec = pluggy.HookspecMarker(global_spec_name)
global_hookimpl = pluggy.HookimplMarker(global_spec_name)
class PerAccount:
""" per-Account-instance hook specifications.
Except for process_ffi_event all hooks are executed
Except for ac_process_ffi_event all hooks are executed
in the thread which calls Account.wait_shutdown().
"""
@classmethod
def _make_plugin_manager(cls):
pm = pluggy.PluginManager(_account_name)
pm = pluggy.PluginManager(account_spec_name)
pm.add_hookspecs(cls)
return pm
@account_hookspec
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
""" process a CFFI low level events for a given account.
ffi_event has "name", "data1", "data2" values as specified
@@ -37,27 +37,27 @@ class PerAccount:
"""
@account_hookspec
def log_line(self, message):
def ac_log_line(self, message):
""" log a message related to the account. """
@account_hookspec
def configure_completed(self, success):
def ac_configure_completed(self, success):
""" Called when a configure process completed. """
@account_hookspec
def process_incoming_message(self, message):
def ac_incoming_message(self, message):
""" Called on any incoming message (to deaddrop or chat). """
@account_hookspec
def process_message_delivered(self, message):
def ac_message_delivered(self, message):
""" Called when an outgoing message has been delivered to SMTP. """
@account_hookspec
def member_added(self, chat, contact):
def ac_member_added(self, chat, contact):
""" Called for each contact added to a chat. """
@account_hookspec
def member_removed(self, chat, contact):
def ac_member_removed(self, chat, contact):
""" Called for each contact removed from a chat. """
@@ -71,14 +71,14 @@ class Global:
@classmethod
def _get_plugin_manager(cls):
if cls._plugin_manager is None:
cls._plugin_manager = pm = pluggy.PluginManager(_global_name)
cls._plugin_manager = pm = pluggy.PluginManager(global_spec_name)
pm.add_hookspecs(cls)
return cls._plugin_manager
@global_hookspec
def account_init(self, account):
def dc_account_init(self, account):
""" called when `Account::__init__()` function starts executing. """
@global_hookspec
def account_after_shutdown(self, account, dc_context):
def dc_account_after_shutdown(self, account, dc_context):
""" Called after the account has been shutdown. """

View File

@@ -38,9 +38,9 @@ class IOThreads:
@contextmanager
def log_execution(self, message):
self.account.log_line(message + " START")
self.account.ac_log_line(message + " START")
yield
self.account.log_line(message + " FINISHED")
self.account.ac_log_line(message + " FINISHED")
def stop(self, wait=False):
self._thread_quitflag = True
@@ -68,7 +68,7 @@ class IOThreads:
ev = next(it)
except StopIteration:
break
self.account.log_line("calling hook name={} kwargs={}".format(ev.name, ev.kwargs))
self.account.ac_log_line("calling hook name={} kwargs={}".format(ev.name, ev.kwargs))
ev.call_hook()
def imap_thread_run(self):

View File

@@ -14,7 +14,7 @@ class ImexTracker:
self._imex_events = Queue()
@account_hookimpl
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
if ffi_event.name == "DC_EVENT_IMEX_PROGRESS":
self._imex_events.put(ffi_event.data1)
elif ffi_event.name == "DC_EVENT_IMEX_FILE_WRITTEN":
@@ -47,7 +47,7 @@ class ConfigureTracker:
self._ffi_events = []
@account_hookimpl
def process_ffi_event(self, ffi_event):
def ac_process_ffi_event(self, ffi_event):
self._ffi_events.append(ffi_event)
if ffi_event.name == "DC_EVENT_SMTP_CONNECTED":
self._smtp_finished.set()
@@ -55,7 +55,7 @@ class ConfigureTracker:
self._imap_finished.set()
@account_hookimpl
def configure_completed(self, success):
def ac_configure_completed(self, success):
self._configure_events.put(success)
def wait_smtp_connected(self):

View File

@@ -178,7 +178,7 @@ class TestOfflineChat:
chat.add_contact(contact1)
for ev in ac1.iter_events(timeout=1):
if ev.name == "member_added":
if ev.name == "ac_member_added":
assert ev.kwargs["chat"] == chat
if ev.kwargs["contact"] == ac1.get_self_contact():
continue
@@ -193,7 +193,7 @@ class TestOfflineChat:
ac1._handle_current_events()
chat.remove_contact(contact1)
for ev in ac1.iter_events(timeout=1):
if ev.name == "member_removed":
if ev.name == "ac_member_removed":
assert ev.kwargs["chat"] == chat
if ev.kwargs["contact"] == ac1.get_self_contact():
continue
@@ -463,11 +463,11 @@ class TestOfflineChat:
class InPlugin:
@account_hookimpl
def member_added(self, chat, contact):
def ac_member_added(self, chat, contact):
in_list.append(("added", chat, contact))
@account_hookimpl
def member_removed(self, chat, contact):
def ac_member_removed(self, chat, contact):
in_list.append(("removed", chat, contact))
ac1.add_account_plugin(InPlugin())
@@ -1051,14 +1051,14 @@ class TestOnlineAccount:
class InPlugin:
@account_hookimpl
def process_incoming_message(self, message):
def ac_incoming_message(self, message):
message_queue.put(message)
delivered = queue.Queue()
class OutPlugin:
@account_hookimpl
def process_message_delivered(self, message):
def ac_message_delivered(self, message):
delivered.put(message)
ac1.add_account_plugin(OutPlugin())
@@ -1287,11 +1287,11 @@ class TestOnlineAccount:
class InPlugin:
@account_hookimpl
def member_added(self, chat, contact):
def ac_member_added(self, chat, contact):
in_list.put(("added", chat, contact))
@account_hookimpl
def member_removed(self, chat, contact):
def ac_member_removed(self, chat, contact):
in_list.put(("removed", chat, contact))
ac2.add_account_plugin(InPlugin())

View File

@@ -26,7 +26,7 @@ def test_dc_close_events(tmpdir, acfactory):
class ShutdownPlugin:
@global_hookimpl
def account_after_shutdown(self, account):
def dc_account_after_shutdown(self, account):
assert account._dc_context is None
shutdowns.append(account)
register_global_plugin(ShutdownPlugin())