/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/iterablefile.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2005 Aaron Bentley
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
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.
8
 
#
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.
13
 
#
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
17
 
 
18
 
from __future__ import absolute_import
19
 
 
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
import doctest
20
19
 
21
20
class IterableFileBase(object):
22
21
    """Create a file-like object from any iterable"""
24
23
    def __init__(self, iterable):
25
24
        object.__init__(self)
26
25
        self._iter = iterable.__iter__()
27
 
        self._buffer = b""
 
26
        self._buffer = ""
28
27
        self.done = False
29
28
 
30
29
    def read_n(self, length):
52
51
                if len(result) >= length:
53
52
                    return length
54
53
            try:
55
 
                return result.index(sequence) + len(sequence)
 
54
                return result.index(sequence)+len(sequence)
56
55
            except ValueError:
57
56
                return None
58
57
        return self._read(test_contents)
68
67
        result = self._buffer
69
68
        while result_length(result) is None:
70
69
            try:
71
 
                result += next(self._iter)
 
70
                result += self._iter.next()
72
71
            except StopIteration:
73
72
                self.done = True
74
 
                self._buffer = b""
 
73
                self._buffer = ""
75
74
                return result
76
75
        output_length = result_length(result)
77
76
        self._buffer = result[output_length:]
86
85
            return None
87
86
        return self._read(no_stop)
88
87
 
 
88
 
89
89
    def push_back(self, contents):
90
90
        """
91
91
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
100
100
 
101
101
class IterableFile(object):
102
102
    """This class supplies all File methods that can be implemented cheaply."""
103
 
 
104
103
    def __init__(self, iterable):
105
104
        object.__init__(self)
106
105
        self._file_base = IterableFileBase(iterable)
111
110
    def _make_iterator(self):
112
111
        while not self._file_base.done:
113
112
            self._check_closed()
114
 
            result = self._file_base.read_to(b'\n')
115
 
            if result != b'':
 
113
            result = self._file_base.read_to('\n')
 
114
            if result != '':
116
115
                yield result
117
116
 
118
117
    def _check_closed(self):
133
132
 
134
133
    closed = property(lambda x: x._closed)
135
134
 
136
 
    def __enter__(self):
137
 
        return self
138
 
 
139
 
    def __exit__(self, exc_type, exc_val, exc_tb):
140
 
        # If there was an error raised, prefer the original one
141
 
        try:
142
 
            self.close()
143
 
        except BaseException:
144
 
            if exc_type is None:
145
 
                raise
146
 
        return False
147
 
 
148
135
    def flush(self):
149
136
        """No-op for standard compliance.
150
137
        >>> f = IterableFile([])
155
142
        """
156
143
        self._check_closed()
157
144
 
158
 
    def __next__(self):
 
145
    def next(self):
159
146
        """Implementation of the iterator protocol's next()
160
147
 
161
148
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
162
 
        >>> next(f)
 
149
        >>> f.next()
163
150
        'This \\n'
164
151
        >>> f.close()
165
 
        >>> next(f)
 
152
        >>> f.next()
166
153
        Traceback (most recent call last):
167
154
        ValueError: File is closed.
168
155
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
169
 
        >>> next(f)
 
156
        >>> f.next()
170
157
        'This \\n'
171
 
        >>> next(f)
 
158
        >>> f.next()
172
159
        'is a test.\\n'
173
 
        >>> next(f)
 
160
        >>> f.next()
174
161
        Traceback (most recent call last):
175
162
        StopIteration
176
163
        """
177
164
        self._check_closed()
178
 
        return next(self._iter)
179
 
 
180
 
    next = __next__
 
165
        return self._iter.next()
181
166
 
182
167
    def __iter__(self):
183
168
        """
240
225
        Traceback (most recent call last):
241
226
        ValueError: File is closed.
242
227
        """
243
 
        return self.read_to(b'\n', size)
 
228
        return self.read_to('\n', size)
244
229
 
245
230
    def readlines(self, sizehint=None):
246
231
        """
256
241
        lines = []
257
242
        while True:
258
243
            line = self.readline()
259
 
            if line == b"":
 
244
            if line == "":
260
245
                return lines
261
246
            if sizehint is None:
262
247
                lines.append(line)
267
252
                self._file_base.push_back(line)
268
253
                return lines
269
254
 
270
 
 
 
255
        
271
256
if __name__ == "__main__":
272
 
    import doctest
273
257
    doctest.testmod()