/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/rio.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
import re
36
36
 
37
 
from . import osutils
38
 
from .iterablefile import IterableFile
39
 
from .sixish import (
40
 
    text_type,
41
 
    )
 
37
from bzrlib import osutils
 
38
from bzrlib.iterablefile import IterableFile
42
39
 
43
40
# XXX: some redundancy is allowing to write stanzas in isolation as well as
44
41
# through a writer object.
50
47
 
51
48
    def write_stanza(self, stanza):
52
49
        if self._soft_nl:
53
 
            self._to_file.write(b'\n')
 
50
            self._to_file.write('\n')
54
51
        stanza.write(self._to_file)
55
52
        self._soft_nl = True
56
53
 
77
74
    """Produce a rio IterableFile from an iterable of stanzas"""
78
75
    def str_iter():
79
76
        if header is not None:
80
 
            yield header + b'\n'
 
77
            yield header + '\n'
81
78
        first_stanza = True
82
79
        for s in stanzas:
83
80
            if first_stanza is not True:
84
 
                yield b'\n'
 
81
                yield '\n'
85
82
            for line in s.to_lines():
86
83
                yield line
87
84
            first_stanza = False
124
121
        """Append a name and value to the stanza."""
125
122
        if not valid_tag(tag):
126
123
            raise ValueError("invalid tag %r" % (tag,))
127
 
        if isinstance(value, bytes):
128
 
            value = value.decode('ascii')
129
 
        elif isinstance(value, text_type):
 
124
        if isinstance(value, str):
 
125
            value = unicode(value)
 
126
        elif isinstance(value, unicode):
130
127
            pass
 
128
        ## elif isinstance(value, (int, long)):
 
129
        ##    value = str(value)           # XXX: python2.4 without L-suffix
131
130
        else:
132
131
            raise TypeError("invalid type for rio value: %r of type %s"
133
132
                            % (value, type(value)))
174
173
            # max() complains if sequence is empty
175
174
            return []
176
175
        result = []
177
 
        for text_tag, text_value in self.items:
178
 
            tag = text_tag.encode('ascii')
179
 
            value = text_value.encode('utf-8')
180
 
            if value == b'':
181
 
                result.append(tag + b': \n')
182
 
            elif b'\n' in value:
 
176
        for tag, value in self.items:
 
177
            if value == '':
 
178
                result.append(tag + ': \n')
 
179
            elif '\n' in value:
183
180
                # don't want splitlines behaviour on empty lines
184
 
                val_lines = value.split(b'\n')
185
 
                result.append(tag + b': ' + val_lines[0] + b'\n')
 
181
                val_lines = value.split('\n')
 
182
                result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
186
183
                for line in val_lines[1:]:
187
 
                    result.append(b'\t' + line + b'\n')
 
184
                    result.append('\t' + line.encode('utf-8') + '\n')
188
185
            else:
189
 
                result.append(tag + b': ' + value + b'\n')
 
186
                result.append(tag + ': ' + value.encode('utf-8') + '\n')
190
187
        return result
191
188
 
192
189
    def to_string(self):
193
190
        """Return stanza as a single string"""
194
 
        return b''.join(self.to_lines())
 
191
        return ''.join(self.to_lines())
195
192
 
196
193
    def to_unicode(self):
197
194
        """Return stanza as a single Unicode string.
304
301
    max_rio_width = max_width - 4
305
302
    lines = []
306
303
    for pline in stanza.to_lines():
307
 
        for line in pline.split(b'\n')[:-1]:
308
 
            line = re.sub(b'\\\\', b'\\\\\\\\', line)
 
304
        for line in pline.split('\n')[:-1]:
 
305
            line = re.sub('\\\\', '\\\\\\\\', line)
309
306
            while len(line) > 0:
310
307
                partline = line[:max_rio_width]
311
308
                line = line[max_rio_width:]
312
 
                if len(line) > 0 and line[:1] != [b' ']:
 
309
                if len(line) > 0 and line[0] != [' ']:
313
310
                    break_index = -1
314
 
                    break_index = partline.rfind(b' ', -20)
 
311
                    break_index = partline.rfind(' ', -20)
315
312
                    if break_index < 3:
316
 
                        break_index = partline.rfind(b'-', -20)
 
313
                        break_index = partline.rfind('-', -20)
317
314
                        break_index += 1
318
315
                    if break_index < 3:
319
 
                        break_index = partline.rfind(b'/', -20)
 
316
                        break_index = partline.rfind('/', -20)
320
317
                    if break_index >= 3:
321
318
                        line = partline[break_index:] + line
322
319
                        partline = partline[:break_index]
323
320
                if len(line) > 0:
324
 
                    line = b'  ' + line
325
 
                partline = re.sub(b'\r', b'\\\\r', partline)
 
321
                    line = '  ' + line
 
322
                partline = re.sub('\r', '\\\\r', partline)
326
323
                blank_line = False
327
324
                if len(line) > 0:
328
 
                    partline += b'\\'
329
 
                elif re.search(b' $', partline):
330
 
                    partline += b'\\'
 
325
                    partline += '\\'
 
326
                elif re.search(' $', partline):
 
327
                    partline += '\\'
331
328
                    blank_line = True
332
 
                lines.append(b'# ' + partline + b'\n')
 
329
                lines.append('# ' + partline + '\n')
333
330
                if blank_line:
334
 
                    lines.append(b'#   \n')
 
331
                    lines.append('#   \n')
335
332
    return lines
336
333
 
337
334
 
338
335
def _patch_stanza_iter(line_iter):
339
 
    map = {b'\\\\': b'\\',
340
 
           b'\\r' : b'\r',
341
 
           b'\\\n': b''}
 
336
    map = {'\\\\': '\\',
 
337
           '\\r' : '\r',
 
338
           '\\\n': ''}
342
339
    def mapget(match):
343
340
        return map[match.group(0)]
344
341
 
345
342
    last_line = None
346
343
    for line in line_iter:
347
 
        if line.startswith(b'# '):
 
344
        if line.startswith('# '):
348
345
            line = line[2:]
349
 
        elif line.startswith(b'#'):
 
346
        elif line.startswith('#'):
350
347
            line = line[1:]
351
348
        else:
352
349
            raise ValueError("bad line %r" % (line,))
353
350
        if last_line is not None and len(line) > 2:
354
351
            line = line[2:]
355
 
        line = re.sub(b'\r', b'', line)
356
 
        line = re.sub(b'\\\\(.|\n)', mapget, line)
 
352
        line = re.sub('\r', '', line)
 
353
        line = re.sub('\\\\(.|\n)', mapget, line)
357
354
        if last_line is None:
358
355
            last_line = line
359
356
        else:
360
357
            last_line += line
361
 
        if last_line[-1:] == b'\n':
 
358
        if last_line[-1] == '\n':
362
359
            yield last_line
363
360
            last_line = None
364
361
    if last_line is not None:
378
375
 
379
376
 
380
377
try:
381
 
    from ._rio_pyx import (
 
378
    from bzrlib._rio_pyx import (
382
379
        _read_stanza_utf8,
383
380
        _read_stanza_unicode,
384
381
        _valid_tag,
385
382
        )
386
 
except ImportError as e:
 
383
except ImportError, e:
387
384
    osutils.failed_to_load_extension(e)
388
 
    from ._rio_py import (
 
385
    from bzrlib._rio_py import (
389
386
       _read_stanza_utf8,
390
387
       _read_stanza_unicode,
391
388
       _valid_tag,