Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_xep_0103.py @ 4334:111dce64dcb5
plugins XEP-0300, XEP-0446, XEP-0447, XEP0448 and others: Refactoring to use Pydantic:
Pydantic models are used more and more in Libervia, for the bridge API, and also to
convert `domish.Element` to internal representation.
Type hints have also been added in many places.
rel 453
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 03 Dec 2024 00:12:38 +0100 |
parents | 0d7bb4df2343 |
children |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0103.py Tue Dec 03 00:11:00 2024 +0100 +++ b/libervia/backend/plugins/plugin_xep_0103.py Tue Dec 03 00:12:38 2024 +0100 @@ -15,18 +15,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from typing import Dict, Any +from typing import Self +from pydantic import BaseModel, Field from twisted.words.xish import domish +from libervia.backend.core import exceptions from libervia.backend.core.constants import Const as C from libervia.backend.core.i18n import _ from libervia.backend.core.log import getLogger -from libervia.backend.core import exceptions log = getLogger(__name__) - PLUGIN_INFO = { C.PI_NAME: "URL Address Information", C.PI_IMPORT_NAME: "XEP-0103", @@ -37,45 +37,87 @@ C.PI_HANDLER: "no", C.PI_DESCRIPTION: _("""Implementation of XEP-0103 (URL Address Information)"""), } +NS_URL_DATA = "http://jabber.org/protocol/url-data" -NS_URL_DATA = "http://jabber.org/protocol/url-data" + +class Desc(BaseModel): + """ + Model for the <desc/> element. + """ + + content: str + xml_lang: str | None = None + + +class URLData(BaseModel): + """ + Model for the <url-data/> element. + """ + + url: str + desc: list[Desc] = Field(default_factory=list) + + @classmethod + def from_element(cls, element: domish.Element) -> Self: + """Create a URLData instance from a <url-data> element or its parent. + + @param url_data_elt: The <url-data> element or a parent element. + @return: URLData instance. + @raise exceptions.NotFound: If the <url-data> element is not found. + """ + if element.uri != NS_URL_DATA or element.name != "url-data": + child_url_data_elt = next(element.elements(NS_URL_DATA, "url-data"), None) + if child_url_data_elt is None: + raise exceptions.NotFound("<url-data> element not found") + else: + element = child_url_data_elt + kwargs = { + "url": element["target"], + "desc": [ + Desc(content=str(desc_elt), xml_lang=desc_elt.getAttribute("xml:lang")) + for desc_elt in element.elements(NS_URL_DATA, "desc") + ], + } + return cls(**kwargs) + + def to_element(self) -> domish.Element: + """Build the <url-data> element from this instance's data. + + @return: <url-data> element. + """ + url_data_elt = domish.Element((NS_URL_DATA, "url-data")) + url_data_elt["target"] = self.url + for desc in self.desc: + desc_elt = url_data_elt.addElement((NS_URL_DATA, "desc")) + if desc.xml_lang: + desc_elt["xml:lang"] = desc.xml_lang + desc_elt.addContent(desc.content) + return url_data_elt class XEP_0103: namespace = NS_URL_DATA def __init__(self, host): - log.info(_("XEP-0103 (URL Address Information) plugin initialization")) + log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization") host.register_namespace("url-data", NS_URL_DATA) - def get_url_data_elt(self, url: str, **kwargs) -> domish.Element: + def generate_url_data(self, url: str, **kwargs) -> URLData: """Generate the element describing the URL @param url: URL to use @param extra: extra metadata describing how to access the URL @return: ``<url-data/>`` element """ - url_data_elt = domish.Element((NS_URL_DATA, "url-data")) - url_data_elt["target"] = url - return url_data_elt + url_data = URLData(url=url, **kwargs) + return url_data - def parse_url_data_elt(self, url_data_elt: domish.Element) -> Dict[str, Any]: + def parse_url_data_elt(self, url_data_elt: domish.Element) -> URLData: """Parse <url-data/> element @param url_data_elt: <url-data/> element a parent element can also be used - @return: url-data data. It's a dict whose keys correspond to - [get_url_data_elt] parameters + @return: URLData instance @raise exceptions.NotFound: no <url-data/> element has been found """ - if url_data_elt.name != "url-data": - try: - url_data_elt = next(url_data_elt.elements(NS_URL_DATA, "url-data")) - except StopIteration: - raise exceptions.NotFound - try: - data: Dict[str, Any] = {"url": url_data_elt["target"]} - except KeyError: - raise ValueError(f'"target" attribute is missing: {url_data_elt.toXml}') - - return data + return URLData.from_element(url_data_elt)