comparison sat_frontends/tools/jid.py @ 4050:199473ffe4ea

frontends (tools/jid): remove old pyjamas code + type hints
author Goffi <goffi@goffi.org>
date Mon, 15 May 2023 17:19:54 +0200
parents 524856bd7b19
children
comparison
equal deleted inserted replaced
4049:b56bf0c6c064 4050:199473ffe4ea
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 3 # Libervia XMPP
4 # SAT: a jabber client 4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 5
7 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by 7 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or 8 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version. 9 # (at your option) any later version.
15 # GNU Affero General Public License for more details. 14 # GNU Affero General Public License for more details.
16 15
17 # You should have received a copy of the GNU Affero General Public License 16 # 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/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 18
20 19 from typing import Optional, Tuple
21 # hack to use this module with pyjamas
22 try:
23 str("") # XXX: unicode doesn't exist in pyjamas
24
25 # normal version
26 class BaseJID(str):
27 def __new__(cls, jid_str):
28 self = str.__new__(cls, cls._normalize(jid_str))
29 return self
30
31 def __init__(self, jid_str):
32 pass
33
34 def _parse(self):
35 """Find node domain and resource"""
36 node_end = self.find("@")
37 if node_end < 0:
38 node_end = 0
39 domain_end = self.find("/")
40 if domain_end == 0:
41 raise ValueError("a jid can't start with '/'")
42 if domain_end == -1:
43 domain_end = len(self)
44 self.node = self[:node_end] or None
45 self.domain = self[(node_end + 1) if node_end else 0 : domain_end]
46 self.resource = self[domain_end + 1 :] or None
47 20
48 21
49 except ( 22 class JID(str):
50 TypeError, 23 """This class helps manage JID (<local>@<domain>/<resource>)"""
51 AttributeError,
52 ): # Error raised is not the same depending on pyjsbuild options
53 24
54 # pyjamas version 25 def __new__(cls, jid_str: str) -> "JID":
55 class BaseJID(object): 26 return str.__new__(cls, cls._normalize(jid_str))
56 def __init__(self, jid_str):
57 self.__internal_str = JID._normalize(jid_str)
58 27
59 def __str__(self): 28 def __init__(self, jid_str: str):
60 return self.__internal_str 29 self._node, self._domain, self._resource = self._parse()
61
62 def __getattr__(self, name):
63 return getattr(self.__internal_str, name)
64
65 def __eq__(self, other):
66 if not isinstance(other, JID):
67 return False
68 return (
69 self.node == other.node
70 and self.domain == other.domain
71 and self.resource == other.resource
72 )
73
74 def __hash__(self):
75 return hash("JID<{}>".format(self.__internal_str))
76
77 def find(self, *args):
78 return self.__internal_str.find(*args)
79
80 def _parse(self):
81 """Find node domain and resource"""
82 node_end = self.__internal_str.find("@")
83 if node_end < 0:
84 node_end = 0
85 domain_end = self.__internal_str.find("/")
86 if domain_end == 0:
87 raise ValueError("a jid can't start with '/'")
88 if domain_end == -1:
89 domain_end = len(self.__internal_str)
90 self.node = self.__internal_str[:node_end] or None
91 self.domain = self.__internal_str[
92 (node_end + 1) if node_end else 0 : domain_end
93 ]
94 self.resource = self.__internal_str[domain_end + 1 :] or None
95
96
97 class JID(BaseJID):
98 """This class help manage JID (Node@Domaine/Resource)"""
99
100 def __init__(self, jid_str):
101 super(JID, self).__init__(jid_str)
102 self._parse()
103 30
104 @staticmethod 31 @staticmethod
105 def _normalize(jid_str): 32 def _normalize(jid_str: str) -> str:
106 """Naive normalization before instantiating and parsing the JID""" 33 """Naive normalization before instantiating and parsing the JID"""
107 if not jid_str: 34 if not jid_str:
108 return jid_str 35 return jid_str
109 tokens = jid_str.split("/") 36 tokens = jid_str.split("/")
110 tokens[0] = tokens[0].lower() # force node and domain to lower-case 37 tokens[0] = tokens[0].lower() # force node and domain to lower-case
111 return "/".join(tokens) 38 return "/".join(tokens)
112 39
40 def _parse(self) -> Tuple[Optional[str], str, Optional[str]]:
41 """Find node, domain, and resource from JID"""
42 node_end = self.find("@")
43 if node_end < 0:
44 node_end = 0
45 domain_end = self.find("/")
46 if domain_end == 0:
47 raise ValueError("a jid can't start with '/'")
48 if domain_end == -1:
49 domain_end = len(self)
50 node = self[:node_end] or None
51 domain = self[(node_end + 1) if node_end else 0 : domain_end]
52 resource = self[domain_end + 1 :] or None
53 return node, domain, resource
54
113 @property 55 @property
114 def bare(self): 56 def node(self) -> Optional[str]:
57 return self._node
58
59 @property
60 def local(self) -> Optional[str]:
61 return self._node
62
63 @property
64 def domain(self) -> str:
65 return self._domain
66
67 @property
68 def resource(self) -> Optional[str]:
69 return self._resource
70
71 @property
72 def bare(self) -> "JID":
115 if not self.node: 73 if not self.node:
116 return JID(self.domain) 74 return JID(self.domain)
117 return JID("{}@{}".format(self.node, self.domain)) 75 return JID(f"{self.node}@{self.domain}")
118 76
119 def is_valid(self): 77 def change_resource(self, resource: str) -> "JID":
78 """Build a new JID with the same node and domain but a different resource.
79
80 @param resource: The new resource for the JID.
81 @return: A new JID instance with the updated resource.
82 """
83 return JID(f"{self.bare}/{resource}")
84
85 def is_valid(self) -> bool:
120 """ 86 """
121 @return: True if the JID is XMPP compliant 87 @return: True if the JID is XMPP compliant
122 """ 88 """
123 # TODO: implement real check, according to the RFC http://tools.ietf.org/html/rfc6122 89 # Simple check for domain part
124 return self.domain != "" 90 if not self.domain or self.domain.startswith(".") or self.domain.endswith("."):
91 return False
92 if ".." in self.domain:
93 return False
94 return True
125 95
126 96
127 def new_resource(entity, resource): 97 def new_resource(entity: JID, resource: str) -> JID:
128 """Build a new JID from the given entity and resource. 98 """Build a new JID from the given entity and resource.
129 99
130 @param entity (JID): original JID 100 @param entity: original JID
131 @param resource (unicode): new resource 101 @param resource: new resource
132 @return: a new JID instance 102 @return: a new JID instance
133 """ 103 """
134 return JID("%s/%s" % (entity.bare, resource)) 104 return entity.change_resource(resource)