/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: Martin
  • Date: 2017-06-11 14:25:36 UTC
  • mto: This revision was merged to the branch mainline in revision 6688.
  • Revision ID: gzlist@googlemail.com-20170611142536-8la6fegm5f3s7e5r
Finish making rio module string usage work on Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
            # max() complains if sequence is empty
175
175
            return []
176
176
        result = []
177
 
        for tag, value in self.items:
178
 
            if value == '':
179
 
                result.append(tag.encode('ascii') + b': \n')
180
 
            elif '\n' in value:
 
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:
181
183
                # don't want splitlines behaviour on empty lines
182
 
                val_lines = value.split('\n')
183
 
                result.append(tag + b': ' + val_lines[0].encode('utf-8') + b'\n')
 
184
                val_lines = value.split(b'\n')
 
185
                result.append(tag + b': ' + val_lines[0] + b'\n')
184
186
                for line in val_lines[1:]:
185
 
                    result.append(b'\t' + line.encode('utf-8') + b'\n')
 
187
                    result.append(b'\t' + line + b'\n')
186
188
            else:
187
 
                result.append(tag.encode('ascii') + b': ' + value.encode('utf-8') + b'\n')
 
189
                result.append(tag + b': ' + value + b'\n')
188
190
        return result
189
191
 
190
192
    def to_string(self):
302
304
    max_rio_width = max_width - 4
303
305
    lines = []
304
306
    for pline in stanza.to_lines():
305
 
        for line in pline.split('\n')[:-1]:
306
 
            line = re.sub('\\\\', '\\\\\\\\', line)
 
307
        for line in pline.split(b'\n')[:-1]:
 
308
            line = re.sub(b'\\\\', b'\\\\\\\\', line)
307
309
            while len(line) > 0:
308
310
                partline = line[:max_rio_width]
309
311
                line = line[max_rio_width:]
310
 
                if len(line) > 0 and line[0] != [' ']:
 
312
                if len(line) > 0 and line[:1] != [b' ']:
311
313
                    break_index = -1
312
 
                    break_index = partline.rfind(' ', -20)
 
314
                    break_index = partline.rfind(b' ', -20)
313
315
                    if break_index < 3:
314
 
                        break_index = partline.rfind('-', -20)
 
316
                        break_index = partline.rfind(b'-', -20)
315
317
                        break_index += 1
316
318
                    if break_index < 3:
317
 
                        break_index = partline.rfind('/', -20)
 
319
                        break_index = partline.rfind(b'/', -20)
318
320
                    if break_index >= 3:
319
321
                        line = partline[break_index:] + line
320
322
                        partline = partline[:break_index]
321
323
                if len(line) > 0:
322
 
                    line = '  ' + line
323
 
                partline = re.sub('\r', '\\\\r', partline)
 
324
                    line = b'  ' + line
 
325
                partline = re.sub(b'\r', b'\\\\r', partline)
324
326
                blank_line = False
325
327
                if len(line) > 0:
326
 
                    partline += '\\'
327
 
                elif re.search(' $', partline):
328
 
                    partline += '\\'
 
328
                    partline += b'\\'
 
329
                elif re.search(b' $', partline):
 
330
                    partline += b'\\'
329
331
                    blank_line = True
330
 
                lines.append('# ' + partline + '\n')
 
332
                lines.append(b'# ' + partline + b'\n')
331
333
                if blank_line:
332
 
                    lines.append('#   \n')
 
334
                    lines.append(b'#   \n')
333
335
    return lines
334
336
 
335
337
 
336
338
def _patch_stanza_iter(line_iter):
337
 
    map = {'\\\\': '\\',
338
 
           '\\r' : '\r',
339
 
           '\\\n': ''}
 
339
    map = {b'\\\\': b'\\',
 
340
           b'\\r' : b'\r',
 
341
           b'\\\n': b''}
340
342
    def mapget(match):
341
343
        return map[match.group(0)]
342
344
 
343
345
    last_line = None
344
346
    for line in line_iter:
345
 
        if line.startswith('# '):
 
347
        if line.startswith(b'# '):
346
348
            line = line[2:]
347
 
        elif line.startswith('#'):
 
349
        elif line.startswith(b'#'):
348
350
            line = line[1:]
349
351
        else:
350
352
            raise ValueError("bad line %r" % (line,))
351
353
        if last_line is not None and len(line) > 2:
352
354
            line = line[2:]
353
 
        line = re.sub('\r', '', line)
354
 
        line = re.sub('\\\\(.|\n)', mapget, line)
 
355
        line = re.sub(b'\r', b'', line)
 
356
        line = re.sub(b'\\\\(.|\n)', mapget, line)
355
357
        if last_line is None:
356
358
            last_line = line
357
359
        else:
358
360
            last_line += line
359
 
        if last_line[-1] == '\n':
 
361
        if last_line[-1:] == b'\n':
360
362
            yield last_line
361
363
            last_line = None
362
364
    if last_line is not None: