/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    def __init__(self, iterable):
23
23
        object.__init__(self)
24
24
        self._iter = iterable.__iter__()
25
 
        self._buffer = ""
 
25
        self._buffer = b""
26
26
        self.done = False
27
27
 
28
28
    def read_n(self, length):
29
29
        """
30
 
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_n(8)
31
 
        'This is '
 
30
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_n(8)
 
31
        b'This is '
32
32
        """
33
33
        def test_length(result):
34
34
            if len(result) >= length:
39
39
 
40
40
    def read_to(self, sequence, length=None):
41
41
        """
42
 
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
43
 
        >>> f.read_to('\\n')
44
 
        'Th\\n'
45
 
        >>> f.read_to('\\n')
46
 
        'is is \\n'
 
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'
47
47
        """
48
48
        def test_contents(result):
49
49
            if length is not None:
50
50
                if len(result) >= length:
51
51
                    return length
52
52
            try:
53
 
                return result.index(sequence)+len(sequence)
 
53
                return result.index(sequence) + len(sequence)
54
54
            except ValueError:
55
55
                return None
56
56
        return self._read(test_contents)
66
66
        result = self._buffer
67
67
        while result_length(result) is None:
68
68
            try:
69
 
                result += self._iter.next()
 
69
                result += next(self._iter)
70
70
            except StopIteration:
71
71
                self.done = True
72
 
                self._buffer = ""
 
72
                self._buffer = b""
73
73
                return result
74
74
        output_length = result_length(result)
75
75
        self._buffer = result[output_length:]
77
77
 
78
78
    def read_all(self):
79
79
        """
80
 
        >>> IterableFileBase(['This ', 'is ', 'a ', 'test.']).read_all()
81
 
        'This is a test.'
 
80
        >>> IterableFileBase([b'This ', b'is ', b'a ', b'test.']).read_all()
 
81
        b'This is a test.'
82
82
        """
83
83
        def no_stop(result):
84
84
            return None
85
85
        return self._read(no_stop)
86
86
 
87
 
 
88
87
    def push_back(self, contents):
89
88
        """
90
 
        >>> f = IterableFileBase(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
91
 
        >>> f.read_to('\\n')
92
 
        'Th\\n'
93
 
        >>> f.push_back("Sh")
 
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")
94
93
        >>> f.read_all()
95
 
        'Shis is \\na te\\nst.'
 
94
        b'Shis is \\na te\\nst.'
96
95
        """
97
96
        self._buffer = contents + self._buffer
98
97
 
99
98
 
100
99
class IterableFile(object):
101
100
    """This class supplies all File methods that can be implemented cheaply."""
 
101
 
102
102
    def __init__(self, iterable):
103
103
        object.__init__(self)
104
104
        self._file_base = IterableFileBase(iterable)
109
109
    def _make_iterator(self):
110
110
        while not self._file_base.done:
111
111
            self._check_closed()
112
 
            result = self._file_base.read_to('\n')
113
 
            if result != '':
 
112
            result = self._file_base.read_to(b'\n')
 
113
            if result != b'':
114
114
                yield result
115
115
 
116
116
    def _check_closed(self):
119
119
 
120
120
    def close(self):
121
121
        """
122
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
122
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
123
123
        >>> f.closed
124
124
        False
125
125
        >>> f.close()
131
131
 
132
132
    closed = property(lambda x: x._closed)
133
133
 
 
134
    def __enter__(self):
 
135
        return self
 
136
 
 
137
    def __exit__(self, exc_type, exc_val, exc_tb):
 
138
        # If there was an error raised, prefer the original one
 
139
        try:
 
140
            self.close()
 
141
        except BaseException:
 
142
            if exc_type is None:
 
143
                raise
 
144
        return False
 
145
 
134
146
    def flush(self):
135
147
        """No-op for standard compliance.
136
148
        >>> f = IterableFile([])
