35
from bzrlib import osutils
36
from bzrlib.iterablefile import IterableFile
36
from .iterablefile import IterableFile
38
38
# XXX: some redundancy is allowing to write stanzas in isolation as well as
39
39
# through a writer object.
41
42
class RioWriter(object):
42
44
def __init__(self, to_file):
43
45
self._soft_nl = False
44
46
self._to_file = to_file
46
48
def write_stanza(self, stanza):
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
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):
126
## elif isinstance(value, (int, long)):
127
## value = str(value) # XXX: python2.4 without L-suffix
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
174
for tag, value in self.items:
176
result.append(tag + ': \n')
185
for text_tag, text_value in self.items:
186
tag = text_tag.encode('ascii')
187
value = text_value.encode('utf-8', 'surrogateescape')
189
result.append(tag + b': \n')
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')
184
result.append(tag + ': ' + value.encode('utf-8') + '\n')
197
result.append(tag + b': ' + value + b'\n')
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())
191
204
def to_unicode(self):
192
205
"""Return stanza as a single Unicode string.
299
312
max_rio_width = max_width - 4
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' ']:
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)
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:
320
partline = re.sub('\r', '\\\\r', partline)
333
partline = re.sub(b'\r', b'\\\\r', partline)
321
334
blank_line = False
322
335
if len(line) > 0:
324
elif re.search(' $', partline):
337
elif re.search(b' $', partline):
326
339
blank_line = True
327
lines.append('# ' + partline + '\n')
340
lines.append(b'# ' + partline + b'\n')
342
lines.append(b'# \n')
333
346
def _patch_stanza_iter(line_iter):
347
map = {b'\\\\': b'\\',
337
351
def mapget(match):
338
352
return map[match.group(0)]
341
355
for line in line_iter:
342
if line.startswith('# '):
356
if line.startswith(b'# '):
344
elif line.startswith('#'):
358
elif line.startswith(b'#'):
347
361
raise ValueError("bad line %r" % (line,))
348
362
if last_line is not None and len(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:
355
369
last_line += line
356
if last_line[-1] == '\n':
370
if last_line[-1:] == b'\n':
359
373
if last_line is not None: