117
117
def add(self, tag, value):
118
118
"""Append a name and value to the stanza."""
119
if not valid_tag(tag):
120
raise ValueError("invalid tag %r" % (tag,))
119
assert valid_tag(tag), \
120
("invalid tag %r" % tag)
121
121
if isinstance(value, str):
122
122
value = unicode(value)
123
123
elif isinstance(value, unicode):
302
306
raise ValueError('tag/value separator not found in line %r'
304
308
tag = str(line[:colon_index])
305
if not valid_tag(tag):
306
raise ValueError("invalid rio tag %r" % (tag,))
309
assert valid_tag(tag), \
310
"invalid rio tag %r" % tag
307
311
accum_value = line[colon_index+2:-1]
309
313
if tag is not None: # add last tag-value
312
316
else: # didn't see any content
316
def to_patch_lines(stanza, max_width=72):
317
"""Convert a stanza into RIO-Patch format lines.
319
RIO-Patch is a RIO variant designed to be e-mailed as part of a patch.
320
It resists common forms of damage such as newline conversion or the removal
321
of trailing whitespace, yet is also reasonably easy to read.
323
:param max_width: The maximum number of characters per physical line.
324
:return: a list of lines
327
raise ValueError(max_width)
328
max_rio_width = max_width - 4
330
for pline in stanza.to_lines():
331
for line in pline.split('\n')[:-1]:
332
line = re.sub('\\\\', '\\\\\\\\', line)
334
partline = line[:max_rio_width]
335
line = line[max_rio_width:]
336
if len(line) > 0 and line[0] != [' ']:
338
break_index = partline.rfind(' ', -20)
340
break_index = partline.rfind('-', -20)
343
break_index = partline.rfind('/', -20)
345
line = partline[break_index:] + line
346
partline = partline[:break_index]
349
partline = re.sub('\r', '\\\\r', partline)
353
elif re.search(' $', partline):
356
lines.append('# ' + partline + '\n')
362
def _patch_stanza_iter(line_iter):
367
return map[match.group(0)]
370
for line in line_iter:
371
if line.startswith('# '):
373
elif line.startswith('#'):
376
raise ValueError("bad line %r" % (line,))
377
if last_line is not None and len(line) > 2:
379
line = re.sub('\r', '', line)
380
line = re.sub('\\\\(.|\n)', mapget, line)
381
if last_line is None:
385
if last_line[-1] == '\n':
388
if last_line is not None:
392
def read_patch_stanza(line_iter):
393
"""Convert an iterable of RIO-Patch format lines into a Stanza.
395
RIO-Patch is a RIO variant designed to be e-mailed as part of a patch.
396
It resists common forms of damage such as newline conversion or the removal
397
of trailing whitespace, yet is also reasonably easy to read.
401
return read_stanza(_patch_stanza_iter(line_iter))