home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1651 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.8 KB  |  323 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.rdata as dns
  8. _pows = (0x1L, 0xAL, 0x64L, 0x3E8L, 0x2710L, 0x186A0L, 0xF4240L, 0x989680L, 0x5F5E100L, 0x3B9ACA00L, 0x2540BE400L)
  9.  
  10. def _exponent_of(what, desc):
  11.     exp = None
  12.     for i in xrange(len(_pows)):
  13.         if what // _pows[i] == 0x0L:
  14.             exp = i - 1
  15.             break
  16.             continue
  17.     
  18.     if exp is None or exp < 0:
  19.         raise dns.exception.SyntaxError, '%s value out of bounds' % desc
  20.     exp < 0
  21.     return exp
  22.  
  23.  
  24. def _float_to_tuple(what):
  25.     if what < 0:
  26.         sign = -1
  27.         what *= -1
  28.     else:
  29.         sign = 1
  30.     what = long(round(what * 3600000))
  31.     degrees = int(what // 3600000)
  32.     what -= degrees * 3600000
  33.     minutes = int(what // 60000)
  34.     what -= minutes * 60000
  35.     seconds = int(what // 1000)
  36.     what -= int(seconds * 1000)
  37.     what = int(what)
  38.     return (degrees * sign, minutes, seconds, what)
  39.  
  40.  
  41. def _tuple_to_float(what):
  42.     if what[0] < 0:
  43.         sign = -1
  44.         value = float(what[0]) * -1
  45.     else:
  46.         sign = 1
  47.         value = float(what[0])
  48.     value += float(what[1]) / 60
  49.     value += float(what[2]) / 3600
  50.     value += float(what[3]) / 3.6e+06
  51.     return sign * value
  52.  
  53.  
  54. def _encode_size(what, desc):
  55.     what = long(what)
  56.     exponent = _exponent_of(what, desc) & 15
  57.     base = what // pow(10, exponent) & 15
  58.     return base * 16 + exponent
  59.  
  60.  
  61. def _decode_size(what, desc):
  62.     exponent = what & 15
  63.     if exponent > 9:
  64.         raise dns.exception.SyntaxError, 'bad %s exponent' % desc
  65.     exponent > 9
  66.     base = (what & 240) >> 4
  67.     if base > 9:
  68.         raise dns.exception.SyntaxError, 'bad %s base' % desc
  69.     base > 9
  70.     return long(base) * pow(10, exponent)
  71.  
  72.  
  73. class LOC(dns.rdata.Rdata):
  74.     __slots__ = [
  75.         'latitude',
  76.         'longitude',
  77.         'altitude',
  78.         'size',
  79.         'horizontal_precision',
  80.         'vertical_precision']
  81.     
  82.     def __init__(self, rdclass, rdtype, latitude, longitude, altitude, size = 1, hprec = 10000, vprec = 10):
  83.         super(LOC, self).__init__(rdclass, rdtype)
  84.         if isinstance(latitude, int) or isinstance(latitude, long):
  85.             latitude = float(latitude)
  86.         
  87.         if isinstance(latitude, float):
  88.             latitude = _float_to_tuple(latitude)
  89.         
  90.         self.latitude = latitude
  91.         if isinstance(longitude, int) or isinstance(longitude, long):
  92.             longitude = float(longitude)
  93.         
  94.         if isinstance(longitude, float):
  95.             longitude = _float_to_tuple(longitude)
  96.         
  97.         self.longitude = longitude
  98.         self.altitude = float(altitude)
  99.         self.size = float(size)
  100.         self.horizontal_precision = float(hprec)
  101.         self.vertical_precision = float(vprec)
  102.  
  103.     
  104.     def to_text(self, origin = None, relativize = True, **kw):
  105.         if self.latitude[0] > 0:
  106.             lat_hemisphere = 'N'
  107.             lat_degrees = self.latitude[0]
  108.         else:
  109.             lat_hemisphere = 'S'
  110.             lat_degrees = -1 * self.latitude[0]
  111.         if self.longitude[0] > 0:
  112.             long_hemisphere = 'E'
  113.             long_degrees = self.longitude[0]
  114.         else:
  115.             long_hemisphere = 'W'
  116.             long_degrees = -1 * self.longitude[0]
  117.         text = '%d %d %d.%03d %s %d %d %d.%03d %s %0.2fm' % (lat_degrees, self.latitude[1], self.latitude[2], self.latitude[3], lat_hemisphere, long_degrees, self.longitude[1], self.longitude[2], self.longitude[3], long_hemisphere, self.altitude / 100)
  118.         if self.size != 1 and self.horizontal_precision != 10000 or self.vertical_precision != 10:
  119.             text += ' %0.2fm %0.2fm %0.2fm' % (self.size / 100, self.horizontal_precision / 100, self.vertical_precision / 100)
  120.         
  121.         return text
  122.  
  123.     
  124.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  125.         latitude = [
  126.             0,
  127.             0,
  128.             0,
  129.             0]
  130.         longitude = [
  131.             0,
  132.             0,
  133.             0,
  134.             0]
  135.         size = 1
  136.         hprec = 10000
  137.         vprec = 10
  138.         latitude[0] = tok.get_int()
  139.         t = tok.get_string()
  140.         if t.isdigit():
  141.             latitude[1] = int(t)
  142.             t = tok.get_string()
  143.             if '.' in t:
  144.                 (seconds, milliseconds) = t.split('.')
  145.                 if not seconds.isdigit():
  146.                     raise dns.exception.SyntaxError, 'bad latitude seconds value'
  147.                 seconds.isdigit()
  148.                 latitude[2] = int(seconds)
  149.                 if latitude[2] >= 60:
  150.                     raise dns.exception.SyntaxError, 'latitude seconds >= 60'
  151.                 latitude[2] >= 60
  152.                 l = len(milliseconds)
  153.                 if l == 0 and l > 3 or not milliseconds.isdigit():
  154.                     raise dns.exception.SyntaxError, 'bad latitude milliseconds value'
  155.                 not milliseconds.isdigit()
  156.                 if l == 1:
  157.                     m = 100
  158.                 elif l == 2:
  159.                     m = 10
  160.                 else:
  161.                     m = 1
  162.                 latitude[3] = m * int(milliseconds)
  163.                 t = tok.get_string()
  164.             elif t.isdigit():
  165.                 latitude[2] = int(t)
  166.                 t = tok.get_string()
  167.             
  168.         
  169.         if t == 'S':
  170.             latitude[0] *= -1
  171.         elif t != 'N':
  172.             raise dns.exception.SyntaxError, 'bad latitude hemisphere value'
  173.         
  174.         longitude[0] = tok.get_int()
  175.         t = tok.get_string()
  176.         if t.isdigit():
  177.             longitude[1] = int(t)
  178.             t = tok.get_string()
  179.             if '.' in t:
  180.                 (seconds, milliseconds) = t.split('.')
  181.                 if not seconds.isdigit():
  182.                     raise dns.exception.SyntaxError, 'bad longitude seconds value'
  183.                 seconds.isdigit()
  184.                 longitude[2] = int(seconds)
  185.                 if longitude[2] >= 60:
  186.                     raise dns.exception.SyntaxError, 'longitude seconds >= 60'
  187.                 longitude[2] >= 60
  188.                 l = len(milliseconds)
  189.                 if l == 0 and l > 3 or not milliseconds.isdigit():
  190.                     raise dns.exception.SyntaxError, 'bad longitude milliseconds value'
  191.                 not milliseconds.isdigit()
  192.                 if l == 1:
  193.                     m = 100
  194.                 elif l == 2:
  195.                     m = 10
  196.                 else:
  197.                     m = 1
  198.                 longitude[3] = m * int(milliseconds)
  199.                 t = tok.get_string()
  200.             elif t.isdigit():
  201.                 longitude[2] = int(t)
  202.                 t = tok.get_string()
  203.             
  204.         
  205.         if t == 'W':
  206.             longitude[0] *= -1
  207.         elif t != 'E':
  208.             raise dns.exception.SyntaxError, 'bad longitude hemisphere value'
  209.         
  210.         t = tok.get_string()
  211.         if t[-1] == 'm':
  212.             t = t[0:-1]
  213.         
  214.         altitude = float(t) * 100
  215.         (ttype, value) = tok.get()
  216.         if ttype != dns.tokenizer.EOL and ttype != dns.tokenizer.EOF:
  217.             if value[-1] == 'm':
  218.                 value = value[0:-1]
  219.             
  220.             size = float(value) * 100
  221.             (ttype, value) = tok.get()
  222.             if ttype != dns.tokenizer.EOL and ttype != dns.tokenizer.EOF:
  223.                 if value[-1] == 'm':
  224.                     value = value[0:-1]
  225.                 
  226.                 hprec = float(value) * 100
  227.                 (ttype, value) = tok.get()
  228.                 if ttype != dns.tokenizer.EOL and ttype != dns.tokenizer.EOF:
  229.                     if value[-1] == 'm':
  230.                         value = value[0:-1]
  231.                         vprec = float(value) * 100
  232.                         (ttype, value) = tok.get()
  233.                         if ttype != dns.tokenizer.EOL and ttype != dns.tokenizer.EOF:
  234.                             raise dns.exception.SyntaxError, 'expected EOL or EOF'
  235.                         ttype != dns.tokenizer.EOF
  236.                     
  237.                 
  238.             
  239.         
  240.         return cls(rdclass, rdtype, latitude, longitude, altitude, size, hprec, vprec)
  241.  
  242.     from_text = classmethod(from_text)
  243.     
  244.     def to_wire(self, file, compress = None, origin = None):
  245.         if self.latitude[0] < 0:
  246.             sign = -1
  247.             degrees = long(-1 * self.latitude[0])
  248.         else:
  249.             sign = 1
  250.             degrees = long(self.latitude[0])
  251.         milliseconds = (degrees * 3600000 + self.latitude[1] * 60000 + self.latitude[2] * 1000 + self.latitude[3]) * sign
  252.         latitude = 0x80000000L + milliseconds
  253.         if self.longitude[0] < 0:
  254.             sign = -1
  255.             degrees = long(-1 * self.longitude[0])
  256.         else:
  257.             sign = 1
  258.             degrees = long(self.longitude[0])
  259.         milliseconds = (degrees * 3600000 + self.longitude[1] * 60000 + self.longitude[2] * 1000 + self.longitude[3]) * sign
  260.         longitude = 0x80000000L + milliseconds
  261.         altitude = long(self.altitude) + 0x989680L
  262.         size = _encode_size(self.size, 'size')
  263.         hprec = _encode_size(self.horizontal_precision, 'horizontal precision')
  264.         vprec = _encode_size(self.vertical_precision, 'vertical precision')
  265.         wire = struct.pack('!BBBBIII', 0, size, hprec, vprec, latitude, longitude, altitude)
  266.         file.write(wire)
  267.  
  268.     
  269.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  270.         (version, size, hprec, vprec, latitude, longitude, altitude) = struct.unpack('!BBBBIII', wire[current:current + rdlen])
  271.         if latitude > 0x80000000L:
  272.             latitude = float(latitude - 0x80000000L) / 3600000
  273.         else:
  274.             latitude = -1 * float(0x80000000L - latitude) / 3600000
  275.         if latitude < -90 or latitude > 90:
  276.             raise dns.exception.FormError, 'bad latitude'
  277.         latitude > 90
  278.         if longitude > 0x80000000L:
  279.             longitude = float(longitude - 0x80000000L) / 3600000
  280.         else:
  281.             longitude = -1 * float(0x80000000L - longitude) / 3600000
  282.         if longitude < -180 or longitude > 180:
  283.             raise dns.exception.FormError, 'bad longitude'
  284.         longitude > 180
  285.         altitude = float(altitude) - 1e+07
  286.         size = _decode_size(size, 'size')
  287.         hprec = _decode_size(hprec, 'horizontal precision')
  288.         vprec = _decode_size(vprec, 'vertical precision')
  289.         return cls(rdclass, rdtype, latitude, longitude, altitude, size, hprec, vprec)
  290.  
  291.     from_wire = classmethod(from_wire)
  292.     
  293.     def _cmp(self, other):
  294.         f = cStringIO.StringIO()
  295.         self.to_wire(f)
  296.         wire1 = f.getvalue()
  297.         f.seek(0)
  298.         f.truncate()
  299.         other.to_wire(f)
  300.         wire2 = f.getvalue()
  301.         f.close()
  302.         return cmp(wire1, wire2)
  303.  
  304.     
  305.     def _get_float_latitude(self):
  306.         return _tuple_to_float(self.latitude)
  307.  
  308.     
  309.     def _set_float_latitude(self, value):
  310.         self.latitude = _float_to_tuple(value)
  311.  
  312.     float_latitude = property(_get_float_latitude, _set_float_latitude, doc = 'latitude as a floating point value')
  313.     
  314.     def _get_float_longitude(self):
  315.         return _tuple_to_float(self.longitude)
  316.  
  317.     
  318.     def _set_float_longitude(self, value):
  319.         self.longitude = _float_to_tuple(value)
  320.  
  321.     float_longitude = property(_get_float_longitude, _set_float_longitude, doc = 'longitude as a floating point value')
  322.  
  323.