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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4.  
  5. try:
  6.     from errno import EINVAL
  7. except ImportError:
  8.     EINVAL = 22
  9.  
  10. __all__ = [
  11.     'StringIO']
  12.  
  13. def _complain_ifclosed(closed):
  14.     if closed:
  15.         raise ValueError, 'I/O operation on closed file'
  16.  
  17.  
  18. class StringIO:
  19.     
  20.     def __init__(self, buf = ''):
  21.         if not isinstance(buf, basestring):
  22.             buf = str(buf)
  23.         self.buf = buf
  24.         self.len = len(buf)
  25.         self.buflist = []
  26.         self.pos = 0
  27.         self.closed = False
  28.         self.softspace = 0
  29.  
  30.     
  31.     def __iter__(self):
  32.         return self
  33.  
  34.     
  35.     def next(self):
  36.         _complain_ifclosed(self.closed)
  37.         r = self.readline()
  38.         if not r:
  39.             raise StopIteration
  40.         return r
  41.  
  42.     
  43.     def close(self):
  44.         if not self.closed:
  45.             self.closed = True
  46.             del self.buf
  47.             del self.pos
  48.  
  49.     
  50.     def isatty(self):
  51.         _complain_ifclosed(self.closed)
  52.         return False
  53.  
  54.     
  55.     def seek(self, pos, mode = 0):
  56.         _complain_ifclosed(self.closed)
  57.         if self.buflist:
  58.             self.buf += ''.join(self.buflist)
  59.             self.buflist = []
  60.         if mode == 1:
  61.             pos += self.pos
  62.         elif mode == 2:
  63.             pos += self.len
  64.         self.pos = max(0, pos)
  65.  
  66.     
  67.     def tell(self):
  68.         _complain_ifclosed(self.closed)
  69.         return self.pos
  70.  
  71.     
  72.     def read(self, n = -1):
  73.         _complain_ifclosed(self.closed)
  74.         if self.buflist:
  75.             self.buf += ''.join(self.buflist)
  76.             self.buflist = []
  77.         r = self.buf[self.pos:newpos]
  78.         self.pos = newpos
  79.         return r
  80.  
  81.     
  82.     def readline(self, length = None):
  83.         _complain_ifclosed(self.closed)
  84.         if self.buflist:
  85.             self.buf += ''.join(self.buflist)
  86.             self.buflist = []
  87.         i = self.buf.find('\n', self.pos)
  88.         if length is not None and length > 0 and self.pos + length < newpos:
  89.             newpos = self.pos + length
  90.         
  91.         r = self.buf[self.pos:newpos]
  92.         self.pos = newpos
  93.         return r
  94.  
  95.     
  96.     def readlines(self, sizehint = 0):
  97.         total = 0
  98.         lines = []
  99.         line = self.readline()
  100.         while line:
  101.             lines.append(line)
  102.             total += len(line)
  103.             if sizehint < sizehint:
  104.                 pass
  105.             elif sizehint <= total:
  106.                 break
  107.             line = self.readline()
  108.         return lines
  109.  
  110.     
  111.     def truncate(self, size = None):
  112.         _complain_ifclosed(self.closed)
  113.         if size is None:
  114.             size = self.pos
  115.         elif size < 0:
  116.             raise IOError(EINVAL, 'Negative size not allowed')
  117.         elif size < self.pos:
  118.             self.pos = size
  119.         self.buf = self.getvalue()[:size]
  120.         self.len = size
  121.  
  122.     
  123.     def write(self, s):
  124.         _complain_ifclosed(self.closed)
  125.         if not s:
  126.             return None
  127.         if not None(s, basestring):
  128.             s = str(s)
  129.         spos = self.pos
  130.         slen = self.len
  131.         if spos == slen:
  132.             self.buflist.append(s)
  133.             self.len = self.pos = spos + len(s)
  134.             return None
  135.         if None > slen:
  136.             self.buflist.append('\x00' * (spos - slen))
  137.             slen = spos
  138.         newpos = spos + len(s)
  139.         self.len = slen
  140.         self.pos = newpos
  141.  
  142.     
  143.     def writelines(self, iterable):
  144.         write = self.write
  145.         for line in iterable:
  146.             write(line)
  147.         
  148.  
  149.     
  150.     def flush(self):
  151.         _complain_ifclosed(self.closed)
  152.  
  153.     
  154.     def getvalue(self):
  155.         if self.buflist:
  156.             self.buf += ''.join(self.buflist)
  157.             self.buflist = []
  158.         return self.buf
  159.  
  160.  
  161.  
  162. def test():
  163.     import sys
  164.     if sys.argv[1:]:
  165.         file = sys.argv[1]
  166.     else:
  167.         file = '/etc/passwd'
  168.     lines = open(file, 'r').readlines()
  169.     text = open(file, 'r').read()
  170.     f = StringIO()
  171.     for line in lines[:-2]:
  172.         f.write(line)
  173.     
  174.     f.writelines(lines[-2:])
  175.     if f.getvalue() != text:
  176.         raise RuntimeError, 'write failed'
  177.     length = f.tell()
  178.     print 'File length =', length
  179.     f.seek(len(lines[0]))
  180.     f.write(lines[1])
  181.     f.seek(0)
  182.     print 'First line =', repr(f.readline())
  183.     print 'Position =', f.tell()
  184.     line = f.readline()
  185.     print 'Second line =', repr(line)
  186.     f.seek(-len(line), 1)
  187.     line2 = f.read(len(line))
  188.     if line != line2:
  189.         raise RuntimeError, 'bad result after seek back'
  190.     f.seek(len(line2), 1)
  191.     list = f.readlines()
  192.     line = list[-1]
  193.     f.seek(f.tell() - len(line))
  194.     line2 = f.read()
  195.     if line != line2:
  196.         raise RuntimeError, 'bad result after seek back from EOF'
  197.     print 'Read', len(list), 'more lines'
  198.     print 'File length =', f.tell()
  199.     if f.tell() != length:
  200.         raise RuntimeError, 'bad length'
  201.     f.truncate(length / 2)
  202.     f.seek(0, 2)
  203.     print 'Truncated length =', f.tell()
  204.     if f.tell() != length / 2:
  205.         raise RuntimeError, 'truncate did not adjust length'
  206.     f.close()
  207.  
  208. if __name__ == '__main__':
  209.     test()
  210.