2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
6 # This file is part of translate.
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.
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.
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
22 """A wrapper for cStringIO that provides more of the functions of StringIO at the speed of cStringIO"""
27 def __init__(self
, buf
= ''):
28 if not isinstance(buf
, (str, unicode)):
30 if isinstance(buf
, unicode):
31 buf
= buf
.encode('utf-8')
33 self
.buf
= cStringIO
.StringIO()
51 """Free the memory buffer.
55 del self
.buf
, self
.pos
59 raise ValueError, "I/O operation on closed file"
62 def seek(self
, pos
, mode
= 0):
64 raise ValueError, "I/O operation on closed file"
65 self
.buf
.seek(pos
, mode
)
66 self
.pos
= self
.buf
.tell()
70 raise ValueError, "I/O operation on closed file"
73 def read(self
, n
= None):
75 raise ValueError, "I/O operation on closed file"
80 self
.pos
= self
.buf
.tell()
83 def readline(self
, length
=None):
85 raise ValueError, "I/O operation on closed file"
86 if length
is not None:
87 r
= self
.buf
.readline(length
)
89 r
= self
.buf
.readline(length
)
90 self
.pos
= self
.buf
.tell()
95 raise ValueError, "I/O operation on closed file"
96 lines
= self
.buf
.readlines()
97 self
.pos
= self
.buf
.tell()
100 def truncate(self
, size
=None):
102 raise ValueError, "I/O operation on closed file"
103 self
.buf
.truncate(size
)
104 self
.pos
= self
.buf
.tell()
106 self
.len = self
.buf
.tell()
107 self
.buf
.seek(self
.pos
)
111 raise ValueError, "I/O operation on closed file"
112 origpos
= self
.buf
.tell()
114 self
.pos
= self
.buf
.tell()
115 if origpos
+ len(s
) > self
.len:
117 self
.len = self
.buf
.tell()
118 self
.buf
.seek(self
.pos
)
120 def writelines(self
, lines
):
122 raise ValueError, "I/O operation on closed file"
123 self
.buf
.writelines(lines
)
124 self
.pos
= self
.buf
.tell()
126 self
.len = self
.buf
.tell()
127 self
.buf
.seek(self
.pos
)
131 raise ValueError, "I/O operation on closed file"
136 raise ValueError, "I/O operation on closed file"
137 return self
.buf
.getvalue()
139 class CatchStringOutput(StringIO
, object):
140 """catches the output before it is closed and sends it to an onclose method"""
141 def __init__(self
, onclose
):
142 """Set up the output stream, and remember a method to call on closing"""
143 StringIO
.__init
__(self
)
144 self
.onclose
= onclose
147 """wrap the underlying close method, to pass the value to onclose before it goes"""
148 value
= self
.getvalue()
150 super(CatchStringOutput
, self
).close()
153 """use this method to force the closing of the stream if it isn't closed yet"""