/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/rio.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:
32
32
 
33
33
import re
34
34
 
35
 
from bzrlib import osutils
36
 
from bzrlib.iterablefile import IterableFile
 
35
from . import osutils
 
36
from .iterablefile import IterableFile
37
37
 
38
38
# XXX: some redundancy is allowing to write stanzas in isolation as well as
39
39
# through a writer object.
40
40
 
 
41
 
41
42
class RioWriter(object):
 
43
 
42
44
    def __init__(self, to_file):
43
45
        self._soft_nl = False
44
46
        self._to_file = to_file
45
47
 
46
48
    def write_stanza(self, stanza):
47
49
        if self._soft_nl:
48
 
            self._to_file.write('\n')
 
50
            self._to_file.write(b'\n')
49
51
        stanza.write(self._to_file)
50
52
        self._soft_nl = True
51
53
 
56
58
    to_file can be anything that can be enumerated as a sequence of
57
59
    lines (with newlines.)
58
60
    """
 
61
 
59
62
    def __init__(self, from_file):
60
63
        self._from_file = from_file
61
64
 
72
75
    """Produce a rio IterableFile from an iterable of stanzas"""
73
76
    def str_iter():
74
77
        if header is not None:
75
 
            yield header + '\n'
 
78
            yield header + b'\n'
76
79
        first_stanza = True
77
80
        for s in stanzas:
78
81
            if first_stanza is not True:
79
 
                yield '\n'
 
82
                yield b'\n'
80
83
            for line in s.to_lines():
81
84
                yield line
82
85
            first_stanza = False
84
87
 
85
88
 
86
89
def read_stanzas(from_file):
 
90
 
87
91
    while True:
88
92
        s = read_stanza(from_file)
89
93
        if s is None:
90
94
            break
91
 
        else:
92
 
            yield s
 
95
        yield s
 
96
 
 
97
 
 
98
def read_stanzas_unicode(from_file):
 
99
 
 
100
    while True:
 
101
        s = read_stanza_unicode(from_file)
 
102
        if s is None:
 
103
            break
 
104
        yield s
 
105
 
93
106
 
94
107
class Stanza(object):
95
108
    """One stanza for rio.
119
132
        """Append a name and value to the stanza."""
120
133
        if not valid_tag(tag):
121
134
            raise ValueError("invalid tag %r" % (tag,))
122
 
        if isinstance(value, str):
123
 
            value = unicode(value)
124
 
        elif isinstance(value, unicode):
 
135
        if isinstance(value, bytes):
 
136
            value = value.decode('ascii')
 
137
        elif isinstance(value, str):
125
138
            pass
126
 
        ## elif isinstance(value, (int, long)):
127
 
        ##    value = str(value)           # XXX: python2.4 without L-suffix
128
139
        else:
129
140
            raise TypeError("invalid type for rio value: %r of type %s"
130
141
                            % (value, type(value)))
171
182
            # max() complains if sequence is empty
172
183
            return []
173
184
        result = []
174
 
        for tag, value in self.items:
175
 
            if value == '':
176
 
                result.append(tag + ': \n')
177
 
            elif '\n' in value:
 
185
        for text_tag, text_value in self.items:
 
186
            tag = text_tag.encode('ascii')
 
187
            value = text_value.encode('utf-8', 'surrogateescape')
 
188
            if value == b'':
 
189
                result.append(tag + b': \n')
 
190
            elif b'\n' in value:
178
191
                # don't want splitlines behaviour on empty lines
179
 
                val_lines = value.split('\n')
180
 
                result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
 
192
                val_lines = value.split(b'\n')
 
193
                result.append(tag + b': ' + val_lines[0] + b'\n')
181
194
                for line in val_lines[1:]:
182
 
                    result.append('\t' + line.encode('utf-8') + '\n')
 
195
                    result.append(b'\t' + line + b'\n')
183
196
            else:
184
 
                result.append(tag + ': ' + value.encode('utf-8') + '\n')
 
197
                result.append(tag + b': ' + value + b'\n')
185
198
        return result
186
199
 
187
200
    def to_string(self):
188
201
        """Return stanza as a single string"""
189
 
        return ''.join(self.to_lines())
 
202
        return b''.join(self.to_lines())
190
203
 
191
204
    def to_unicode(self):
