mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Updated and simplified new JID class to have more documentation and use PEP8 style.
This commit is contained in:
parent
fec69be731
commit
aa1dbe97e0
2 changed files with 116 additions and 64 deletions
|
@ -6,5 +6,6 @@
|
||||||
See the file LICENSE for copying permission.
|
See the file LICENSE for copying permission.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from sleekxmpp.xmlstream.jid import JID
|
||||||
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ElementBase
|
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ElementBase
|
||||||
from sleekxmpp.xmlstream.xmlstream import XMLStream, RESPONSE_TIMEOUT
|
from sleekxmpp.xmlstream.xmlstream import XMLStream, RESPONSE_TIMEOUT
|
||||||
|
|
|
@ -1,43 +1,93 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2010 Nathanael C. Fritz
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class JID(object):
|
class JID(object):
|
||||||
|
"""
|
||||||
|
A representation of a Jabber ID, or JID.
|
||||||
|
|
||||||
|
Each JID may have three components: a user, a domain, and an optional
|
||||||
|
resource. For example: user@domain/resource
|
||||||
|
|
||||||
|
When a resource is not used, the JID is called a bare JID.
|
||||||
|
The JID is a full JID otherwise.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
jid -- Alias for 'full'.
|
||||||
|
full -- The value of the full JID.
|
||||||
|
bare -- The value of the bare JID.
|
||||||
|
user -- The username portion of the JID.
|
||||||
|
domain -- The domain name portion of the JID.
|
||||||
|
server -- Alias for 'domain'.
|
||||||
|
resource -- The resource portion of the JID.
|
||||||
|
|
||||||
|
Methods:
|
||||||
|
reset -- Use a new JID value.
|
||||||
|
regenerate -- Recreate the JID from its components.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, jid):
|
def __init__(self, jid):
|
||||||
"""Initialize a new jid"""
|
"""Initialize a new JID"""
|
||||||
self.reset(jid)
|
self.reset(jid)
|
||||||
|
|
||||||
def reset(self, jid):
|
def reset(self, jid):
|
||||||
"""Start fresh from a new jid string"""
|
"""
|
||||||
|
Start fresh from a new JID string.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
jid - The new JID value.
|
||||||
|
"""
|
||||||
self._full = self._jid = str(jid)
|
self._full = self._jid = str(jid)
|
||||||
self._domain = None
|
self._domain = None
|
||||||
self._resource = None
|
self._resource = None
|
||||||
self._user = None
|
self._user = None
|
||||||
self._domain = None
|
|
||||||
self._bare = None
|
self._bare = None
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
"""Handle getting the jid values, using cache if available"""
|
"""
|
||||||
|
Handle getting the JID values, using cache if available.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
name -- One of: user, server, domain, resource,
|
||||||
|
full, or bare.
|
||||||
|
"""
|
||||||
if name == 'resource':
|
if name == 'resource':
|
||||||
if self._resource is not None: return self._resource
|
if self._resource is None:
|
||||||
self._resource = self._jid.split('/', 1)[-1]
|
self._resource = self._jid.split('/', 1)[-1]
|
||||||
return self._resource
|
return self._resource
|
||||||
elif name == 'user':
|
elif name == 'user':
|
||||||
if self._user is not None: return self._user
|
if self._user is None:
|
||||||
if '@' in self._jid:
|
if '@' in self._jid:
|
||||||
self._user = self._jid.split('@', 1)[0]
|
self._user = self._jid.split('@', 1)[0]
|
||||||
else:
|
else:
|
||||||
self._user = self._user
|
self._user = self._user
|
||||||
return self._user
|
return self._user
|
||||||
elif name in ('server', 'domain'):
|
elif name in ('server', 'domain'):
|
||||||
if self._domain is not None: return self._domain
|
if self._domain is None:
|
||||||
self._domain = self._jid.split('@', 1)[-1].split('/', 1)[0]
|
self._domain = self._jid.split('@', 1)[-1].split('/', 1)[0]
|
||||||
return self._domain
|
return self._domain
|
||||||
elif name == 'full':
|
elif name == 'full':
|
||||||
return self._jid
|
return self._jid
|
||||||
elif name == 'bare':
|
elif name == 'bare':
|
||||||
if self._bare is not None: return self._bare
|
if self._bare is None:
|
||||||
self._bare = self._jid.split('/', 1)[0]
|
self._bare = self._jid.split('/', 1)[0]
|
||||||
return self._bare
|
return self._bare
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
"""Edit a jid by updating it's individual values, resetting by a generated jid in the end"""
|
"""
|
||||||
|
Edit a JID by updating it's individual values, resetting the
|
||||||
|
generated JID in the end.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
name -- The name of the JID part. One of: user, domain,
|
||||||
|
server, resource, full, jid, or bare.
|
||||||
|
value -- The new value for the JID part.
|
||||||
|
"""
|
||||||
if name in ('resource', 'user', 'domain'):
|
if name in ('resource', 'user', 'domain'):
|
||||||
object.__setattr__(self, "_%s" % name, value)
|
object.__setattr__(self, "_%s" % name, value)
|
||||||
self.regenerate()
|
self.regenerate()
|
||||||
|
@ -56,15 +106,16 @@ class JID(object):
|
||||||
else:
|
else:
|
||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
|
|
||||||
|
|
||||||
def regenerate(self):
|
def regenerate(self):
|
||||||
"""Generate a new jid based on current values, useful after editing"""
|
"""Generate a new JID based on current values, useful after editing."""
|
||||||
jid = ""
|
jid = ""
|
||||||
if self.user: jid = "%s@" % self.user
|
if self.user:
|
||||||
|
jid = "%s@" % self.user
|
||||||
jid += self.domain
|
jid += self.domain
|
||||||
if self.resource: jid += "/%s" % self.resource
|
if self.resource:
|
||||||
|
jid += "/%s" % self.resource
|
||||||
self.reset(jid)
|
self.reset(jid)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
"""Use the full JID as the string value."""
|
||||||
return self.full
|
return self.full
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue