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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import struct
  6. import dns.exception as dns
  7. import dns.inet as dns
  8. import dns.rdata as dns
  9. import dns.tokenizer as dns
  10.  
  11. class APLItem(object):
  12.     __slots__ = [
  13.         'family',
  14.         'negation',
  15.         'address',
  16.         'prefix']
  17.     
  18.     def __init__(self, family, negation, address, prefix):
  19.         self.family = family
  20.         self.negation = negation
  21.         self.address = address
  22.         self.prefix = prefix
  23.  
  24.     
  25.     def __str__(self):
  26.         if self.negation:
  27.             return '!%d:%s/%s' % (self.family, self.address, self.prefix)
  28.         return '%d:%s/%s' % (self.family, self.address, self.prefix)
  29.  
  30.     
  31.     def to_wire(self, file):
  32.         if self.family == 1:
  33.             address = dns.inet.inet_pton(dns.inet.AF_INET, self.address)
  34.         elif self.family == 2:
  35.             address = dns.inet.inet_pton(dns.inet.AF_INET6, self.address)
  36.         else:
  37.             address = self.address.decode('hex_codec')
  38.         last = 0
  39.         for i in xrange(len(address) - 1, -1, -1):
  40.             if address[i] != chr(0):
  41.                 last = i + 1
  42.                 break
  43.                 continue
  44.         
  45.         address = address[0:last]
  46.         l = len(address)
  47.         if self.negation:
  48.             l |= 128
  49.         
  50.         header = struct.pack('!HBB', self.family, self.prefix, l)
  51.         file.write(header)
  52.         file.write(address)
  53.  
  54.  
  55.  
  56. class APL(dns.rdata.Rdata):
  57.     __slots__ = [
  58.         'items']
  59.     
  60.     def __init__(self, rdclass, rdtype, items):
  61.         super(APL, self).__init__(rdclass, rdtype)
  62.         self.items = items
  63.  
  64.     
  65.     def to_text(self, origin = None, relativize = True, **kw):
  66.         return ' '.join(map((lambda x: str(x)), self.items))
  67.  
  68.     
  69.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  70.         items = []
  71.         while None:
  72.             (ttype, item) = tok.get()
  73.             if ttype == dns.tokenizer.EOL or ttype == dns.tokenizer.EOF:
  74.                 break
  75.             
  76.             if item[0] == '!':
  77.                 negation = True
  78.                 item = item[1:]
  79.             else:
  80.                 negation = False
  81.             (family, rest) = item.split(':', 1)
  82.             family = int(family)
  83.             (address, prefix) = rest.split('/', 1)
  84.             prefix = int(prefix)
  85.             item = APLItem(family, negation, address, prefix)
  86.             continue
  87.             return cls(rdclass, rdtype, items)
  88.  
  89.     from_text = classmethod(from_text)
  90.     
  91.     def to_wire(self, file, compress = None, origin = None):
  92.         for item in self.items:
  93.             item.to_wire(file)
  94.         
  95.  
  96.     
  97.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  98.         items = []
  99.         while rdlen < 4:
  100.             raise dns.exception.FormError
  101.         header = struct.unpack('!HBB', wire[current:current + 4])
  102.         afdlen = header[2]
  103.         if afdlen > 127:
  104.             negation = True
  105.             afdlen -= 128
  106.         else:
  107.             negation = False
  108.         current += 4
  109.         rdlen -= 4
  110.         if rdlen < afdlen:
  111.             raise dns.exception.FormError
  112.         rdlen < afdlen
  113.         address = wire[current:current + afdlen]
  114.         l = len(address)
  115.         if header[0] == 1:
  116.             if l < 4:
  117.                 address += '\x00' * (4 - l)
  118.             
  119.             address = dns.inet.inet_ntop(dns.inet.AF_INET, address)
  120.         elif header[0] == 2:
  121.             if l < 16:
  122.                 address += '\x00' * (16 - l)
  123.             
  124.             address = dns.inet.inet_ntop(dns.inet.AF_INET6, address)
  125.         else:
  126.             address = address.encode('hex_codec')
  127.         current += afdlen
  128.         rdlen -= afdlen
  129.         item = APLItem(header[0], negation, address, header[1])
  130.         items.append(item)
  131.         if rdlen == 0:
  132.             break
  133.             continue
  134.         continue
  135.         return cls(rdclass, rdtype, items)
  136.  
  137.     from_wire = classmethod(from_wire)
  138.     
  139.     def _cmp(self, other):
  140.         f = cStringIO.StringIO()
  141.         self.to_wire(f)
  142.         wire1 = f.getvalue()
  143.         f.seek(0)
  144.         f.truncate()
  145.         other.to_wire(f)
  146.         wire2 = f.getvalue()
  147.         f.close()
  148.         return cmp(wire1, wire2)
  149.  
  150.  
  151.