comparison sat_frontends/tools/jid.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
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 20
21 # hack to use this module with pyjamas 21 # hack to use this module with pyjamas
22 try: 22 try:
23 unicode('') # XXX: unicode doesn't exist in pyjamas 23 unicode("") # XXX: unicode doesn't exist in pyjamas
24 24
25 # normal version 25 # normal version
26 class BaseJID(unicode): 26 class BaseJID(unicode):
27 def __new__(cls, jid_str): 27 def __new__(cls, jid_str):
28 self = unicode.__new__(cls, cls._normalize(jid_str)) 28 self = unicode.__new__(cls, cls._normalize(jid_str))
31 def __init__(self, jid_str): 31 def __init__(self, jid_str):
32 pass 32 pass
33 33
34 def _parse(self): 34 def _parse(self):
35 """Find node domain and resource""" 35 """Find node domain and resource"""
36 node_end = self.find('@') 36 node_end = self.find("@")
37 if node_end < 0: 37 if node_end < 0:
38 node_end = 0 38 node_end = 0
39 domain_end = self.find('/') 39 domain_end = self.find("/")
40 if domain_end == 0: 40 if domain_end == 0:
41 raise ValueError("a jid can't start with '/'") 41 raise ValueError("a jid can't start with '/'")
42 if domain_end == -1: 42 if domain_end == -1:
43 domain_end = len(self) 43 domain_end = len(self)
44 self.node = self[:node_end] or None 44 self.node = self[:node_end] or None
45 self.domain = self[(node_end + 1) if node_end else 0:domain_end] 45 self.domain = self[(node_end + 1) if node_end else 0 : domain_end]
46 self.resource = self[domain_end + 1:] or None 46 self.resource = self[domain_end + 1 :] or None
47 47
48 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options 48
49 except (
50 TypeError,
51 AttributeError,
52 ): # Error raised is not the same depending on pyjsbuild options
49 53
50 # pyjamas version 54 # pyjamas version
51 class BaseJID(object): 55 class BaseJID(object):
52 def __init__(self, jid_str): 56 def __init__(self, jid_str):
53 self.__internal_str = JID._normalize(jid_str) 57 self.__internal_str = JID._normalize(jid_str)
59 return getattr(self.__internal_str, name) 63 return getattr(self.__internal_str, name)
60 64
61 def __eq__(self, other): 65 def __eq__(self, other):
62 if not isinstance(other, JID): 66 if not isinstance(other, JID):
63 return False 67 return False
64 return (self.node == other.node 68 return (
65 and self.domain == other.domain 69 self.node == other.node
66 and self.resource == other.resource) 70 and self.domain == other.domain
71 and self.resource == other.resource
72 )
67 73
68 def __hash__(self): 74 def __hash__(self):
69 return hash('JID<{}>'.format(self.__internal_str)) 75 return hash("JID<{}>".format(self.__internal_str))
70 76
71 def find(self, *args): 77 def find(self, *args):
72 return self.__internal_str.find(*args) 78 return self.__internal_str.find(*args)
73 79
74 def _parse(self): 80 def _parse(self):
75 """Find node domain and resource""" 81 """Find node domain and resource"""
76 node_end = self.__internal_str.find('@') 82 node_end = self.__internal_str.find("@")
77 if node_end < 0: 83 if node_end < 0:
78 node_end = 0 84 node_end = 0
79 domain_end = self.__internal_str.find('/') 85 domain_end = self.__internal_str.find("/")
80 if domain_end == 0: 86 if domain_end == 0:
81 raise ValueError("a jid can't start with '/'") 87 raise ValueError("a jid can't start with '/'")
82 if domain_end == -1: 88 if domain_end == -1:
83 domain_end = len(self.__internal_str) 89 domain_end = len(self.__internal_str)
84 self.node = self.__internal_str[:node_end] or None 90 self.node = self.__internal_str[:node_end] or None
85 self.domain = self.__internal_str[(node_end + 1) if node_end else 0:domain_end] 91 self.domain = self.__internal_str[
86 self.resource = self.__internal_str[domain_end + 1:] or None 92 (node_end + 1) if node_end else 0 : domain_end
93 ]
94 self.resource = self.__internal_str[domain_end + 1 :] or None
87 95
88 96
89 class JID(BaseJID): 97 class JID(BaseJID):
90 """This class help manage JID (Node@Domaine/Resource)""" 98 """This class help manage JID (Node@Domaine/Resource)"""
91 99
96 @staticmethod 104 @staticmethod
97 def _normalize(jid_str): 105 def _normalize(jid_str):
98 """Naive normalization before instantiating and parsing the JID""" 106 """Naive normalization before instantiating and parsing the JID"""
99 if not jid_str: 107 if not jid_str:
100 return jid_str 108 return jid_str
101 tokens = jid_str.split('/') 109 tokens = jid_str.split("/")
102 tokens[0] = tokens[0].lower() # force node and domain to lower-case 110 tokens[0] = tokens[0].lower() # force node and domain to lower-case
103 return '/'.join(tokens) 111 return "/".join(tokens)
104 112
105 @property 113 @property
106 def bare(self): 114 def bare(self):
107 if not self.node: 115 if not self.node:
108 return JID(self.domain) 116 return JID(self.domain)