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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import struct
  5.  
  6. try:
  7.     from cStringIO import StringIO as _StringIO
  8. except ImportError:
  9.     from StringIO import StringIO as _StringIO
  10.  
  11. __all__ = [
  12.     'Error',
  13.     'Packer',
  14.     'Unpacker',
  15.     'ConversionError']
  16.  
  17. class Error(Exception):
  18.     
  19.     def __init__(self, msg):
  20.         self.msg = msg
  21.  
  22.     
  23.     def __repr__(self):
  24.         return repr(self.msg)
  25.  
  26.     
  27.     def __str__(self):
  28.         return str(self.msg)
  29.  
  30.  
  31.  
  32. class ConversionError(Error):
  33.     pass
  34.  
  35.  
  36. class Packer:
  37.     
  38.     def __init__(self):
  39.         self.reset()
  40.  
  41.     
  42.     def reset(self):
  43.         self._Packer__buf = _StringIO()
  44.  
  45.     
  46.     def get_buffer(self):
  47.         return self._Packer__buf.getvalue()
  48.  
  49.     get_buf = get_buffer
  50.     
  51.     def pack_uint(self, x):
  52.         self._Packer__buf.write(struct.pack('>L', x))
  53.  
  54.     pack_int = pack_uint
  55.     pack_enum = pack_int
  56.     
  57.     def pack_bool(self, x):
  58.         if x:
  59.             self._Packer__buf.write('\x00\x00\x00\x01')
  60.         else:
  61.             self._Packer__buf.write('\x00\x00\x00\x00')
  62.  
  63.     
  64.     def pack_uhyper(self, x):
  65.         self.pack_uint(x >> 32 & 0xFFFFFFFFL)
  66.         self.pack_uint(x & 0xFFFFFFFFL)
  67.  
  68.     pack_hyper = pack_uhyper
  69.     
  70.     def pack_float(self, x):
  71.         
  72.         try:
  73.             self._Packer__buf.write(struct.pack('>f', x))
  74.         except struct.error:
  75.             msg = None
  76.             raise ConversionError, msg
  77.  
  78.  
  79.     
  80.     def pack_double(self, x):
  81.         
  82.         try:
  83.             self._Packer__buf.write(struct.pack('>d', x))
  84.         except struct.error:
  85.             msg = None
  86.             raise ConversionError, msg
  87.  
  88.  
  89.     
  90.     def pack_fstring(self, n, s):
  91.         if n < 0:
  92.             raise ValueError, 'fstring size must be nonnegative'
  93.         n < 0
  94.         data = s[:n]
  95.         n = ((n + 3) // 4) * 4
  96.         data = data + (n - len(data)) * '\x00'
  97.         self._Packer__buf.write(data)
  98.  
  99.     pack_fopaque = pack_fstring
  100.     
  101.     def pack_string(self, s):
  102.         n = len(s)
  103.         self.pack_uint(n)
  104.         self.pack_fstring(n, s)
  105.  
  106.     pack_opaque = pack_string
  107.     pack_bytes = pack_string
  108.     
  109.     def pack_list(self, list, pack_item):
  110.         for item in list:
  111.             self.pack_uint(1)
  112.             pack_item(item)
  113.         
  114.         self.pack_uint(0)
  115.  
  116.     
  117.     def pack_farray(self, n, list, pack_item):
  118.         if len(list) != n:
  119.             raise ValueError, 'wrong array size'
  120.         len(list) != n
  121.         for item in list:
  122.             pack_item(item)
  123.         
  124.  
  125.     
  126.     def pack_array(self, list, pack_item):
  127.         n = len(list)
  128.         self.pack_uint(n)
  129.         self.pack_farray(n, list, pack_item)
  130.  
  131.  
  132.  
  133. class Unpacker:
  134.     
  135.     def __init__(self, data):
  136.         self.reset(data)
  137.  
  138.     
  139.     def reset(self, data):
  140.         self._Unpacker__buf = data
  141.         self._Unpacker__pos = 0
  142.  
  143.     
  144.     def get_position(self):
  145.         return self._Unpacker__pos
  146.  
  147.     
  148.     def set_position(self, position):
  149.         self._Unpacker__pos = position
  150.  
  151.     
  152.     def get_buffer(self):
  153.         return self._Unpacker__buf
  154.  
  155.     
  156.     def done(self):
  157.         if self._Unpacker__pos < len(self._Unpacker__buf):
  158.             raise Error('unextracted data remains')
  159.         self._Unpacker__pos < len(self._Unpacker__buf)
  160.  
  161.     
  162.     def unpack_uint(self):
  163.         i = self._Unpacker__pos
  164.         self._Unpacker__pos = j = i + 4
  165.         data = self._Unpacker__buf[i:j]
  166.         if len(data) < 4:
  167.             raise EOFError
  168.         len(data) < 4
  169.         x = struct.unpack('>L', data)[0]
  170.         
  171.         try:
  172.             return int(x)
  173.         except OverflowError:
  174.             return x
  175.  
  176.  
  177.     
  178.     def unpack_int(self):
  179.         i = self._Unpacker__pos
  180.         self._Unpacker__pos = j = i + 4
  181.         data = self._Unpacker__buf[i:j]
  182.         if len(data) < 4:
  183.             raise EOFError
  184.         len(data) < 4
  185.         return struct.unpack('>l', data)[0]
  186.  
  187.     unpack_enum = unpack_int
  188.     
  189.     def unpack_bool(self):
  190.         return bool(self.unpack_int())
  191.  
  192.     
  193.     def unpack_uhyper(self):
  194.         hi = self.unpack_uint()
  195.         lo = self.unpack_uint()
  196.         return long(hi) << 32 | lo
  197.  
  198.     
  199.     def unpack_hyper(self):
  200.         x = self.unpack_uhyper()
  201.         if x >= 0x8000000000000000L:
  202.             x = x - 0x10000000000000000L
  203.         
  204.         return x
  205.  
  206.     
  207.     def unpack_float(self):
  208.         i = self._Unpacker__pos
  209.         self._Unpacker__pos = j = i + 4
  210.         data = self._Unpacker__buf[i:j]
  211.         if len(data) < 4:
  212.             raise EOFError
  213.         len(data) < 4
  214.         return struct.unpack('>f', data)[0]
  215.  
  216.     
  217.     def unpack_double(self):
  218.         i = self._Unpacker__pos
  219.         self._Unpacker__pos = j = i + 8
  220.         data = self._Unpacker__buf[i:j]
  221.         if len(data) < 8:
  222.             raise EOFError
  223.         len(data) < 8
  224.         return struct.unpack('>d', data)[0]
  225.  
  226.     
  227.     def unpack_fstring(self, n):
  228.         if n < 0:
  229.             raise ValueError, 'fstring size must be nonnegative'
  230.         n < 0
  231.         i = self._Unpacker__pos
  232.         j = i + ((n + 3) // 4) * 4
  233.         if j > len(self._Unpacker__buf):
  234.             raise EOFError
  235.         j > len(self._Unpacker__buf)
  236.         self._Unpacker__pos = j
  237.         return self._Unpacker__buf[i:i + n]
  238.  
  239.     unpack_fopaque = unpack_fstring
  240.     
  241.     def unpack_string(self):
  242.         n = self.unpack_uint()
  243.         return self.unpack_fstring(n)
  244.  
  245.     unpack_opaque = unpack_string
  246.     unpack_bytes = unpack_string
  247.     
  248.     def unpack_list(self, unpack_item):
  249.         list = []
  250.         while None:
  251.             x = self.unpack_uint()
  252.             if x == 0:
  253.                 break
  254.             
  255.             if x != 1:
  256.                 raise ConversionError, '0 or 1 expected, got %r' % (x,)
  257.             item = unpack_item()
  258.             list.append(item)
  259.             continue
  260.             return list
  261.  
  262.     
  263.     def unpack_farray(self, n, unpack_item):
  264.         list = []
  265.         for i in range(n):
  266.             list.append(unpack_item())
  267.         
  268.         return list
  269.  
  270.     
  271.     def unpack_array(self, unpack_item):
  272.         n = self.unpack_uint()
  273.         return self.unpack_farray(n, unpack_item)
  274.  
  275.  
  276.