comparison frontends/src/tools/jid.py @ 1290:faa1129559b8 frontends_multi_profiles

core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit): /!\ not finished, everything is still instable ! - bridge: DBus bridge has been modified to allow blocking call to be called in the same way as asynchronous calls - bridge: calls with a callback and no errback are now possible, default errback log the error - constants: removed hack to manage presence without OrderedDict, as an OrderedDict like class has been implemented in Libervia - core: getLastResource has been removed and replaced by getMainResource (there is a global better management of resources) - various style improvments: use of constants when possible, fixed variable overlaps, import of module instead of direct class import - frontends: printInfo and printMessage methods in (Quick)Chat are more generic (use of extra instead of timestamp) - frontends: bridge creation and option parsing (command line arguments) are now specified by the frontend in QuickApp __init__ - frontends: ProfileManager manage a more complete plug sequence (some stuff formerly manage in contact_list have moved to ProfileManager) - quick_frontend (quick_widgets): QuickWidgetsManager is now iterable (all widgets are then returned), or can return an iterator on a specific class (return all widgets of this class) with getWidgets - frontends: tools.jid can now be used in Pyjamas, with some care - frontends (XMLUI): profile is now managed - core (memory): big improvment on entities cache management (and specially resource management) - core (params/exceptions): added PermissionError - various fixes and improvments, check diff for more details
author Goffi <goffi@goffi.org>
date Sat, 24 Jan 2015 01:00:29 +0100
parents e3a9ea76de35
children 4895e1e092fb
comparison
equal deleted inserted replaced
1289:653f2e2eea31 1290:faa1129559b8
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 20
21 class JID(unicode): 21 # hack to use this module with pyjamas
22 try:
23 unicode('') # XXX: unicode doesn't exist in pyjamas
24
25 # normal version
26 class BaseJID(unicode):
27 def __new__(cls, jid_str):
28 self = unicode.__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
48 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
49 # pyjamas version
50 class BaseJID(object):
51 def __init__(self, jid_str):
52 self.__internal_str = JID._normalize(jid_str)
53
54 def __str__(self):
55 return self.__internal_str
56
57 def __getattr__(self, name):
58 return getattr(self.__internal_str, name)
59
60 def __eq__(self, other):
61 return (self.node == other.node
62 and self.domain == other.domain
63 and self.resource == other.resource)
64
65 def __hash__(self):
66 return hash(self.__internal_str)
67
68 def find(self, *args):
69 return self.__internal_str.find(*args)
70
71 def _parse(self):
72 """Find node domain and resource"""
73 node_end = self.__internal_str.find('@')
74 if node_end < 0:
75 node_end = 0
76 domain_end = self.__internal_str.find('/')
77 if domain_end == 0:
78 raise ValueError("a jid can't start with '/'")
79 if domain_end == -1:
80 domain_end = len(self.__internal_str)
81 self.node = self.__internal_str[:node_end] or None
82 self.domain = self.__internal_str[(node_end + 1) if node_end else 0:domain_end]
83 self.resource = self.__internal_str[domain_end + 1:] or None
84
85
86 class JID(BaseJID):
22 """This class help manage JID (Node@Domaine/Resource)""" 87 """This class help manage JID (Node@Domaine/Resource)"""
23 88
24 def __new__(cls, jid): 89 def __init__(self, jid_str):
25 self = unicode.__new__(cls, cls._normalize(jid)) 90 super(JID, self).__init__(jid_str)
26 self._parse() 91 self._parse()
27 return self
28 92
29 @classmethod 93 @staticmethod
30 def _normalize(cls, jid): 94 def _normalize(jid_str):
31 """Naive normalization before instantiating and parsing the JID""" 95 """Naive normalization before instantiating and parsing the JID"""
32 if not jid: 96 if not jid_str:
33 return jid 97 return jid_str
34 tokens = jid.split('/') 98 tokens = jid_str.split('/')
35 tokens[0] = tokens[0].lower() # force node and domain to lower-case 99 tokens[0] = tokens[0].lower() # force node and domain to lower-case
36 return '/'.join(tokens) 100 return '/'.join(tokens)
37 101
38 @property 102 @property
39 def bare(self): 103 def bare(self):
40 if not self.node: 104 if not self.node:
41 return JID(self.domain) 105 return JID(self.domain)
42 return JID(u"{}@{}".format(self.node, self.domain)) 106 return JID(u"{}@{}".format(self.node, self.domain))
43 107
44 def _parse(self):
45 """Find node domain and resource"""
46 node_end = self.find('@')
47 if node_end < 0:
48 node_end = 0
49 domain_end = self.find('/')
50 if domain_end == 0:
51 raise ValueError("a jid can't start with '/'")
52 if domain_end == -1:
53 domain_end = len(self)
54 self.node = self[:node_end] or None
55 self.domain = self[(node_end + 1) if node_end else 0:domain_end]
56 self.resource = self[domain_end + 1:] or None
57
58 def is_valid(self): 108 def is_valid(self):
59 """ 109 """
60 @return: True if the JID is XMPP compliant 110 @return: True if the JID is XMPP compliant
61 """ 111 """
62 # TODO: implement real check, according to the RFC http://tools.ietf.org/html/rfc6122 112 # TODO: implement real check, according to the RFC http://tools.ietf.org/html/rfc6122