comparison sat/plugins/plugin_xep_0300.py @ 3916:40d47cc29ea4

plugin XEP-0300: type hints
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 15:19:08 +0200
parents be6d91572633
children 524856bd7b19
comparison
equal deleted inserted replaced
3915:287938675461 3916:40d47cc29ea4
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from typing import Tuple
21 from sat.core.constants import Const as C 21 import base64
22 from sat.core.log import getLogger 22 from collections import OrderedDict
23 23 import hashlib
24 log = getLogger(__name__) 24
25 from sat.core import exceptions
26 from twisted.words.xish import domish
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
28 from twisted.internet import threads 25 from twisted.internet import threads
29 from twisted.internet import defer 26 from twisted.internet import defer
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
28 from twisted.words.xish import domish
29 from wokkel import disco, iwokkel
30 from zope.interface import implementer 30 from zope.interface import implementer
31 from wokkel import disco, iwokkel 31
32 from collections import OrderedDict 32 from sat.core import exceptions
33 import hashlib 33 from sat.core.constants import Const as C
34 import base64 34 from sat.core.i18n import _
35 from sat.core.log import getLogger
36
37 log = getLogger(__name__)
35 38
36 39
37 PLUGIN_INFO = { 40 PLUGIN_INFO = {
38 C.PI_NAME: "Cryptographic Hash Functions", 41 C.PI_NAME: "Cryptographic Hash Functions",
39 C.PI_IMPORT_NAME: "XEP-0300", 42 C.PI_IMPORT_NAME: "XEP-0300",
59 ("sha-1", hashlib.sha1), 62 ("sha-1", hashlib.sha1),
60 ("sha-256", hashlib.sha256), 63 ("sha-256", hashlib.sha256),
61 ("sha-512", hashlib.sha512), 64 ("sha-512", hashlib.sha512),
62 ) 65 )
63 ) 66 )
67 ALGO_DEFAULT = ALGO_DEFAULT
64 68
65 def __init__(self, host): 69 def __init__(self, host):
66 log.info(_("plugin Hashes initialization")) 70 log.info(_("plugin Hashes initialization"))
67 host.registerNamespace("hashes", NS_HASHES) 71 host.registerNamespace("hashes", NS_HASHES)
68 72
174 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8') 178 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8')
175 hash_elt.addContent(b64_hash) 179 hash_elt.addContent(b64_hash)
176 hash_elt["algo"] = algo 180 hash_elt["algo"] = algo
177 return hash_elt 181 return hash_elt
178 182
179 def parseHashElt(self, parent): 183 def parseHashElt(self, parent: domish.Element) -> Tuple[str, bytes]:
180 """Find and parse a hash element 184 """Find and parse a hash element
181 185
182 if multiple elements are found, the strongest managed one is returned 186 if multiple elements are found, the strongest managed one is returned
183 @param (domish.Element): parent of <hash/> element 187 @param parent: parent of <hash/> element
184 @return (tuple[str, bytes]): (algo, hash) tuple 188 @return: (algo, hash) tuple
185 both values can be None if <hash/> is empty 189 both values can be None if <hash/> is empty
186 @raise exceptions.NotFound: the element is not present 190 @raise exceptions.NotFound: the element is not present
187 @raise exceptions.DataError: the element is invalid 191 @raise exceptions.DataError: the element is invalid
188 """ 192 """
189 algos = list(XEP_0300.ALGOS.keys()) 193 algos = list(XEP_0300.ALGOS.keys())