14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
17
19
# \subsection{\emph{rio} - simple text metaformat}
19
21
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
35
from bzrlib import osutils
36
from bzrlib.iterablefile import IterableFile
38
from .iterablefile import IterableFile
38
43
# XXX: some redundancy is allowing to write stanzas in isolation as well as
39
44
# through a writer object.
41
46
class RioWriter(object):
42
48
def __init__(self, to_file):
43
49
self._soft_nl = False
44
50
self._to_file = to_file
46
52
def write_stanza(self, stanza):
48
self._to_file.write('\n')
54
self._to_file.write(b'\n')
49
55
stanza.write(self._to_file)
50
56
self._soft_nl = True
119
139
"""Append a name and value to the stanza."""
120
140
if not valid_tag(tag):
121
141
raise ValueError("invalid tag %r" % (tag,))
122
if isinstance(value, str):
123
value = unicode(value)
124
elif isinstance(value, unicode):
142
if isinstance(value, bytes):
143
value = value.decode('ascii')
144
elif isinstance(value, text_type):
126
## elif isinstance(value, (int, long)):
127
## value = str(value) # XXX: python2.4 without L-suffix
129
147
raise TypeError("invalid type for rio value: %r of type %s"
130
148
% (value, type(value)))
171
189
# max() complains if sequence is empty
174
for tag, value in self.items:
176
result.append(tag + ': \n')
192
for text_tag, text_value in self.items:
193
tag = text_tag.encode('ascii')
194
value = text_value.encode('utf-8')
196
result.append(tag + b': \n')
178
198
# 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')
199
val_lines = value.split(b'\n')
200
result.append(tag + b': ' + val_lines[0] + b'\n')
181
201
for line in val_lines[1:]:
182
result.append('\t' + line.encode('utf-8') + '\n')
202
result.append(b'\t' + line + b'\n')
184
result.append(tag + ': ' + value.encode('utf-8') + '\n')
204
result.append(tag + b': ' + value + b'\n')
187
207
def to_string(self):
188
208
"""Return stanza as a single string"""
189
return ''.join(self.to_lines())
209
return b''.join(self.to_lines())
191
211
def to_unicode(self):
192
212
"""Return stanza as a single Unicode string.
299
319
max_rio_width = max_width - 4
301
321
for pline in stanza.to_lines():
302
for line in pline.split('\n')[:-1]:
303
line = re.sub('\\\\', '\\\\\\\\', line)
322
for line in pline.split(b'\n')[:-1]:
323
line = re.sub(b'\\\\', b'\\\\\\\\', line)
304
324
while len(line) > 0:
305
325
partline = line[:max_rio_width]
306
326
line = line[max_rio_width:]
307
if len(line) > 0 and line[0] != [' ']:
327
if len(line) > 0 and line[:1] != [b' ']:
309
break_index = partline.rfind(' ', -20)
329
break_index = partline.rfind(b' ', -20)
310
330
if break_index < 3:
311
break_index = partline.rfind('-', -20)
331
break_index = partline.rfind(b'-', -20)
313
333
if break_index < 3:
314
break_index = partline.rfind('/', -20)
334
break_index = partline.rfind(b'/', -20)
315
335
if break_index >= 3:
316
336
line = partline[break_index:] + line
317
337
partline = partline[:break_index]
318
338
if len(line) > 0:
320
partline = re.sub('\r', '\\\\r', partline)
340
partline = re.sub(b'\r', b'\\\\r', partline)
321
341
blank_line = False
322
342
if len(line) > 0:
324
elif re.search(' $', partline):
344
elif re.search(b' $', partline):
326
346
blank_line = True
327
lines.append('# ' + partline + '\n')
347
lines.append(b'# ' + partline + b'\n')
349
lines.append(b'# \n')
333
353
def _patch_stanza_iter(line_iter):
354
map = {b'\\\\': b'\\',
337
357
def mapget(match):
338
358
return map[match.group(0)]
341
361
for line in line_iter:
342
if line.startswith('# '):
362
if line.startswith(b'# '):
344
elif line.startswith('#'):
364
elif line.startswith(b'#'):
347
367
raise ValueError("bad line %r" % (line,))
348
368
if last_line is not None and len(line) > 2:
350
line = re.sub('\r', '', line)
351
line = re.sub('\\\\(.|\n)', mapget, line)
370
line = re.sub(b'\r', b'', line)
371
line = re.sub(b'\\\\(.|\n)', mapget, line)
352
372
if last_line is None:
355
375
last_line += line
356
if last_line[-1] == '\n':
376
if last_line[-1:] == b'\n':
359
379
if last_line is not None: