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   
25 -class StdIOWrapper:
26
27 - def __init__(self, stream):
28 self.stream = stream 29 self.pos = 0 30 self.closed = 0
31
32 - def __getattr__(self, attrname, default=None):
33 return getattr(self.stream, attrname, default)
34
35 - def close(self):
36 if not self.closed: 37 self.closed = 1 38 self.stream.close()
39
40 - def seek(self, pos, mode=0):
41 raise ValueError("I/O operation on closed file")
42
43 - def tell(self):
44 if self.closed: 45 raise ValueError("I/O operation on closed file") 46 return self.pos
47
48 - def write(self, s):
49 if self.closed: 50 raise ValueError("I/O operation on closed file") 51 self.stream.write(s) 52 self.pos += len(s)
53
54 - def writelines(self, lines):
55 if self.closed: 56 raise ValueError("I/O operation on closed file") 57 self.stream.writelines(lines) 58 self.pos += len("".join(lines))
59