home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / pyxmpp / jid.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  5.9 KB  |  198 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: jid.py 714 2010-04-05 10:20:10Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import re
  7. import weakref
  8. import warnings
  9. from encodings import idna
  10. from pyxmpp.xmppstringprep import nodeprep, resourceprep
  11. from pyxmpp.exceptions import JIDError
  12. node_invalid_re = re.compile(u'["&\'/:<>@\\s\\x00-\\x19]', re.UNICODE)
  13. resource_invalid_re = re.compile(u'[\\s\\x00-\\x19]', re.UNICODE)
  14.  
  15. def are_domains_equal(a, b):
  16.     a = idna.ToASCII(a)
  17.     b = idna.ToASCII(b)
  18.     return a.lower() == b.lower()
  19.  
  20.  
  21. class JID(object):
  22.     cache = weakref.WeakValueDictionary()
  23.     __slots__ = [
  24.         'node',
  25.         'domain',
  26.         'resource',
  27.         '__weakref__']
  28.     
  29.     def __new__(cls, node_or_jid = None, domain = None, resource = None, check = True):
  30.         if isinstance(node_or_jid, JID):
  31.             return node_or_jid
  32.         if obj is None:
  33.             obj = object.__new__(cls)
  34.         
  35.         if node_or_jid:
  36.             node_or_jid = unicode(node_or_jid)
  37.         
  38.         if node_or_jid:
  39.             if u'@' in node_or_jid or u'/' in node_or_jid:
  40.                 obj._JID__from_unicode(node_or_jid)
  41.                 cls.cache[node_or_jid] = obj
  42.             elif domain is None and resource is None:
  43.                 if node_or_jid is None:
  44.                     raise JIDError, 'At least domain must be given'
  45.                 node_or_jid is None
  46.                 domain = node_or_jid
  47.                 node_or_jid = None
  48.             
  49.         if check:
  50.             obj._JID__set_node(node_or_jid)
  51.             obj._JID__set_domain(domain)
  52.             obj._JID__set_resource(resource)
  53.         else:
  54.             object.__setattr__(obj, 'node', node_or_jid)
  55.             object.__setattr__(obj, 'domain', domain)
  56.             object.__setattr__(obj, 'resource', resource)
  57.         return obj
  58.  
  59.     
  60.     def __setattr__(self, name, value):
  61.         raise RuntimeError, 'JID objects are immutable!'
  62.  
  63.     
  64.     def __from_unicode(self, s, check = True):
  65.         s1 = s.split(u'/', 1)
  66.         s2 = s1[0].split(u'@', 1)
  67.         if len(s2) == 2:
  68.             if check:
  69.                 self._JID__set_node(s2[0])
  70.                 self._JID__set_domain(s2[1])
  71.             else:
  72.                 object.__setattr__(self, 'node', s2[0])
  73.                 object.__setattr__(self, 'domain', s2[1])
  74.         elif check:
  75.             self._JID__set_domain(s2[0])
  76.         else:
  77.             object.__setattr__(self, 'domain', s2[0])
  78.         object.__setattr__(self, 'node', None)
  79.         if len(s1) == 2:
  80.             if check:
  81.                 self._JID__set_resource(s1[1])
  82.             else:
  83.                 object.__setattr__(self, 'resource', s1[1])
  84.         else:
  85.             object.__setattr__(self, 'resource', None)
  86.         if not self.domain:
  87.             raise JIDError, 'Domain is required in JID.'
  88.         self.domain
  89.  
  90.     
  91.     def __set_node(self, s):
  92.         if s:
  93.             s = unicode(s)
  94.             s = nodeprep.prepare(s)
  95.             if len(s.encode('utf-8')) > 1023:
  96.                 raise JIDError, 'Node name too long'
  97.             len(s.encode('utf-8')) > 1023
  98.         else:
  99.             s = None
  100.         object.__setattr__(self, 'node', s)
  101.  
  102.     
  103.     def __set_domain(self, s):
  104.         if s is None:
  105.             raise JIDError, 'Domain must be given'
  106.         s is None
  107.         if s:
  108.             s = unicode(s)
  109.         
  110.         s = idna.nameprep(s)
  111.         if len(s.encode('utf-8')) > 1023:
  112.             raise JIDError, 'Domain name too long'
  113.         len(s.encode('utf-8')) > 1023
  114.         object.__setattr__(self, 'domain', s)
  115.  
  116.     
  117.     def __set_resource(self, s):
  118.         if s:
  119.             s = unicode(s)
  120.             s = resourceprep.prepare(s)
  121.             if len(s.encode('utf-8')) > 1023:
  122.                 raise JIDError, 'Resource name too long'
  123.             len(s.encode('utf-8')) > 1023
  124.         else:
  125.             s = None
  126.         object.__setattr__(self, 'resource', s)
  127.  
  128.     
  129.     def __str__(self):
  130.         return self.as_utf8()
  131.  
  132.     
  133.     def __unicode__(self):
  134.         return self.as_unicode()
  135.  
  136.     
  137.     def __repr__(self):
  138.         return '<JID: %r>' % self.as_unicode()
  139.  
  140.     
  141.     def as_utf8(self):
  142.         return self.as_unicode().encode('utf-8')
  143.  
  144.     
  145.     def as_string(self):
  146.         warnings.warn('JID.as_string() is deprecated. Use unicode() or `as_utf8` instead.', DeprecationWarning, stacklevel = 1)
  147.         return self.as_utf8()
  148.  
  149.     
  150.     def as_unicode(self):
  151.         r = self.domain
  152.         if self.node:
  153.             r = self.node + u'@' + r
  154.         
  155.         if self.resource:
  156.             r = r + u'/' + self.resource
  157.         
  158.         if not JID.cache.has_key(r):
  159.             JID.cache[r] = self
  160.         
  161.         return r
  162.  
  163.     
  164.     def bare(self):
  165.         return JID(self.node, self.domain, check = False)
  166.  
  167.     
  168.     def __eq__(self, other):
  169.         if other is None:
  170.             return False
  171.         if type(other) in (str, unicode):
  172.             
  173.             try:
  174.                 other = JID(other)
  175.             return False
  176.  
  177.         elif not isinstance(other, JID):
  178.             return False
  179.         other is None
  180.         if self.node == other.node and are_domains_equal(self.domain, other.domain):
  181.             pass
  182.         return self.resource == other.resource
  183.  
  184.     
  185.     def __ne__(self, other):
  186.         return not self.__eq__(other)
  187.  
  188.     
  189.     def __cmp__(self, other):
  190.         a = self.as_unicode()
  191.         return cmp(a, other)
  192.  
  193.     
  194.     def __hash__(self):
  195.         return hash(self.node) ^ hash(self.domain) ^ hash(self.resource)
  196.  
  197.  
  198.