plugin XEP-0050: use Enums and add type hints:

- Replaces namedtuple with enums which are more appropriate here.
- Add docstrings and type hints to `AdHocCommand`

rel 465
This commit is contained in:
Goffi 2025-10-01 17:57:47 +02:00
commit a32b8bf9e8

View file

@ -19,6 +19,7 @@
from collections import OrderedDict
import os.path
from typing import cast
import uuid
from twisted.internet import defer
@ -29,6 +30,7 @@ from wokkel import data_form
from libervia.backend.core.constants import Const as C
from libervia.backend.core.i18n import D_, _
from libervia.backend.core.log import getLogger
from libervia.backend.plugins.plugin_xep_0050 import XEP_0050, Action, Error, Note, Status
log = getLogger(__name__)
@ -124,7 +126,7 @@ class AdHocDBus:
method=self._ad_hoc_remotes_get,
async_=True,
)
self._c = host.plugins["XEP-0050"]
self._c = cast(XEP_0050, host.plugins["XEP-0050"])
host.register_namespace("mediaplayer", NS_MEDIA_PLAYER)
self.session_con = None
@ -295,7 +297,7 @@ class AdHocDBus:
if len(actions) == 1:
# it's our first request, we ask the desired new status
status = self._c.STATUS.EXECUTING
status = Status.EXECUTING
form = data_form.Form("form", title=_("Command selection"))
options = []
for path, iface, command in methods:
@ -319,10 +321,10 @@ class AdHocDBus:
answer_form = data_form.Form.fromElement(x_elt)
command = answer_form["command"]
except (KeyError, StopIteration):
raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD)
raise self._c.ad_hoc_error(Error.BAD_PAYLOAD)
if command not in names_map:
raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD)
raise self._c.ad_hoc_error(Error.BAD_PAYLOAD)
path, iface, command = names_map[command]
proxy = await self.session_con.getRemoteObject(bus_name, path)
@ -337,15 +339,15 @@ class AdHocDBus:
del actions[:]
names_map.clear()
return await d_bus_callback(
client, None, session_data, self._c.ACTION.EXECUTE, node
client, None, session_data, Action.EXECUTE, node
)
form = data_form.Form("form", title=_("Updated"))
form.addField(data_form.Field("fixed", "Command sent"))
status = self._c.STATUS.COMPLETED
status = Status.COMPLETED
payload = None
note = (self._c.NOTE.INFO, _("Command sent"))
note = (Note.INFO, _("Command sent"))
else:
raise self._c.AdHocError(self._c.ERROR.INTERNAL)
raise self._c.ad_hoc_error(Error.INTERNAL)
return (payload, status, None, note)
@ -408,7 +410,7 @@ class AdHocDBus:
client,
device_jid,
NS_MEDIA_PLAYER,
action=self._c.ACTION.CANCEL,
action=Action.CANCEL,
session_id=session_id,
)
@ -458,10 +460,10 @@ class AdHocDBus:
bus_names = await self._dbus_list_names()
bus_names = [b for b in bus_names if b.startswith(MPRIS_PREFIX)]
if len(bus_names) == 0:
note = (self._c.NOTE.INFO, D_("No media player found."))
return (None, self._c.STATUS.COMPLETED, None, note)
note = (Note.INFO, D_("No media player found."))
return (None, Status.COMPLETED, None, note)
options = []
status = self._c.STATUS.EXECUTING
status = Status.EXECUTING
form = data_form.Form(
"form", title=D_("Media Player Selection"), formNamespace=NS_MEDIA_PLAYER
)
@ -491,8 +493,8 @@ class AdHocDBus:
"Hack attempt? Refused bus: {bus_name}"
).format(bus_name=bus_name)
)
note = (self._c.NOTE.ERROR, D_("Invalid player name."))
return (None, self._c.STATUS.COMPLETED, None, note)
note = (Note.ERROR, D_("Invalid player name."))
return (None, Status.COMPLETED, None, note)
try:
proxy = await self.session_con.getRemoteObject(
@ -500,8 +502,8 @@ class AdHocDBus:
)
except Exception as e:
log.warning(_("Can't get D-Bus proxy: {reason}").format(reason=e))
note = (self._c.NOTE.ERROR, D_("Media player is not available anymore"))
return (None, self._c.STATUS.COMPLETED, None, note)
note = (Note.ERROR, D_("Media player is not available anymore"))
return (None, Status.COMPLETED, None, note)
try:
command = command_form["command"]
except KeyError:
@ -543,5 +545,5 @@ class AdHocDBus:
)
payload = form.toElement()
status = self._c.STATUS.EXECUTING
status = Status.EXECUTING
return (payload, status, None, None)