annotate libervia/backend/plugins/plugin_xep_0103.py @ 4258:ba28ca268f4a

plugin text syntaxes: fix empty string handling in `_remove_markups`.
author Goffi <goffi@goffi.org>
date Wed, 05 Jun 2024 22:33:37 +0200
parents 4b842c1fb686
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3895
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # it under the terms of the GNU Affero General Public License as published by
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # (at your option) any later version.
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # GNU Affero General Public License for more details.
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # You should have received a copy of the GNU Affero General Public License
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 from typing import Dict, Any
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from twisted.words.xish import domish
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core import exceptions
3895
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 log = getLogger(__name__)
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_NAME: "URL Address Information",
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_IMPORT_NAME: "XEP-0103",
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_TYPE: "XEP",
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_MODES: C.PLUG_MODE_BOTH,
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_PROTOCOLS: ["XEP-0103"],
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "XEP_0103",
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _("""Implementation of XEP-0103 (URL Address Information)"""),
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 NS_URL_DATA = "http://jabber.org/protocol/url-data"
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class XEP_0103:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 namespace = NS_URL_DATA
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self, host):
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 log.info(_("XEP-0103 (URL Address Information) plugin initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3895
diff changeset
49 host.register_namespace("url-data", NS_URL_DATA)
3895
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def get_url_data_elt(
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self,
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 url: str,
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 **kwargs
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 ) -> domish.Element:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 """Generate the element describing the URL
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 @param url: URL to use
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 @param extra: extra metadata describing how to access the URL
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 @return: ``<url-data/>`` element
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 url_data_elt = domish.Element((NS_URL_DATA, "url-data"))
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 url_data_elt["target"] = url
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return url_data_elt
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def parse_url_data_elt(
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self,
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 url_data_elt: domish.Element
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 ) -> Dict[str, Any]:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 """Parse <url-data/> element
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param url_data_elt: <url-data/> element
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 a parent element can also be used
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @return: url-data data. It's a dict whose keys correspond to
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 [get_url_data_elt] parameters
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @raise exceptions.NotFound: no <url-data/> element has been found
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 """
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if url_data_elt.name != "url-data":
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 try:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 url_data_elt = next(
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 url_data_elt.elements(NS_URL_DATA, "url-data")
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 )
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except StopIteration:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 raise exceptions.NotFound
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 try:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 data: Dict[str, Any] = {"url": url_data_elt["target"]}
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 except KeyError:
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 raise ValueError(f'"target" attribute is missing: {url_data_elt.toXml}')
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
eb0a77bea363 plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return data