/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/rio.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-22 19:37:57 UTC
  • mto: This revision was merged to the branch mainline in revision 2032.
  • Revision ID: john@arbash-meinel.com-20060922193757-a8341ac119f0d33c
Create a 'read_stanza_unicode' to handle unicode processing

Show diffs side-by-side

added added

removed removed

Lines of Context:
245
245
 
246
246
    The raw lines must be in utf-8 encoding.
247
247
    """
248
 
    items = []
 
248
    unicode_iter = (line.decode('utf-8') for line in line_iter)
 
249
    return read_stanza_unicode(unicode_iter)
 
250
 
 
251
 
 
252
def read_stanza_unicode(unicode_iter):
 
253
    """Read a Stanza from a list of lines or a file.
 
254
 
 
255
    The lines should already be in unicode form. This returns a single
 
256
    stanza that was read. If there is a blank line at the end of the Stanza,
 
257
    it is consumed. It is not an error for there to be no blank line at
 
258
    the end of the iterable. If there is a blank line at the beginning,
 
259
    this is treated as an empty Stanza and None is returned.
 
260
 
 
261
    Only the stanza lines and the trailing blank (if any) are consumed
 
262
    from the unicode_iter
 
263
 
 
264
    :param unicode_iter: A iterable, yeilding Unicode strings. See read_stanza
 
265
        if you have a utf-8 encoded string.
 
266
    :return: A Stanza object if there are any lines in the file.
 
267
        None otherwise
 
268
    """
249
269
    stanza = Stanza()
250
270
    tag = None
251
271
    accum_value = None
252
 
    for line in line_iter:
 
272
    
 
273
    # TODO: jam 20060922 This code should raise real errors rather than
 
274
    #       using 'assert' to process user input, or raising ValueError
 
275
    #       rather than a more specific error.
 
276
 
 
277
    for line in unicode_iter:
253
278
        if line is None or line == '':
254
279
            break       # end of file
255
280
        if line == '\n':
256
281
            break       # end of stanza
257
 
        if not isinstance(line, unicode):
258
 
            line = line.decode('utf-8')
259
 
        assert line[-1] == '\n'
 
282
        assert line.endswith('\n')
260
283
        real_l = line
261
284
        if line[0] == '\t': # continues previous value
262
285
            if tag is None:
268
291
            try:
269
292
                colon_index = line.index(': ')
270
293
            except ValueError:
271
 
                raise ValueError('tag/value separator not found in line %r' % real_l)
 
294
                raise ValueError('tag/value separator not found in line %r'
 
295
                                 % real_l)
272
296
            tag = str(line[:colon_index])
273
297
            assert valid_tag(tag), \
274
298
                    "invalid rio tag %r" % tag
275
299
            accum_value = line[colon_index+2:-1]
 
300
 
276
301
    if tag is not None: # add last tag-value
277
302
        stanza.add(tag, accum_value)
278
303
        return stanza