Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0103.py @ 4284:3a550e9a2b55
black reformatting
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 14 Jul 2024 16:47:45 +0200 |
parents | 0d7bb4df2343 |
children |
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 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
51 def get_url_data_elt(self, url: str, **kwargs) -> domish.Element: |
3895
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 """Generate the element describing the URL |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 @param url: URL to use |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 @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
|
56 @return: ``<url-data/>`` element |
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 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
|
59 url_data_elt["target"] = url |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 return url_data_elt |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
62 def parse_url_data_elt(self, url_data_elt: domish.Element) -> Dict[str, Any]: |
3895
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 """Parse <url-data/> element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 @param url_data_elt: <url-data/> element |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 a parent element can also be used |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 @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
|
68 [get_url_data_elt] parameters |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 @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
|
70 """ |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 if url_data_elt.name != "url-data": |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 url_data_elt = next(url_data_elt.elements(NS_URL_DATA, "url-data")) |
3895
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 except StopIteration: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 raise exceptions.NotFound |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 try: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 data: Dict[str, Any] = {"url": url_data_elt["target"]} |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 except KeyError: |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 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
|
80 |
eb0a77bea363
plugin XEP-0103: URL Address Information implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 return data |