Package translate :: Package misc :: Module stdiotell |
[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.stdiotell

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 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 sys.stdout etc that provides tell() for current position"""
    23
    24 -class StdIOWrapper:
    25 -    def __init__(self, stream):
    26          self.stream = stream
    27          self.pos = 0
    28          self.closed = 0
    29
    30 -    def __getattr__(self, attrname, default=None):
    31          return getattr(self.stream, attrname, default)
    32
    33 -    def close(self):
    34          if not self.closed:
    35              self.closed = 1
    36              self.stream.close()
    37
    38 -    def seek(self, pos, mode = 0):
    39          raise ValueError, "I/O operation on closed file"
    40
    41 -    def tell(self):
    42          if self.closed:
    43              raise ValueError, "I/O operation on closed file"
    44          return self.pos
    45
    46 -    def write(self, s):
    47          if self.closed:
    48              raise ValueError, "I/O operation on closed file"
    49          self.stream.write(s)
    50          self.pos += len(s)
    51
    52 -    def writelines(self, lines):
    53          if self.closed:
    54              raise ValueError, "I/O operation on closed file"
    55          self.stream.writelines(lines)
    56          self.pos += len("".join(lines))
    57