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:
parent
a37101adc7
commit
a32b8bf9e8
1 changed files with 241 additions and 157 deletions
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
|
||||
import time
|
||||
from typing import cast
|
||||
from uuid import uuid4
|
||||
|
||||
from twisted.internet import defer
|
||||
|
|
@ -30,6 +31,7 @@ from libervia.backend.core.core_types import SatXMPPEntity
|
|||
from libervia.backend.core.i18n import D_, _
|
||||
from libervia.backend.core.log import getLogger
|
||||
from libervia.backend.memory import persistent
|
||||
from libervia.backend.plugins.plugin_xep_0050 import XEP_0050, Error, Note, Status
|
||||
from libervia.backend.tools.common import data_format, date_utils
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
|
@ -51,7 +53,7 @@ class AdHocRegistration:
|
|||
def __init__(self, host):
|
||||
log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
|
||||
self.host = host
|
||||
self._c = host.plugins["XEP-0050"]
|
||||
self._c = cast(XEP_0050, host.plugins["XEP-0050"])
|
||||
self.ad_hoc_registration_data = persistent.LazyPersistentBinaryDict(
|
||||
"registration_links"
|
||||
)
|
||||
|
|
@ -134,7 +136,7 @@ class AdHocRegistration:
|
|||
|
||||
if len(actions) == 1:
|
||||
# First request, presenting the form to the user
|
||||
status = self._c.STATUS.EXECUTING
|
||||
status = Status.EXECUTING
|
||||
form = data_form.Form("form", title=D_("Create Registration Link"))
|
||||
|
||||
form.addField(
|
||||
|
|
@ -216,18 +218,18 @@ class AdHocRegistration:
|
|||
|
||||
await self.ad_hoc_registration_data.aset(link_id, link_data)
|
||||
|
||||
status = self._c.STATUS.COMPLETED
|
||||
status = Status.COMPLETED
|
||||
payload = None
|
||||
note = (
|
||||
self._c.NOTE.INFO,
|
||||
Note.INFO,
|
||||
D_("Registration link created successfully: {link_id}").format(
|
||||
link_id=link_id
|
||||
),
|
||||
)
|
||||
except (KeyError, StopIteration, ValueError):
|
||||
raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD)
|
||||
raise self._c.ad_hoc_error(Error.BAD_PAYLOAD)
|
||||
else:
|
||||
raise self._c.AdHocError(self._c.ERROR.INTERNAL)
|
||||
raise self._c.ad_hoc_error(Error.INTERNAL)
|
||||
|
||||
return (payload, status, None, note)
|
||||
|
||||
|
|
@ -256,7 +258,7 @@ class AdHocRegistration:
|
|||
|
||||
if len(actions) == 1:
|
||||
all_links = await self.ad_hoc_registration_data.all()
|
||||
status = self._c.STATUS.EXECUTING
|
||||
status = Status.EXECUTING
|
||||
|
||||
form = data_form.Form("form", title=D_("Registered Links"))
|
||||
for link_id, link_data in all_links.items():
|
||||
|
|
@ -272,10 +274,10 @@ class AdHocRegistration:
|
|||
payload = form.toElement()
|
||||
note = None
|
||||
|
||||
status = self._c.STATUS.COMPLETED
|
||||
status = Status.COMPLETED
|
||||
|
||||
else:
|
||||
raise self._c.AdHocError(self._c.ERROR.INTERNAL)
|
||||
raise self._c.ad_hoc_error(Error.INTERNAL)
|
||||
|
||||
return (payload, status, None, note)
|
||||
|
||||
|
|
@ -305,7 +307,7 @@ class AdHocRegistration:
|
|||
|
||||
if len(actions) == 1:
|
||||
all_links = await self.ad_hoc_registration_data.all()
|
||||
status = self._c.STATUS.EXECUTING
|
||||
status = Status.EXECUTING
|
||||
form = data_form.Form("form", title=D_("Delete Registration Link"))
|
||||
|
||||
link_options = [data_form.Option(link_id, link_id) for link_id in all_links]
|
||||
|
|
@ -330,17 +332,17 @@ class AdHocRegistration:
|
|||
|
||||
await self.ad_hoc_registration_data.adel(link_to_delete)
|
||||
|
||||
status = self._c.STATUS.COMPLETED
|
||||
status = Status.COMPLETED
|
||||
payload = None
|
||||
note = (
|
||||
self._c.NOTE.INFO,
|
||||
Note.INFO,
|
||||
D_("Registration link {link_id} deleted successfully.").format(
|
||||
link_id=link_to_delete
|
||||
),
|
||||
)
|
||||
except (KeyError, StopIteration, ValueError):
|
||||
raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD)
|
||||
raise self._c.ad_hoc_error(Error.BAD_PAYLOAD)
|
||||
else:
|
||||
raise self._c.AdHocError(self._c.ERROR.INTERNAL)
|
||||
raise self._c.ad_hoc_error(Error.INTERNAL)
|
||||
|
||||
return (payload, status, None, note)
|
||||
|
|
|
|||
Loading…
Reference in a new issue