255
255
The raw lines must be in utf-8 encoding.
257
unicode_iter = (line.decode('utf-8') for line in line_iter)
258
return read_stanza_unicode(unicode_iter)
257
return _read_stanza_utf8(line_iter)
261
260
def read_stanza_unicode(unicode_iter):
275
274
:return: A Stanza object if there are any lines in the file.
282
# TODO: jam 20060922 This code should raise real errors rather than
283
# using 'assert' to process user input, or raising ValueError
284
# rather than a more specific error.
286
for line in unicode_iter:
287
if line is None or line == '':
290
break # end of stanza
292
if line[0] == '\t': # continues previous value
294
raise ValueError('invalid continuation line %r' % real_l)
295
accum_value += '\n' + line[1:-1]
296
else: # new tag:value line
298
stanza.add(tag, accum_value)
300
colon_index = line.index(': ')
302
raise ValueError('tag/value separator not found in line %r'
304
tag = str(line[:colon_index])
305
if not valid_tag(tag):
306
raise ValueError("invalid rio tag %r" % (tag,))
307
accum_value = line[colon_index+2:-1]
309
if tag is not None: # add last tag-value
310
stanza.add(tag, accum_value)
312
else: # didn't see any content
277
return _read_stanza_unicode(unicode_iter)
316
279
def to_patch_lines(stanza, max_width=72):
317
280
"""Convert a stanza into RIO-Patch format lines.