Mercurial > libervia-web
comparison src/browser/sat_browser/jid.py @ 519:138336986bd0
browser_side: refactorize class JID to allow modifying its attributes
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 02 Sep 2014 21:18:10 +0200 |
parents | da690ef8019e |
children | b38629924602 |
comparison
equal
deleted
inserted
replaced
518:3d8e8f693576 | 519:138336986bd0 |
---|---|
23 | 23 |
24 class JID(object): | 24 class JID(object): |
25 """This class help manage JID (Node@Domaine/Resource)""" | 25 """This class help manage JID (Node@Domaine/Resource)""" |
26 | 26 |
27 def __init__(self, jid): | 27 def __init__(self, jid): |
28 self.__raw = str(self.__normalize(jid)) | 28 try: |
29 assert(isinstance(jid, str)) | |
30 except AssertionError: | |
31 raise AssertionError('Expected a string but got a %(class)s: %(inst)s' % | |
32 {'class': jid.__class__, 'inst': jid}) | |
33 self.__raw = str(JID.__normalize(jid)) | |
29 self.__parse() | 34 self.__parse() |
30 | 35 |
31 @classmethod | 36 @classmethod |
32 def __normalize(cls, jid): | 37 def __normalize(cls, jid): |
33 """Naive normalization before instantiating and parsing the JID""" | 38 """Naive normalization before instantiating and parsing the JID""" |
35 return jid | 40 return jid |
36 tokens = jid.split('/') | 41 tokens = jid.split('/') |
37 tokens[0] = tokens[0].lower() # force node and domain to lower-case | 42 tokens[0] = tokens[0].lower() # force node and domain to lower-case |
38 return '/'.join(tokens) | 43 return '/'.join(tokens) |
39 | 44 |
45 # XXX: I defined node, domain and resource as properties here, and renamed | |
46 # the class attributes with the '__' prefix, but it appeared that the setter | |
47 # methods were not called! Maybe pyjamas doesn't handle them well. | |
48 def setNode(self, node): | |
49 self.node = node | |
50 self.__build() | |
51 | |
52 def setDomain(self, domain): | |
53 self.domain = domain | |
54 self.__build() | |
55 | |
56 def setResource(self, resource): | |
57 self.resource = resource | |
58 self.__build() | |
59 | |
60 @property | |
61 def bare(self): | |
62 return self.domain if not self.node else (self.node + "@" + self.domain) | |
63 | |
64 @bare.setter | |
65 def bare(self, bare): | |
66 self.__parseBare(bare) | |
67 self.build() | |
68 | |
69 def __build(self): | |
70 """Build the JID string from the node, domain and resource""" | |
71 self.__raw = self.bare if not self.resource else (self.bare + '/' + self.resource) | |
72 | |
40 def __parse(self): | 73 def __parse(self): |
41 """Find node domain and resource""" | 74 """Parse the JID string to extract the node, domain and resource""" |
42 node_end = self.__raw.find('@') | 75 tokens = self.__raw.split('/') |
43 if node_end < 0: | 76 bare, self.resource = (tokens[0], tokens[1]) if len(tokens) > 1 else (self.__raw, '') |
44 node_end = 0 | 77 self.__parseBare(bare) |
45 domain_end = self.__raw.find('/') | 78 |
46 if domain_end < 1: | 79 def __parseBare(self, bare): |
47 domain_end = len(self.__raw) | 80 """Parse the given JID bare to extract the node and domain |
48 self.node = self.__raw[:node_end] | 81 |
49 self.domain = self.__raw[(node_end + 1) if node_end else 0:domain_end] | 82 @param bare (str): JID bare to parse |
50 self.resource = self.__raw[domain_end + 1:] | 83 """ |
51 if not node_end: | 84 tokens = bare.split('@') |
52 self.bare = self.__raw | 85 self.node, self.domain = (tokens[0], tokens[1]) if len(tokens) > 1 else ('', bare) |
53 else: | |
54 self.bare = self.node + '@' + self.domain | |
55 | 86 |
56 def __str__(self): | 87 def __str__(self): |
57 return self.__raw.__str__() | 88 try: |
89 return self.__raw | |
90 except AttributeError: | |
91 raise AttributeError("Trying to output a JID which has not been parsed yet") | |
58 | 92 |
59 def is_valid(self): | 93 def is_valid(self): |
60 """ | 94 """ |
61 @return: True if the JID is XMPP compliant | 95 @return: True if the JID is XMPP compliant |
62 """ | 96 """ |
63 #FIXME: always return True for the moment | |
64 return self.domain != '' | 97 return self.domain != '' |
65 | 98 |
66 def __eq__(self, other): | 99 def __eq__(self, other): |
67 """Redefine equality operator to implement the naturally expected test""" | 100 """Redefine equality operator to implement the naturally expected behavior""" |
68 return self.node == other.node and self.domain == other.domain and self.resource == other.resource | 101 return self.__raw == other.__raw |
102 | |
103 def __hash__(self): | |
104 """Redefine hash to implement the naturally expected behavior""" | |
105 return hash(self.__raw) | |
106 | |
107 def full(self): | |
108 return str(self) |