36
from .iterablefile import IterableFile
35
from bzrlib import osutils
36
from bzrlib.iterablefile import IterableFile
38
38
# XXX: some redundancy is allowing to write stanzas in isolation as well as
39
39
# through a writer object.
42
41
class RioWriter(object):
44
42
def __init__(self, to_file):
45
43
self._soft_nl = False
46
44
self._to_file = to_file
48
46
def write_stanza(self, stanza):
50
self._to_file.write(b'\n')
48
self._to_file.write('\n')
51
49
stanza.write(self._to_file)
52
50
self._soft_nl = True
132
119
"""Append a name and value to the stanza."""
133
120
if not valid_tag(tag):
134
121
raise ValueError("invalid tag %r" % (tag,))
135
if isinstance(value, bytes):
136
value = value.decode('ascii')
137
elif isinstance(value, str):
122
if isinstance(value, str):
123
value = unicode(value)
124
elif isinstance(value, unicode):
126
## elif isinstance(value, (int, long)):
127
## value = str(value) # XXX: python2.4 without L-suffix
140
129
raise TypeError("invalid type for rio value: %r of type %s"
141
130
% (value, type(value)))
182
171
# max() complains if sequence is empty
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')
174
for tag, value in self.items:
176
result.append(tag + ': \n')
191
178
# don't want splitlines behaviour on empty lines
192
val_lines = value.split(b'\n')
193
result.append(tag + b': ' + val_lines[0] + b'\n')
179
val_lines = value.split('\n')
180
result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
194
181
for line in val_lines[1:]:
195
result.append(b'\t' + line + b'\n')
182
result.append('\t' + line.encode('utf-8') + '\n')
197
result.append(tag + b': ' + value + b'\n')
184
result.append(tag + ': ' + value.encode('utf-8') + '\n')
200
187
def to_string(self):
201
188
"""Return stanza as a single string"""
202
return b''.join(self.to_lines())
189
return ''.join(self.to_lines())
204
191
def to_unicode(self):
205
192
"""Return stanza as a single Unicode string.
312
299
max_rio_width = max_width - 4
314
301
for pline in stanza.to_lines():
315
for line in pline.split(b'\n')[:-1]:
316
line = re.sub(b'\\\\', b'\\\\\\\\', line)
302
for line in pline.split('\n')[:-1]:
303
line = re.sub('\\\\', '\\\\\\\\', line)
317
304
while len(line) > 0:
318
305
partline = line[:max_rio_width]
319
306
line = line[max_rio_width:]
320
if len(line) > 0 and line[:1] != [b' ']:
307
if len(line) > 0 and line[0] != [' ']:
322
break_index = partline.rfind(b' ', -20)
309
break_index = partline.rfind(' ', -20)
323
310
if break_index < 3:
324
break_index = partline.rfind(b'-', -20)
311
break_index = partline.rfind('-', -20)
326
313
if break_index < 3:
327
break_index = partline.rfind(b'/', -20)
314
break_index = partline.rfind('/', -20)
328
315
if break_index >= 3:
329
316
line = partline[break_index:] + line
330
317
partline = partline[:break_index]
331
318
if len(line) > 0:
333
partline = re.sub(b'\r', b'\\\\r', partline)
320
partline = re.sub('\r', '\\\\r', partline)
334
321
blank_line = False
335
322
if len(line) > 0:
337
elif re.search(b' $', partline):
324
elif re.search(' $', partline):
339
326
blank_line = True
340
lines.append(b'# ' + partline + b'\n')
327
lines.append('# ' + partline + '\n')
342
lines.append(b'# \n')
346
333
def _patch_stanza_iter(line_iter):
347
map = {b'\\\\': b'\\',
351
337
def mapget(match):
352
338
return map[match.group(0)]
355
341
for line in line_iter:
356
if line.startswith(b'# '):
342
if line.startswith('# '):
358
elif line.startswith(b'#'):
344
elif line.startswith('#'):
361
347
raise ValueError("bad line %r" % (line,))
362
348
if last_line is not None and len(line) > 2:
364
line = re.sub(b'\r', b'', line)
365
line = re.sub(b'\\\\(.|\n)', mapget, line)
350
line = re.sub('\r', '', line)
351
line = re.sub('\\\\(.|\n)', mapget, line)
366
352
if last_line is None:
369
355
last_line += line
370
if last_line[-1:] == b'\n':
356
if last_line[-1] == '\n':
373
359
if last_line is not None: