home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / jabber / objects / bytestreams.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  7.3 KB  |  223 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from pprint import pprint
  5. from pyxmpp.utils import from_utf8
  6. from pyxmpp.utils import to_utf8
  7. import libxml2
  8. from pyxmpp.jid import JID
  9. from pyxmpp.jabber.disco import DiscoCacheFetcherBase
  10. from pyxmpp.objects import StanzaPayloadObject
  11. from pyxmpp.xmlextra import common_doc, get_node_ns_uri
  12. BYTESTREAMS_NS = 'http://jabber.org/protocol/bytestreams'
  13. BYTESTREAMS_UDP_NS = BYTESTREAMS_NS + '#udp'
  14.  
  15. class StreamHost(StanzaPayloadObject):
  16.     xml_element_name = 'streamhost'
  17.     xml_element_namespace = BYTESTREAMS_NS
  18.     
  19.     def __init__(self, xmlnode_or_jid, host = None, port = None, zeroconf = None):
  20.         if isinstance(xmlnode_or_jid, libxml2.xmlNode):
  21.             self.from_xml(xmlnode_or_jid)
  22.         else:
  23.             self.jid = JID(xmlnode_or_jid)
  24.             self.host = host
  25.             self.port = port
  26.             self.zeroconf = zeroconf
  27.         if not bool(self.port) ^ bool(self.zeroconf):
  28.             raise ValueError, 'StreamHost element requires one of [port, zeroconf]'
  29.         bool(self.port) ^ bool(self.zeroconf)
  30.  
  31.     
  32.     def from_xml(self, node):
  33.         if node.type != 'element':
  34.             raise ValueError, 'XML node is not a streamhost (not en element)'
  35.         node.type != 'element'
  36.         ns = get_node_ns_uri(node)
  37.         if ns or ns != self.xml_element_namespace or node.name != self.xml_element_name:
  38.             raise ValueError, 'XML node is not a %s descriptor' % self.xml_element_name
  39.         node.name != self.xml_element_name
  40.         jid = JID(node.prop('jid').decode('utf-8'))
  41.         self.jid = jid
  42.         host = node.prop('host').decode('utf-8')
  43.         self.host = host
  44.         port = node.prop('port')
  45.         self.port = None if port else None
  46.         zeroconf = node.prop('zeroconf')
  47.         self.zeroconf = None if zeroconf else None
  48.  
  49.     
  50.     def __eq__(self, other):
  51.         if other is self:
  52.             return True
  53.         if not isinstance(other, self.__class__):
  54.             return False
  55.         if self.jid == other.jid and self.host == other.host and self.port == other.port:
  56.             pass
  57.         return self.zeroconf == other.zeroconf
  58.  
  59.     
  60.     def __ne__(self, other):
  61.         return not self.__eq__(other)
  62.  
  63.     
  64.     def __hash__(self):
  65.         return hash((self.jid, self.host, self.port, self.zeroconf))
  66.  
  67.     
  68.     def complete_xml_element(self, xmlnode, _unused):
  69.         xmlnode.setProp('jid', JID(self.jid).as_utf8())
  70.         xmlnode.setProp('host', unicode(self.host).encode('utf-8'))
  71.         if self.port:
  72.             xmlnode.setProp('port', unicode(self.port).encode('utf-8'))
  73.         
  74.         if self.zeroconf:
  75.             xmlnode.setProp('zeroconf', unicode(self.zeroconf).encode('utf-8'))
  76.         
  77.  
  78.     
  79.     def __str__(self):
  80.         n = self.as_xml(doc = common_doc)
  81.         r = n.serialize()
  82.         n.unlinkNode()
  83.         n.freeNode()
  84.         return r
  85.  
  86.  
  87.  
  88. class ByteStreams(StanzaPayloadObject):
  89.     xml_element_name = 'query'
  90.     xml_element_namespace = BYTESTREAMS_NS
  91.     
  92.     def __init__(self, xmlnode_or_node = None, sid = None, mode = None):
  93.         self.hosts = []
  94.         self.sid = sid
  95.         self.mode = mode
  96.         self.activate = None
  97.         self.host_used = None
  98.         if isinstance(xmlnode_or_node, libxml2.xmlNode):
  99.             self.from_xml(xmlnode_or_node)
  100.         else:
  101.             self.node = xmlnode_or_node
  102.  
  103.     
  104.     def from_xml(self, node, strict = True):
  105.         node_ = node.prop('node')
  106.         sid = node.prop('sid')
  107.         self.sid = None if sid else None
  108.         mode = node.prop('mode')
  109.         self.mode = None if mode else None
  110.         self.mode = None if self.mode != 'tcp' else None
  111.         if node_:
  112.             self.node = node_.decode('utf-8')
  113.         else:
  114.             self.node = None
  115.         if node.type != 'element':
  116.             raise ValueError, 'XML node is not a bytestreams (not en element)'
  117.         node.type != 'element'
  118.         ns = get_node_ns_uri(node)
  119.         if ns or ns != BYTESTREAMS_NS or node.name != 'query':
  120.             raise ValueError, 'XML node is not a bytestreams query'
  121.         node.name != 'query'
  122.         n = node.children
  123.         while n:
  124.             if n.type != 'element':
  125.                 n = n.next
  126.                 continue
  127.             
  128.             ns = get_node_ns_uri(n)
  129.             if ns and ns != BYTESTREAMS_NS:
  130.                 n = n.next
  131.                 continue
  132.             
  133.             if n.name == 'streamhost':
  134.                 
  135.                 try:
  136.                     self.add_host(n)
  137.                 except ValueError:
  138.                     if strict:
  139.                         raise 
  140.                     strict
  141.                 except:
  142.                     None<EXCEPTION MATCH>ValueError
  143.                 
  144.  
  145.             None<EXCEPTION MATCH>ValueError
  146.             if n.name == 'streamhost-used':
  147.                 host_used = JID(n.prop('jid').decode('utf-8'))
  148.                 self.host_used = host_used
  149.             elif n.name == 'activate':
  150.                 activate = JID(n.getContent())
  151.                 self.activate = activate
  152.             
  153.             n = n.next
  154.  
  155.     
  156.     def complete_xml_element(self, xmlnode, _unused):
  157.         if self.node:
  158.             xmlnode.setProp('node', to_utf8(self.node))
  159.         
  160.         if self.sid:
  161.             xmlnode.setProp('sid', to_utf8(self.sid))
  162.         
  163.         if self.mode and self.mode != 'tcp':
  164.             xmlnode.setProp('mode', to_utf8(self.mode))
  165.         
  166.         for host in self.hosts:
  167.             
  168.             try:
  169.                 host.as_xml(xmlnode, _unused)
  170.             continue
  171.             pprint(host)
  172.             raise 
  173.             continue
  174.  
  175.         
  176.         if self.activate:
  177.             xmlnode.newChild(None, 'activate', JID(self.activate).as_utf8())
  178.         
  179.         if self.host_used:
  180.             h = xmlnode.newChild(None, 'streamhost-used', None)
  181.             h.setProp('jid', JID(self.host_used).as_utf8())
  182.         
  183.  
  184.     
  185.     def __str__(self):
  186.         n = self.as_xml(doc = common_doc)
  187.         r = n.serialize()
  188.         n.unlinkNode()
  189.         n.freeNode()
  190.         return r
  191.  
  192.     
  193.     def add_host(self, streamhost_jid, host = None, port = None, zeroconf = None):
  194.         sh = StreamHost(streamhost_jid, host, port, zeroconf)
  195.         self.hosts.append(sh)
  196.         return sh
  197.  
  198.  
  199.  
  200. def register_streamhost_cache_fetchers(cache_suite, stream):
  201.     tmp = stream
  202.     
  203.     class ByteStreamsCacheFetcher((DiscoCacheFetcherBase,)):
  204.         stream = tmp
  205.         disco_class = ByteStreams
  206.         __response = DiscoCacheFetcherBase._DiscoCacheFetcherBase__response
  207.         __error = DiscoCacheFetcherBase._DiscoCacheFetcherBase__error
  208.         __timeout = DiscoCacheFetcherBase._DiscoCacheFetcherBase__timeout
  209.         
  210.         def fetch(self):
  211.             Iq = Iq
  212.             import pyxmpp.iq
  213.             (jid, node) = self.address
  214.             iq = Iq(to_jid = jid, stanza_type = 'get')
  215.             disco = self.disco_class(node)
  216.             disco.as_xml(iq.xmlnode)
  217.             self.stream.set_response_handlers(iq, self._ByteStreamsCacheFetcher__response, self._ByteStreamsCacheFetcher__error, self._ByteStreamsCacheFetcher__timeout)
  218.             self.stream.send(iq)
  219.  
  220.  
  221.     cache_suite.register_fetcher(ByteStreams, ByteStreamsCacheFetcher)
  222.  
  223.