home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / xdrlib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  7.7 KB  |  264 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  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.         data = s[:n]
  94.         n = ((n + 3) // 4) * 4
  95.         data = data + (n - len(data)) * '\x00'
  96.         self._Packer__buf.write(data)
  97.  
  98.     pack_fopaque = pack_fstring
  99.     
  100.     def pack_string(self, s):
  101.         n = len(s)
  102.         self.pack_uint(n)
  103.         self.pack_fstring(n, s)
  104.  
  105.     pack_opaque = pack_string
  106.     pack_bytes = pack_string
  107.     
  108.     def pack_list(self, list, pack_item):
  109.         for item in list:
  110.             self.pack_uint(1)
  111.             pack_item(item)
  112.         
  113.         self.pack_uint(0)
  114.  
  115.     
  116.     def pack_farray(self, n, list, pack_item):
  117.         if len(list) != n:
  118.             raise ValueError, 'wrong array size'
  119.         for item in list:
  120.             pack_item(item)
  121.         
  122.  
  123.     
  124.     def pack_array(self, list, pack_item):
  125.         n = len(list)
  126.         self.pack_uint(n)
  127.         self.pack_farray(n, list, pack_item)
  128.  
  129.  
  130.  
  131. class Unpacker:
  132.     
  133.     def __init__(self, data):
  134.         self.reset(data)
  135.  
  136.     
  137.     def reset(self, data):
  138.         self._Unpacker__buf = data
  139.         self._Unpacker__pos = 0
  140.  
  141.     
  142.     def get_position(self):
  143.         return self._Unpacker__pos
  144.  
  145.     
  146.     def set_position(self, position):
  147.         self._Unpacker__pos = position
  148.  
  149.     
  150.     def get_buffer(self):
  151.         return self._Unpacker__buf
  152.  
  153.     
  154.     def done(self):
  155.         if self._Unpacker__pos < len(self._Unpacker__buf):
  156.             raise Error('unextracted data remains')
  157.  
  158.     
  159.     def unpack_uint(self):
  160.         i = self._Unpacker__pos
  161.         self._Unpacker__pos = j = i + 4
  162.         data = self._Unpacker__buf[i:j]
  163.         if len(data) < 4:
  164.             raise EOFError
  165.         x = struct.unpack('>L', data)[0]
  166.         
  167.         try:
  168.             return int(x)
  169.         except OverflowError:
  170.             return x
  171.  
  172.  
  173.     
  174.     def unpack_int(self):
  175.         i = self._Unpacker__pos
  176.         self._Unpacker__pos = j = i + 4
  177.         data = self._Unpacker__buf[i:j]
  178.         if len(data) < 4:
  179.             raise EOFError
  180.         return struct.unpack('>l', data)[0]
  181.  
  182.     unpack_enum = unpack_int
  183.     
  184.     def unpack_bool(self):
  185.         return bool(self.unpack_int())
  186.  
  187.     
  188.     def unpack_uhyper(self):
  189.         hi = self.unpack_uint()
  190.         lo = self.unpack_uint()
  191.         return long(hi) << 32 | lo
  192.  
  193.     
  194.     def unpack_hyper(self):
  195.         x = self.unpack_uhyper()
  196.         if x >= 0x8000000000000000L:
  197.             x = x - 0x10000000000000000L
  198.         return x
  199.  
  200.     
  201.     def unpack_float(self):
  202.         i = self._Unpacker__pos
  203.         self._Unpacker__pos = j = i + 4
  204.         data = self._Unpacker__buf[i:j]
  205.         if len(data) < 4:
  206.             raise EOFError
  207.         return struct.unpack('>f', data)[0]
  208.  
  209.     
  210.     def unpack_double(self):
  211.         i = self._Unpacker__pos
  212.         self._Unpacker__pos = j = i + 8
  213.         data = self._Unpacker__buf[i:j]
  214.         if len(data) < 8:
  215.             raise EOFError
  216.         return struct.unpack('>d', data)[0]
  217.  
  218.     
  219.     def unpack_fstring(self, n):
  220.         if n < 0:
  221.             raise ValueError, 'fstring size must be nonnegative'
  222.         i = self._Unpacker__pos
  223.         j = i + ((n + 3) // 4) * 4
  224.         if j > len(self._Unpacker__buf):
  225.             raise EOFError
  226.         self._Unpacker__pos = j
  227.         return self._Unpacker__buf[i:i + n]
  228.  
  229.     unpack_fopaque = unpack_fstring
  230.     
  231.     def unpack_string(self):
  232.         n = self.unpack_uint()
  233.         return self.unpack_fstring(n)
  234.  
  235.     unpack_opaque = unpack_string
  236.     unpack_bytes = unpack_string
  237.     
  238.     def unpack_list(self, unpack_item):
  239.         list = []
  240.         while None:
  241.             x = self.unpack_uint()
  242.             if x == 0:
  243.                 break
  244.             if x != 1:
  245.                 raise ConversionError, '0 or 1 expected, got %r' % (x,)
  246.             item = unpack_item()
  247.             continue
  248.             return list
  249.  
  250.     
  251.     def unpack_farray(self, n, unpack_item):
  252.         list = []
  253.         for i in range(n):
  254.             list.append(unpack_item())
  255.         
  256.         return list
  257.  
  258.     
  259.     def unpack_array(self, unpack_item):
  260.         n = self.unpack_uint()
  261.         return self.unpack_farray(n, unpack_item)
  262.  
  263.  
  264.