home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1679 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.7 KB  |  107 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import socket
  5. import struct
  6. import dns.ipv4 as dns
  7. import dns.rdata as dns
  8. _proto_tcp = socket.getprotobyname('tcp')
  9. _proto_udp = socket.getprotobyname('udp')
  10.  
  11. class WKS(dns.rdata.Rdata):
  12.     __slots__ = [
  13.         'address',
  14.         'protocol',
  15.         'bitmap']
  16.     
  17.     def __init__(self, rdclass, rdtype, address, protocol, bitmap):
  18.         super(WKS, self).__init__(rdclass, rdtype)
  19.         self.address = address
  20.         self.protocol = protocol
  21.         self.bitmap = bitmap
  22.  
  23.     
  24.     def to_text(self, origin = None, relativize = True, **kw):
  25.         bits = []
  26.         for i in xrange(0, len(self.bitmap)):
  27.             byte = ord(self.bitmap[i])
  28.             for j in xrange(0, 8):
  29.                 if byte & 128 >> j:
  30.                     bits.append(str(i * 8 + j))
  31.                     continue
  32.             
  33.         
  34.         text = ' '.join(bits)
  35.         return '%s %d %s' % (self.address, self.protocol, text)
  36.  
  37.     
  38.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  39.         address = tok.get_string()
  40.         protocol = tok.get_string()
  41.         if protocol.isdigit():
  42.             protocol = int(protocol)
  43.         else:
  44.             protocol = socket.getprotobyname(protocol)
  45.         bitmap = []
  46.         while None:
  47.             (ttype, value) = tok.get()
  48.             if ttype == dns.tokenizer.EOL or ttype == dns.tokenizer.EOF:
  49.                 break
  50.             
  51.             if value.isdigit():
  52.                 serv = int(value)
  53.             elif protocol != _proto_udp and protocol != _proto_tcp:
  54.                 raise NotImplementedError, 'protocol must be TCP or UDP'
  55.             
  56.             if protocol == _proto_udp:
  57.                 protocol_text = 'udp'
  58.             else:
  59.                 protocol_text = 'tcp'
  60.             serv = socket.getservbyname(value, protocol_text)
  61.             i = serv // 8
  62.             l = len(bitmap)
  63.             if l < i + 1:
  64.                 for j in xrange(l, i + 1):
  65.                     bitmap.append('\x00')
  66.                 
  67.             
  68.             bitmap[i] = chr(ord(bitmap[i]) | 128 >> serv % 8)
  69.             continue
  70.             bitmap = dns.rdata._truncate_bitmap(bitmap)
  71.             return cls(rdclass, rdtype, address, protocol, bitmap)
  72.  
  73.     from_text = classmethod(from_text)
  74.     
  75.     def to_wire(self, file, compress = None, origin = None):
  76.         file.write(dns.ipv4.inet_aton(self.address))
  77.         protocol = struct.pack('!B', self.protocol)
  78.         file.write(protocol)
  79.         file.write(self.bitmap)
  80.  
  81.     
  82.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  83.         address = dns.ipv4.inet_ntoa(wire[current:current + 4])
  84.         (protocol,) = struct.unpack('!B', wire[current + 4:current + 5])
  85.         current += 5
  86.         rdlen -= 5
  87.         bitmap = wire[current:current + rdlen]
  88.         return cls(rdclass, rdtype, address, protocol, bitmap)
  89.  
  90.     from_wire = classmethod(from_wire)
  91.     
  92.     def _cmp(self, other):
  93.         sa = dns.ipv4.inet_aton(self.address)
  94.         oa = dns.ipv4.inet_aton(other.address)
  95.         v = cmp(sa, oa)
  96.         if v == 0:
  97.             sp = struct.pack('!B', self.protocol)
  98.             op = struct.pack('!B', other.protocol)
  99.             v = cmp(sp, op)
  100.             if v == 0:
  101.                 v = cmp(self.bitmap, other.bitmap)
  102.             
  103.         
  104.         return v
  105.  
  106.  
  107.