1
# Copyright (C) 2005 Aaron Bentley, Canonical Ltd
1
# Copyright (C) 2005 Aaron Bentley
2
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from __future__ import absolute_import
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
20
class IterableFileBase(object):
22
21
"""Create a file-like object from any iterable"""
68
67
result = self._buffer
69
68
while result_length(result) is None:
71
result += next(self._iter)
70
result += self._iter.next()
72
71
except StopIteration:
76
75
output_length = result_length(result)
77
76
self._buffer = result[output_length:]
87
86
return self._read(no_stop)
89
89
def push_back(self, contents):
91
91
>>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
101
101
class IterableFile(object):
102
102
"""This class supplies all File methods that can be implemented cheaply."""
104
103
def __init__(self, iterable):
105
104
object.__init__(self)
106
105
self._file_base = IterableFileBase(iterable)
134
133
closed = property(lambda x: x._closed)
139
def __exit__(self, exc_type, exc_val, exc_tb):
140
# If there was an error raised, prefer the original one
143
except BaseException:
149
136
"""No-op for standard compliance.
150
137
>>> f = IterableFile([])
156
143
self._check_closed()
159
146
"""Implementation of the iterator protocol's next()
161
148
>>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
166
153
Traceback (most recent call last):
167
154
ValueError: File is closed.
168
155
>>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
174
161
Traceback (most recent call last):
177
164
self._check_closed()
178
return next(self._iter)
165
return self._iter.next()
182
167
def __iter__(self):
240
225
Traceback (most recent call last):
241
226
ValueError: File is closed.
243
return self.read_to(b'\n', size)
228
return self.read_to('\n', size)
245
230
def readlines(self, sizehint=None):