141
153
        """
142
154
        self._check_closed()
143
155
 
144
 
    def next(self):
 
156
    def __next__(self):
145
157
        """Implementation of the iterator protocol's next()
146
158
 
147
 
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.'])
148
 
        >>> f.next()
149
 
        'This \\n'
 
159
        >>> f = IterableFile([b'This \\n', b'is ', b'a ', b'test.'])
 
160
        >>> next(f)
 
161
        b'This \\n'
150
162
        >>> f.close()
151
 
        >>> f.next()
 
163
        >>> next(f)
152
164
        Traceback (most recent call last):
153
165
        ValueError: File is closed.
154
 
        >>> f = IterableFile(['This \\n', 'is ', 'a ', 'test.\\n'])
155
 
        >>> f.next()
156
 
        'This \\n'
157
 
        >>> f.next()
158
 
        'is a test.\\n'
159
 
        >>> f.next()
 
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'
 
171
        >>> next(f)
160
172
        Traceback (most recent call last):
161
173
        StopIteration
162
174
        """
163
175
        self._check_closed()
164
 
        return self._iter.next()
 
176
        return next(self._iter)
 
177
 
 
178
    next = __next__
165
179
 
166
180
    def __iter__(self):
167
181
        """
168
 
        >>> list(IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.']))
169
 
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
170
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
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.'])
171
185
        >>> f.close()
172
186
        >>> list(f)
173
187
        Traceback (most recent call last):
177
191
 
178
192
    def read(self, length=None):
179
193
        """
180
 
        >>> IterableFile(['This ', 'is ', 'a ', 'test.']).read()
181
 
        'This is a test.'
182
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
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.'])
183
197
        >>> f.read(10)
184
 
        'This is a '
185
 
        >>> f = IterableFile(['This ', 'is ', 'a ', 'test.'])
 
198
        b'This is a '
 
199
        >>> f = IterableFile([b'This ', b'is ', b'a ', b'test.'])
186
200
        >>> f.close()
187
201
        >>> f.read(10)
188
202
        Traceback (most recent call last):
199
213
        Read characters until a sequence is found, with optional max size.
200
214
        The specified sequence, if found, will be included in the result
201
215
 
202
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
203
 
        >>> f.read_to('i')
204
 
        'Th\\ni'
205
 
        >>> f.read_to('i')
206
 
        's i'
 
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'
207
221
        >>> f.close()
208
 
        >>> f.read_to('i')
 
222
        >>> f.read_to(b'i')
209
223
        Traceback (most recent call last):
210
224
        ValueError: File is closed.
211
225
        """
214
228
 
215
229
    def readline(self, size=None):
216
230
        """
217
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
231
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
218
232
        >>> f.readline()
219
 
        'Th\\n'
 
233
        b'Th\\n'
220
234
        >>> f.readline(4)
221
 
        'is i'
 
235
        b'is i'
222
236
        >>> f.close()
223
237
        >>> f.readline()
224
238
        Traceback (most recent call last):
225
239
        ValueError: File is closed.
226
240
        """
227
 
        return self.read_to('\n', size)
 
241
        return self.read_to(b'\n', size)
228
242
 
229
243
    def readlines(self, sizehint=None):
230
244
        """
231
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
245
        >>> f = IterableFile([b'Th\\nis ', b'is \\n', b'a ', b'te\\nst.'])
232
246
        >>> f.readlines()
233
 
        ['Th\\n', 'is is \\n', 'a te\\n', 'st.']
234
 
        >>> f = IterableFile(['Th\\nis ', 'is \\n', 'a ', 'te\\nst.'])
 
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.'])
235
249
        >>> f.close()
236
250
        >>> f.readlines()
237
251
        Traceback (most recent call last):
240
254
        lines = []
241
255
        while True:
242
256
            line = self.readline()
243
 
            if line == "":
 
257
            if line == b"":
244
258
                return lines
245
259
            if sizehint is None:
246
260
                lines.append(line)