Package translate :: Package misc :: Module wStringIO

[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.wStringIO

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2006 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21
     22  """A wrapper for cStringIO that provides more of the functions of StringIO at the speed of cStringIO"""
     23
     24  import cStringIO
     25
     26
     27 -class StringIO:
     28
     29 -    def __init__(self, buf=''):
     30          if not isinstance(buf, (str, unicode)):
     31              buf = str(buf)
     32          if isinstance(buf, unicode):
     33              buf = buf.encode('utf-8')
     34          self.len = len(buf)
     35          self.buf = cStringIO.StringIO()
     36          self.buf.write(buf)
     37          self.buf.seek(0)
     38          self.pos = 0
     39          self.closed = 0
     40
     41 -    def __iter__(self):
     42          return self
     43
     44 -    def next(self):
     45          if self.closed:
     46              raise StopIteration
     47          r = self.readline()
     48          if not r:
     49              raise StopIteration
     50          return r
     51
     52 -    def close(self):
     53          """Free the memory buffer.
     54          """
     55          if not self.closed:
     56              self.closed = 1
     57              del self.buf, self.pos
     58
     59 -    def isatty(self):
     60          if self.closed:
     61              raise ValueError("I/O operation on closed file")
     62          return False
     63
     64 -    def seek(self, pos, mode=0):
     65          if self.closed:
     66              raise ValueError("I/O operation on closed file")
     67          self.buf.seek(pos, mode)
     68          self.pos = self.buf.tell()
     69
     70 -    def tell(self):
     71          if self.closed:
     72              raise ValueError("I/O operation on closed file")
     73          return self.pos
     74
     75 -    def read(self, n=None):
     76          if self.closed:
     77              raise ValueError("I/O operation on closed file")
     78          if n == None:
     79              r = self.buf.read()
     80          else:
     81              r = self.buf.read(n)
     82          self.pos = self.buf.tell()
     83          return r
     84
     85 -    def readline(self, length=None):
     86          if self.closed:
     87              raise ValueError("I/O operation on closed file")
     88          if length is not None:
     89              r = self.buf.readline(length)
     90          else:
     91              r = self.buf.readline()
     92          self.pos = self.buf.tell()
     93          return r
     94
     95 -    def readlines(self):
     96          if self.closed:
     97              raise ValueError("I/O operation on closed file")
     98          lines = self.buf.readlines()
     99          self.pos = self.buf.tell()
    100          return lines
    101
    102 -    def truncate(self, size=None):
    103          if self.closed:
    104              raise ValueError("I/O operation on closed file")
    105          self.buf.truncate(size)
    106          self.pos = self.buf.tell()
    107          self.buf.seek(0, 2)
    108          self.len = self.buf.tell()
    109          self.buf.seek(self.pos)
    110
    111 -    def write(self, s):
    112          if self.closed:
    113              raise ValueError("I/O operation on closed file")
    114          origpos = self.buf.tell()
    115          self.buf.write(s)
    116          self.pos = self.buf.tell()
    117          if origpos + len(s) > self.len:
    118              self.buf.seek(0, 2)
    119              self.len = self.buf.tell()
    120              self.buf.seek(self.pos)
    121
    122 -    def writelines(self, lines):
    123          if self.closed:
    124              raise ValueError("I/O operation on closed file")
    125          self.buf.writelines(lines)
    126          self.pos = self.buf.tell()
    127          self.buf.seek(0, 2)
    128          self.len = self.buf.tell()
    129          self.buf.seek(self.pos)
    130
    131 -    def flush(self):
    132          if self.closed:
    133              raise ValueError("I/O operation on closed file")
    134          self.buf.flush()
    135
    136 -    def getvalue(self):
    137          if self.closed:
    138              raise ValueError("I/O operation on closed file")
    139          return self.buf.getvalue()
    140
    141
    142 -class CatchStringOutput(StringIO, object):
    143      """catches the output before it is closed and sends it to an onclose method"""
    144
    145 -    def __init__(self, onclose):
    146          """Set up the output stream, and remember a method to call on closing"""
    147          StringIO.__init__(self)
    148          self.onclose = onclose
    149
    150 -    def close(self):
    151          """wrap the underlying close method, to pass the value to onclose before it goes"""
    152          value = self.getvalue()
    153          self.onclose(value)
    154          super(CatchStringOutput, self).close()
    155
    156 -    def slam(self):
    157          """use this method to force the closing of the stream if it isn't closed yet"""
    158          if not self.closed:
    159              self.close()
    160