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

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