129
129
% (value, type(value)))
130
130
self.items.append((tag, value))
133
def from_pairs(cls, pairs):
132
138
def __contains__(self, find_tag):
133
139
"""True if there is any field in this stanza with the given tag."""
134
140
for tag, value in self.items:
193
199
for tag, value in self.items:
195
result.append(tag + ': \n')
201
result.append(tag + u': \n')
197
203
# don't want splitlines behaviour on empty lines
198
val_lines = value.split('\n')
199
result.append(tag + ': ' + val_lines[0] + '\n')
204
val_lines = value.split(u'\n')
205
result.append(tag + u': ' + val_lines[0] + u'\n')
200
206
for line in val_lines[1:]:
201
result.append('\t' + line + '\n')
207
result.append(u'\t' + line + u'\n')
203
result.append(tag + ': ' + value + '\n')
209
result.append(tag + u': ' + value + u'\n')
204
210
return u''.join(result)
206
212
def write(self, to_file):
255
261
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)
263
return _read_stanza_utf8(line_iter)
261
266
def read_stanza_unicode(unicode_iter):
275
280
: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
283
return _read_stanza_unicode(unicode_iter)
316
285
def to_patch_lines(stanza, max_width=72):
317
286
"""Convert a stanza into RIO-Patch format lines.