/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 breezy/iterablefile.py

  • Committer: Jelmer Vernooij
  • Date: 2020-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
 
18
from __future__ import absolute_import
 
19
 
18
20
 
19
21
class IterableFileBase(object):
20
22
    """Create a file-like object from any iterable"""
27
29
 
28
30
    def read_n(self, length):
29
31
        """
30
 
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_n(8)
31
 
        b'This is '
 
32
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_n(8)
 
33
        'This is '
32
34
        """
33
35
        def test_length(result):
34
36
            if len(result) >= length:
39
41
 
40
42
    def read_to(self, sequence, length=None):
41
43
        """
42
 
        >>> f = IterableFileBase([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
43
 
        >>> f.read_to(b'\\n')
44
 
        b'Th\\n'
45
 
        >>> f.read_to(b'\\n')
46
 
        b'is is \\n'
 
44
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
45
        >>> f.read_to('\\n')
 
46
        'Th\\n'
 
47
        >>> f.read_to('\\n')
 
48
        'is is \\n'
47
49
        """
48
50
        def test_contents(result):
49
51
            if length is not None:
77
79
 
78
80
    def read_all(self):
79
81
        """
80
 
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_all()
81
 
        b'This is a test.'
 
82
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_all()
 
83
        'This is a test.'
82
84
        """
83
85
        def no_stop(result):
84
86
            return None
86
88
 
87
89
    def push_back(self, contents):
88
90
        """
89
 
        >>> f = IterableFileBase([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
90
 
        >>> f.read_to(b'\\n')
91
 
        b'Th\\n'
92
 
        >>> f.push_back(b"Sh")
 
91
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
92
        >>> f.read_to('\\n')
 
93
        'Th\\n'
 
94
        >>> f.push_back("Sh")
93
95
        >>> f.read_all()
94
 
        b'Shis is \\na te\\nst.'
 
96
        'Shis is \\na te\\nst.'
95
97
        """
96
98
        self._buffer = contents + self._buffer
97
99
 
119
121
 
120
122
    def close(self):
121
123
        """
122
 
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
 
124
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
123
125
        >>> f.closed
124
126
        False
125
127
        >>> f.close()
156
158
    def __next__(self):
157
159
        """Implementation of the iterator protocol's next()
158
160
 
159
 
        >>> f = IterableFile([b'This \\n', b'is ', b'a ', b'test.'])
 
161
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
160
162
        >>> next(f)
161
 
        b'This \\n'
 
163
        'This \\n'
162
164
        >>> f.close()
163
165
        >>> next(f)
164
166
        Traceback (most recent call last):
165
167
        ValueError: File is closed.
166
 
        >>> f = IterableFile([b'This \\n', b'is ', b'a ', b'test.\\n'])
167
 
        >>> next(f)
168
 
        b'This \\n'
169
 
        >>> next(f)
170
 
        b'is a test.\\n'
 
168
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
 
169
        >>> next(f)
 
170
        'This \\n'
 
171
        >>> next(f)
 
172
        'is a test.\\n'
171
173
        >>> next(f)
172
174
        Traceback (most recent call last):
173
175
        StopIteration
179
181
 
180
182
    def __iter__(self):
181
183
        """
182
 
        >>> list(IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.']))
183
 
        [b'Th\\n', b'is is \\n', b'a te\\n', b'st.']
184
 
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
184
        >>> list(IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.']))
 
185
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
 
186
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
185
187
        >>> f.close()
186
188
        >>> list(f)
187
189
        Traceback (most recent call last):
191
193
 
192
194
    def read(self, length=None):
193
195
        """
194
 
        >>> IterableFile([b'This ', b'is ', b'a ', b'test.']).read()
195
 
        b'This is a test.'
196
 
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
 
196
        >>> IterableFile(['This ', 'is ', 'a ', 'test.']).read()
 
197
        'This is a test.'
 
198
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
197
199
        >>> f.read(10)
198
 
        b'This is a '
199
 
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
 
200
        'This is a '
 
201
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
200
202
        >>> f.close()
201
203
        >>> f.read(10)
202
204
        Traceback (most recent call last):
213
215
        Read characters until a sequence is found, with optional max size.
214
216
        The specified sequence, if found, will be included in the result
215
217
 
216
 
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
217
 
        >>> f.read_to(b'i')
218
 
        b'Th\\ni'
219
 
        >>> f.read_to(b'i')
220
 
        b's i'
 
218
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
219
        >>> f.read_to('i')
 
220
        'Th\\ni'
 
221
        >>> f.read_to('i')
 
222
        's i'
221
223
        >>> f.close()
222
 
        >>> f.read_to(b'i')
 
224
        >>> f.read_to('i')
223
225
        Traceback (most recent call last):
224
226
        ValueError: File is closed.
225
227
        """
228
230
 
229
231
    def readline(self, size=None):
230
232
        """
231
 
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
233
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
232
234
        >>> f.readline()
233
 
        b'Th\\n'
 
235
        'Th\\n'
234
236
        >>> f.readline(4)
235
 
        b'is i'
 
237
        'is i'
236
238
        >>> f.close()
237
239
        >>> f.readline()
238
240
        Traceback (most recent call last):
242
244
 
243
245
    def readlines(self, sizehint=None):
244
246
        """
245
 
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
247
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
246
248
        >>> f.readlines()
247
 
        [b'Th\\n', b'is is \\n', b'a te\\n', b'st.']
248
 
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
 
249
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
 
250
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
249
251
        >>> f.close()
250
252
        >>> f.readlines()
251
253
        Traceback (most recent call last):