1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from bzrlib.rio import (
23
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$')
25
return bool(_tag_re.match(tag))
28
def _read_stanza_utf8(line_iter):
29
unicode_iter = (line.decode('utf-8') for line in line_iter)
30
return _read_stanza_unicode(unicode_iter)
33
def _read_stanza_unicode(unicode_iter):
38
# TODO: jam 20060922 This code should raise real errors rather than
39
# using 'assert' to process user input, or raising ValueError
40
# rather than a more specific error.
41
for line in unicode_iter:
42
if line is None or line == '':
47
if line[0] == '\t': # continues previous value
49
raise ValueError('invalid continuation line %r' % real_l)
50
accum_value += '\n' + line[1:-1]
51
else: # new tag:value line
53
stanza.add(tag, accum_value)
55
colon_index = line.index(': ')
57
raise ValueError('tag/value separator not found in line %r'
59
tag = str(line[:colon_index])
60
if not _valid_tag(tag):
61
raise ValueError("invalid rio tag %r" % (tag,))
62
accum_value = line[colon_index+2:-1]
64
if tag is not None: # add last tag-value
65
stanza.add(tag, accum_value)
67
else: # didn't see any content