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