192
205
        """Return stanza as a single Unicode string.
299
312
    max_rio_width = max_width - 4
300
313
    lines = []
301
314
    for pline in stanza.to_lines():
302
 
        for line in pline.split('\n')[:-1]:
303
 
            line = re.sub('\\\\', '\\\\\\\\', line)
 
315
        for line in pline.split(b'\n')[:-1]:
 
316
            line = re.sub(b'\\\\', b'\\\\\\\\', line)
304
317
            while len(line) > 0:
305
318
                partline = line[:max_rio_width]
306
319
                line = line[max_rio_width:]
307
 
                if len(line) > 0 and line[0] != [' ']:
 
320
                if len(line) > 0 and line[:1] != [b' ']:
308
321
                    break_index = -1
309
 
                    break_index = partline.rfind(' ', -20)
 
322
                    break_index = partline.rfind(b' ', -20)
310
323
                    if break_index < 3:
311
 
                        break_index = partline.rfind('-', -20)
 
324
                        break_index = partline.rfind(b'-', -20)
312
325
                        break_index += 1
313
326
                    if break_index < 3:
314
 
                        break_index = partline.rfind('/', -20)
 
327
                        break_index = partline.rfind(b'/', -20)
315
328
                    if break_index >= 3:
316
329
                        line = partline[break_index:] + line
317
330
                        partline = partline[:break_index]
318
331
                if len(line) > 0:
319
 
                    line = '  ' + line
320
 
                partline = re.sub('\r', '\\\\r', partline)
 
332
                    line = b'  ' + line
 
333
                partline = re.sub(b'\r', b'\\\\r', partline)
321
334
                blank_line = False
322
335
                if len(line) > 0:
323
 
                    partline += '\\'
324
 
                elif re.search(' $', partline):
325
 
                    partline += '\\'
 
336
                    partline += b'\\'
 
337
                elif re.search(b' $', partline):
 
338
                    partline += b'\\'
326
339
                    blank_line = True
327
 
                lines.append('# ' + partline + '\n')
 
340
                lines.append(b'# ' + partline + b'\n')
328
341
                if blank_line:
329
 
                    lines.append('#   \n')
 
342
                    lines.append(b'#   \n')
330
343
    return lines
331
344
 
332
345
 
333
346
def _patch_stanza_iter(line_iter):
334
 
    map = {'\\\\': '\\',
335
 
           '\\r' : '\r',
336
 
           '\\\n': ''}
 
347
    map = {b'\\\\': b'\\',
 
348
           b'\\r': b'\r',
 
349
           b'\\\n': b''}
 
350
 
337
351
    def mapget(match):
338
352
        return map[match.group(0)]
339
353
 
340
354
    last_line = None
341
355
    for line in line_iter:
342
 
        if line.startswith('# '):
 
356
        if line.startswith(b'# '):
343
357
            line = line[2:]
344
 
        elif line.startswith('#'):
 
358
        elif line.startswith(b'#'):
345
359
            line = line[1:]
346
360
        else:
347
361
            raise ValueError("bad line %r" % (line,))
348
362
        if last_line is not None and len(line) > 2:
349
363
            line = line[2:]
350
 
        line = re.sub('\r', '', line)
351
 
        line = re.sub('\\\\(.|\n)', mapget, line)
 
364
        line = re.sub(b'\r', b'', line)
 
365
        line = re.sub(b'\\\\(.|\n)', mapget, line)
352
366
        if last_line is None:
353
367
            last_line = line
354
368
        else:
355
369
            last_line += line
356
 
        if last_line[-1] == '\n':
 
370
        if last_line[-1:] == b'\n':
357
371
            yield last_line
358
372
            last_line = None
359
373
    if last_line is not None:
373
387
 
374
388
 
375
389
try:
376
 
    from bzrlib._rio_pyx import (
 
390
    from ._rio_pyx import (
377
391
        _read_stanza_utf8,
378
392
        _read_stanza_unicode,
379
393
        _valid_tag,
380
394
        )
381
 
except ImportError, e:
 
395
except ImportError as e:
382
396
    osutils.failed_to_load_extension(e)
383
 
    from bzrlib._rio_py import (
384
 
       _read_stanza_utf8,
385
 
       _read_stanza_unicode,
386
 
       _valid_tag,
387
 
       )
 
397
    from ._rio_py import (
 
398
        _read_stanza_utf8,
 
399
        _read_stanza_unicode,
 
400
        _valid_tag,
 
401
        )