Mercurial > libervia-backend
diff sat/plugins/plugin_exp_pubsub_hook.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_pubsub_hook.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_exp_pubsub_hook.py Sat Apr 08 13:54:42 2023 +0200 @@ -56,51 +56,51 @@ log.info(_("PubSub Hook initialization")) self.host = host self.node_hooks = {} # keep track of the number of hooks per node (for all profiles) - host.bridge.addMethod( - "psHookAdd", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook + host.bridge.add_method( + "ps_hook_add", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook ) - host.bridge.addMethod( - "psHookRemove", + host.bridge.add_method( + "ps_hook_remove", ".plugin", in_sign="sssss", out_sign="i", method=self._removeHook, ) - host.bridge.addMethod( - "psHookList", + host.bridge.add_method( + "ps_hook_list", ".plugin", in_sign="s", out_sign="aa{ss}", - method=self._listHooks, + method=self._list_hooks, ) @defer.inlineCallbacks - def profileConnected(self, client): + def profile_connected(self, client): hooks = client._hooks = persistent.PersistentBinaryDict( NS_PUBSUB_HOOK, client.profile ) client._hooks_temporary = {} yield hooks.load() for node in hooks: - self._installNodeManager(client, node) + self._install_node_manager(client, node) - def profileDisconnected(self, client): + def profile_disconnected(self, client): for node in client._hooks: - self._removeNodeManager(client, node) + self._remove_node_manager(client, node) - def _installNodeManager(self, client, node): + def _install_node_manager(self, client, node): if node in self.node_hooks: log.debug(_("node manager already set for {node}").format(node=node)) self.node_hooks[node] += 1 else: # first hook on this node - self.host.plugins["XEP-0060"].addManagedNode( - node, items_cb=self._itemsReceived + self.host.plugins["XEP-0060"].add_managed_node( + node, items_cb=self._items_received ) self.node_hooks[node] = 0 log.info(_("node manager installed on {node}").format(node=node)) - def _removeNodeManager(self, client, node): + def _remove_node_manager(self, client, node): try: self.node_hooks[node] -= 1 except KeyError: @@ -108,12 +108,12 @@ else: if self.node_hooks[node] == 0: del self.node_hooks[node] - self.host.plugins["XEP-0060"].removeManagedNode(node, self._itemsReceived) + self.host.plugins["XEP-0060"].remove_managed_node(node, self._items_received) log.debug(_("hook removed")) else: log.debug(_("node still needed for an other hook")) - def installHook(self, client, service, node, hook_type, hook_arg, persistent): + def install_hook(self, client, service, node, hook_type, hook_arg, persistent): if hook_type not in HOOK_TYPES: raise exceptions.DataError( _("{hook_type} is not handled").format(hook_type=hook_type) @@ -124,7 +124,7 @@ hook_type=hook_type ) ) - self._installNodeManager(client, node) + self._install_node_manager(client, node) hook_data = {"service": service, "type": hook_type, "arg": hook_arg} if persistent: @@ -143,7 +143,7 @@ ) ) - def _itemsReceived(self, client, itemsEvent): + def _items_received(self, client, itemsEvent): node = itemsEvent.nodeIdentifier for hooks in (client._hooks, client._hooks_temporary): if node not in hooks: @@ -188,9 +188,9 @@ ) def _addHook(self, service, node, hook_type, hook_arg, persistent, profile): - client = self.host.getClient(profile) + client = self.host.get_client(profile) service = jid.JID(service) if service else client.jid.userhostJID() - return self.addHook( + return self.add_hook( client, service, str(node), @@ -199,7 +199,7 @@ persistent, ) - def addHook(self, client, service, node, hook_type, hook_arg, persistent): + def add_hook(self, client, service, node, hook_type, hook_arg, persistent): r"""Add a hook which will be triggered on a pubsub notification @param service(jid.JID): service of the node @@ -219,21 +219,21 @@ can be a module path, file path, python code """ assert service is not None - return self.installHook(client, service, node, hook_type, hook_arg, persistent) + return self.install_hook(client, service, node, hook_type, hook_arg, persistent) def _removeHook(self, service, node, hook_type, hook_arg, profile): - client = self.host.getClient(profile) + client = self.host.get_client(profile) service = jid.JID(service) if service else client.jid.userhostJID() - return self.removeHook(client, service, node, hook_type or None, hook_arg or None) + return self.remove_hook(client, service, node, hook_type or None, hook_arg or None) - def removeHook(self, client, service, node, hook_type=None, hook_arg=None): + def remove_hook(self, client, service, node, hook_type=None, hook_arg=None): """Remove a persistent or temporaty root @param service(jid.JID): service of the node @param node(unicode): Pubsub node - @param hook_type(unicode, None): same as for [addHook] + @param hook_type(unicode, None): same as for [add_hook] match all if None - @param hook_arg(unicode, None): same as for [addHook] + @param hook_arg(unicode, None): same as for [add_hook] match all if None @return(int): number of hooks removed """ @@ -254,20 +254,20 @@ if not hooks[node]: # no more hooks, we can remove the node del hooks[node] - self._removeNodeManager(client, node) + self._remove_node_manager(client, node) else: if hooks == client._hooks: hooks.force(node) return removed - def _listHooks(self, profile): - hooks_list = self.listHooks(self.host.getClient(profile)) + def _list_hooks(self, profile): + hooks_list = self.list_hooks(self.host.get_client(profile)) for hook in hooks_list: hook["service"] = hook["service"].full() - hook["persistent"] = C.boolConst(hook["persistent"]) + hook["persistent"] = C.bool_const(hook["persistent"]) return hooks_list - def listHooks(self, client): + def list_hooks(self, client): """return list of registered hooks""" hooks_list = [] for hooks in (client._hooks, client._hooks_temporary):