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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import dns.exception as dns
  5. import dns.rdataclass as dns
  6. import dns.rdatatype as dns
  7. import dns.tokenizer as dns
  8. _hex_chunksize = 32
  9.  
  10. def _hexify(data, chunksize = None):
  11.     if chunksize is None:
  12.         chunksize = _hex_chunksize
  13.     
  14.     hex = data.encode('hex_codec')
  15.     l = len(hex)
  16.     if l > chunksize:
  17.         chunks = []
  18.         i = 0
  19.         while i < l:
  20.             chunks.append(hex[i:i + chunksize])
  21.             i += chunksize
  22.         hex = ' '.join(chunks)
  23.     
  24.     return hex
  25.  
  26. _base64_chunksize = 32
  27.  
  28. def _base64ify(data, chunksize = None):
  29.     if chunksize is None:
  30.         chunksize = _base64_chunksize
  31.     
  32.     b64 = data.encode('base64_codec')
  33.     b64 = b64.replace('\n', '')
  34.     l = len(b64)
  35.     if l > chunksize:
  36.         chunks = []
  37.         i = 0
  38.         while i < l:
  39.             chunks.append(b64[i:i + chunksize])
  40.             i += chunksize
  41.         b64 = ' '.join(chunks)
  42.     
  43.     return b64
  44.  
  45. __escaped = {
  46.     '"': True,
  47.     '\\': True }
  48.  
  49. def _escapify(qstring):
  50.     text = ''
  51.     for c in qstring:
  52.         if c in __escaped:
  53.             text += '\\' + c
  54.             continue
  55.         if ord(c) >= 32 and ord(c) < 127:
  56.             text += c
  57.             continue
  58.         text += '\\%03d' % ord(c)
  59.     
  60.     return text
  61.  
  62.  
  63. def _truncate_bitmap(what):
  64.     for i in xrange(len(what) - 1, -1, -1):
  65.         if what[i] != '\x00':
  66.             break
  67.             continue
  68.     
  69.     return ''.join(what[0:i + 1])
  70.  
  71.  
  72. class Rdata(object):
  73.     __slots__ = [
  74.         'rdclass',
  75.         'rdtype']
  76.     
  77.     def __init__(self, rdclass, rdtype):
  78.         self.rdclass = rdclass
  79.         self.rdtype = rdtype
  80.  
  81.     
  82.     def covers(self):
  83.         return dns.rdatatype.NONE
  84.  
  85.     
  86.     def extended_rdatatype(self):
  87.         return self.covers() << 16 | self.rdtype
  88.  
  89.     
  90.     def to_text(self, origin = None, relativize = True, **kw):
  91.         raise NotImplementedError
  92.  
  93.     
  94.     def to_wire(self, file, compress = None, origin = None):
  95.         raise NotImplementedError
  96.  
  97.     
  98.     def validate(self):
  99.         dns.rdata.from_text(self.rdclass, self.rdtype, self.to_text())
  100.  
  101.     
  102.     def __repr__(self):
  103.         covers = self.covers()
  104.         if covers == dns.rdatatype.NONE:
  105.             ctext = ''
  106.         else:
  107.             ctext = '(' + dns.rdatatype.to_text(covers) + ')'
  108.         return '<DNS ' + dns.rdataclass.to_text(self.rdclass) + ' ' + dns.rdatatype.to_text(self.rdtype) + ctext + ' rdata: ' + str(self) + '>'
  109.  
  110.     
  111.     def __str__(self):
  112.         return self.to_text()
  113.  
  114.     
  115.     def _cmp(self, other):
  116.         raise NotImplementedError
  117.  
  118.     
  119.     def __eq__(self, other):
  120.         if not isinstance(other, Rdata):
  121.             return False
  122.         if self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  123.             return False
  124.         return self._cmp(other) == 0
  125.  
  126.     
  127.     def __ne__(self, other):
  128.         if not isinstance(other, Rdata):
  129.             return True
  130.         if self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  131.             return True
  132.         return self._cmp(other) != 0
  133.  
  134.     
  135.     def __lt__(self, other):
  136.         if not isinstance(other, Rdata) and self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  137.             return NotImplemented
  138.         return self._cmp(other) < 0
  139.  
  140.     
  141.     def __le__(self, other):
  142.         if not isinstance(other, Rdata) and self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  143.             return NotImplemented
  144.         return self._cmp(other) <= 0
  145.  
  146.     
  147.     def __ge__(self, other):
  148.         if not isinstance(other, Rdata) and self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  149.             return NotImplemented
  150.         return self._cmp(other) >= 0
  151.  
  152.     
  153.     def __gt__(self, other):
  154.         if not isinstance(other, Rdata) and self.rdclass != other.rdclass or self.rdtype != other.rdtype:
  155.             return NotImplemented
  156.         return self._cmp(other) > 0
  157.  
  158.     
  159.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  160.         raise NotImplementedError
  161.  
  162.     from_text = classmethod(from_text)
  163.     
  164.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  165.         raise NotImplementedError
  166.  
  167.     from_wire = classmethod(from_wire)
  168.     
  169.     def choose_relativity(self, origin = None, relativize = True):
  170.         pass
  171.  
  172.  
  173.  
  174. class GenericRdata(Rdata):
  175.     __slots__ = [
  176.         'data']
  177.     
  178.     def __init__(self, rdclass, rdtype, data):
  179.         super(GenericRdata, self).__init__(rdclass, rdtype)
  180.         self.data = data
  181.  
  182.     
  183.     def to_text(self, origin = None, relativize = True, **kw):
  184.         return '\\# %d ' % len(self.data) + _hexify(self.data)
  185.  
  186.     
  187.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  188.         if tok.get_string() != '\\#':
  189.             raise dns.exception.SyntaxError, 'generic rdata does not start with \\#'
  190.         tok.get_string() != '\\#'
  191.         length = tok.get_int()
  192.         chunks = []
  193.         while None:
  194.             (ttype, value) = tok.get()
  195.             if ttype == dns.tokenizer.EOL or ttype == dns.tokenizer.EOF:
  196.                 break
  197.             
  198.             continue
  199.             hex = ''.join(chunks)
  200.             data = hex.decode('hex_codec')
  201.             if len(data) != length:
  202.                 raise dns.exception.SyntaxError, 'generic rdata hex data has wrong length'
  203.             len(data) != length
  204.             return cls(rdclass, rdtype, data)
  205.  
  206.     from_text = classmethod(from_text)
  207.     
  208.     def to_wire(self, file, compress = None, origin = None):
  209.         file.write(self.data)
  210.  
  211.     
  212.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  213.         return cls(rdclass, rdtype, wire[current:current + rdlen])
  214.  
  215.     from_wire = classmethod(from_wire)
  216.     
  217.     def _cmp(self, other):
  218.         return cmp(self.data, other.data)
  219.  
  220.  
  221. _rdata_modules = { }
  222. _module_prefix = 'dns.rdtypes'
  223.  
  224. def get_rdata_class(rdclass, rdtype):
  225.     
  226.     def import_module(name):
  227.         mod = __import__(name)
  228.         components = name.split('.')
  229.         for comp in components[1:]:
  230.             mod = getattr(mod, comp)
  231.         
  232.         return mod
  233.  
  234.     mod = _rdata_modules.get((rdclass, rdtype))
  235.     rdclass_text = dns.rdataclass.to_text(rdclass)
  236.     rdtype_text = dns.rdatatype.to_text(rdtype)
  237.     rdtype_text = rdtype_text.replace('-', '_')
  238.     if not mod:
  239.         mod = _rdata_modules.get((dns.rdatatype.ANY, rdtype))
  240.         if not mod:
  241.             
  242.             try:
  243.                 mod = import_module('.'.join([
  244.                     _module_prefix,
  245.                     rdclass_text,
  246.                     rdtype_text]))
  247.                 _rdata_modules[(rdclass, rdtype)] = mod
  248.             except ImportError:
  249.                 
  250.                 try:
  251.                     mod = import_module('.'.join([
  252.                         _module_prefix,
  253.                         'ANY',
  254.                         rdtype_text]))
  255.                     _rdata_modules[(dns.rdataclass.ANY, rdtype)] = mod
  256.                 except ImportError:
  257.                     mod = None
  258.                 except:
  259.                     None<EXCEPTION MATCH>ImportError
  260.                 
  261.  
  262.                 None<EXCEPTION MATCH>ImportError
  263.             
  264.  
  265.         None<EXCEPTION MATCH>ImportError
  266.     
  267.     if mod:
  268.         cls = getattr(mod, rdtype_text)
  269.     else:
  270.         cls = GenericRdata
  271.     return cls
  272.  
  273.  
  274. def from_text(rdclass, rdtype, tok, origin = None, relativize = True):
  275.     if isinstance(tok, str):
  276.         tok = dns.tokenizer.Tokenizer(tok)
  277.     
  278.     cls = get_rdata_class(rdclass, rdtype)
  279.     if cls != GenericRdata:
  280.         token = tok.get()
  281.         tok.unget(token)
  282.         if token[0] == dns.tokenizer.IDENTIFIER and token[1] == '\\#':
  283.             rdata = GenericRdata.from_text(rdclass, rdtype, tok, origin, relativize)
  284.             return from_wire(rdclass, rdtype, rdata.data, 0, len(rdata.data), origin)
  285.     
  286.     return cls.from_text(rdclass, rdtype, tok, origin, relativize)
  287.  
  288.  
  289. def from_wire(rdclass, rdtype, wire, current, rdlen, origin = None):
  290.     cls = get_rdata_class(rdclass, rdtype)
  291.     return cls.from_wire(rdclass, rdtype, wire, current, rdlen, origin)
  292.  
  293.