mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
updated and moved jid class -- jids now have setters
This commit is contained in:
parent
241aba8c76
commit
2cb82afc2c
2 changed files with 96 additions and 0 deletions
70
sleekxmpp/xmlstream/jid.py
Normal file
70
sleekxmpp/xmlstream/jid.py
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
class JID(object):
|
||||||
|
def __init__(self, jid):
|
||||||
|
"""Initialize a new jid"""
|
||||||
|
self.reset(jid)
|
||||||
|
|
||||||
|
def reset(self, jid):
|
||||||
|
"""Start fresh from a new jid string"""
|
||||||
|
self._full = self._jid = str(jid)
|
||||||
|
self._domain = None
|
||||||
|
self._resource = None
|
||||||
|
self._user = None
|
||||||
|
self._domain = None
|
||||||
|
self._bare = None
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
"""Handle getting the jid values, using cache if available"""
|
||||||
|
if name == 'resource':
|
||||||
|
if self._resource is not None: return self._resource
|
||||||
|
self._resource = self._jid.split('/', 1)[-1]
|
||||||
|
return self._resource
|
||||||
|
elif name == 'user':
|
||||||
|
if self._user is not None: return self._user
|
||||||
|
if '@' in self._jid:
|
||||||
|
self._user = self._jid.split('@', 1)[0]
|
||||||
|
else:
|
||||||
|
self._user = self._user
|
||||||
|
return self._user
|
||||||
|
elif name in ('server', 'domain'):
|
||||||
|
if self._domain is not None: return self._domain
|
||||||
|
self._domain = self._jid.split('@', 1)[-1].split('/', 1)[0]
|
||||||
|
return self._domain
|
||||||
|
elif name == 'full':
|
||||||
|
return self._jid
|
||||||
|
elif name == 'bare':
|
||||||
|
if self._bare is not None: return self._bare
|
||||||
|
self._bare = self._jid.split('/', 1)[0]
|
||||||
|
return self._bare
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
"""Edit a jid by updating it's individual values, resetting by a generated jid in the end"""
|
||||||
|
if name in ('resource', 'user', 'domain'):
|
||||||
|
object.__setattr__(self, "_%s" % name, value)
|
||||||
|
self.regenerate()
|
||||||
|
elif name == 'server':
|
||||||
|
self.domain = value
|
||||||
|
elif name in ('full', 'jid'):
|
||||||
|
self.reset(value)
|
||||||
|
elif name == 'bare':
|
||||||
|
if '@' in value:
|
||||||
|
u, d = value.split('@', 1)
|
||||||
|
object.__setattr__(self, "_user", u)
|
||||||
|
object.__setattr__(self, "_domain", d)
|
||||||
|
else:
|
||||||
|
object.__setattr__(self, "_domain", value)
|
||||||
|
self.regenerate()
|
||||||
|
else:
|
||||||
|
object.__setattr__(self, name, value)
|
||||||
|
|
||||||
|
|
||||||
|
def regenerate(self):
|
||||||
|
"""Generate a new jid based on current values, useful after editing"""
|
||||||
|
jid = ""
|
||||||
|
if self.user: jid = "%s@" % self.user
|
||||||
|
jid += self.domain
|
||||||
|
if self.resource: jid += "/%s" % self.resource
|
||||||
|
self.reset(jid)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.full
|
||||||
|
|
26
tests/test_jid.py
Normal file
26
tests/test_jid.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from sleektest import *
|
||||||
|
from sleekxmpp.xmlstream.jid import JID
|
||||||
|
|
||||||
|
class TestJIDClass(SleekTest):
|
||||||
|
def testJIDfromfull(self):
|
||||||
|
j = JID('user@someserver/some/resource')
|
||||||
|
self.assertEqual(j.user, 'user', "User does not match")
|
||||||
|
self.assertEqual(j.domain, 'someserver', "Domain does not match")
|
||||||
|
self.assertEqual(j.resource, 'some/resource', "Resource does not match")
|
||||||
|
self.assertEqual(j.bare, 'user@someserver', "Bare does not match")
|
||||||
|
self.assertEqual(j.full, 'user@someserver/some/resource', "Full does not match")
|
||||||
|
self.assertEqual(str(j), 'user@someserver/some/resource', "String does not match")
|
||||||
|
|
||||||
|
def testJIDchange(self):
|
||||||
|
j = JID('user1@someserver1/some1/resource1')
|
||||||
|
j.user = 'user'
|
||||||
|
j.domain = 'someserver'
|
||||||
|
j.resource = 'some/resource'
|
||||||
|
self.assertEqual(j.user, 'user', "User does not match")
|
||||||
|
self.assertEqual(j.domain, 'someserver', "Domain does not match")
|
||||||
|
self.assertEqual(j.resource, 'some/resource', "Resource does not match")
|
||||||
|
self.assertEqual(j.bare, 'user@someserver', "Bare does not match")
|
||||||
|
self.assertEqual(j.full, 'user@someserver/some/resource', "Full does not match")
|
||||||
|
self.assertEqual(str(j), 'user@someserver/some/resource', "String does not match")
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestJIDClass)
|
Loading…
Reference in a new issue