124
121
"""Append a name and value to the stanza."""
125
122
if not valid_tag(tag):
126
123
raise ValueError("invalid tag %r" % (tag,))
127
if isinstance(value, bytes):
128
value = value.decode('ascii')
129
elif isinstance(value, text_type):
124
if isinstance(value, str):
125
value = unicode(value)
126
elif isinstance(value, unicode):
128
## elif isinstance(value, (int, long)):
129
## value = str(value) # XXX: python2.4 without L-suffix
132
131
raise TypeError("invalid type for rio value: %r of type %s"
133
132
% (value, type(value)))
174
173
# max() complains if sequence is empty
177
for text_tag, text_value in self.items:
178
tag = text_tag.encode('ascii')
179
value = text_value.encode('utf-8')
181
result.append(tag + b': \n')
176
for tag, value in self.items:
178
result.append(tag + ': \n')
183
180
# don't want splitlines behaviour on empty lines
184
val_lines = value.split(b'\n')
185
result.append(tag + b': ' + val_lines[0] + b'\n')
181
val_lines = value.split('\n')
182
result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
186
183
for line in val_lines[1:]:
187
result.append(b'\t' + line + b'\n')
184
result.append('\t' + line.encode('utf-8') + '\n')
189
result.append(tag + b': ' + value + b'\n')
186
result.append(tag + ': ' + value.encode('utf-8') + '\n')
192
189
def to_string(self):
193
190
"""Return stanza as a single string"""
194
return b''.join(self.to_lines())
191
return ''.join(self.to_lines())
196
193
def to_unicode(self):
197
194
"""Return stanza as a single Unicode string.
304
301
max_rio_width = max_width - 4
306
303
for pline in stanza.to_lines():
307
for line in pline.split(b'\n')[:-1]:
308
line = re.sub(b'\\\\', b'\\\\\\\\', line)
304
for line in pline.split('\n')[:-1]:
305
line = re.sub('\\\\', '\\\\\\\\', line)
309
306
while len(line) > 0:
310
307
partline = line[:max_rio_width]
311
308
line = line[max_rio_width:]
312
if len(line) > 0 and line[:1] != [b' ']:
309
if len(line) > 0 and line[0] != [' ']:
314
break_index = partline.rfind(b' ', -20)
311
break_index = partline.rfind(' ', -20)
315
312
if break_index < 3:
316
break_index = partline.rfind(b'-', -20)
313
break_index = partline.rfind('-', -20)
318
315
if break_index < 3:
319
break_index = partline.rfind(b'/', -20)
316
break_index = partline.rfind('/', -20)
320
317
if break_index >= 3:
321
318
line = partline[break_index:] + line
322
319
partline = partline[:break_index]
323
320
if len(line) > 0:
325
partline = re.sub(b'\r', b'\\\\r', partline)
322
partline = re.sub('\r', '\\\\r', partline)
326
323
blank_line = False
327
324
if len(line) > 0:
329
elif re.search(b' $', partline):
326
elif re.search(' $', partline):
331
328
blank_line = True
332
lines.append(b'# ' + partline + b'\n')
329
lines.append('# ' + partline + '\n')
334
lines.append(b'# \n')
338
335
def _patch_stanza_iter(line_iter):
339
map = {b'\\\\': b'\\',
342
339
def mapget(match):
343
340
return map[match.group(0)]
346
343
for line in line_iter:
347
if line.startswith(b'# '):
344
if line.startswith('# '):
349
elif line.startswith(b'#'):
346
elif line.startswith('#'):
352
349
raise ValueError("bad line %r" % (line,))
353
350
if last_line is not None and len(line) > 2:
355
line = re.sub(b'\r', b'', line)
356
line = re.sub(b'\\\\(.|\n)', mapget, line)
352
line = re.sub('\r', '', line)
353
line = re.sub('\\\\(.|\n)', mapget, line)
357
354
if last_line is None:
360
357
last_line += line
361
if last_line[-1:] == b'\n':
358
if last_line[-1] == '\n':
364
361
if last_line is not None: