bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4354.3.1
by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py. |
1 |
# Copyright (C) 2009 Canonical Ltd
|
2 |
#
|
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
|
4354.3.2
by Jelmer Vernooij
Provide custom implementation of _read_stanza_utf8 in Pyrex. |
17 |
"""Python implementation of _read_stanza_*."""
|
18 |
||
|
4354.3.1
by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py. |
19 |
import re |
20 |
||
21 |
from bzrlib.rio import ( |
|
22 |
Stanza, |
|
23 |
)
|
|
24 |
||
25 |
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$') |
|
26 |
def _valid_tag(tag): |
|
27 |
return bool(_tag_re.match(tag)) |
|
28 |
||
29 |
||
30 |
def _read_stanza_utf8(line_iter): |
|
31 |
unicode_iter = (line.decode('utf-8') for line in line_iter) |
|
32 |
return _read_stanza_unicode(unicode_iter) |
|
33 |
||
34 |
||
35 |
def _read_stanza_unicode(unicode_iter): |
|
36 |
stanza = Stanza() |
|
37 |
tag = None |
|
38 |
accum_value = None |
|
39 |
||
40 |
# TODO: jam 20060922 This code should raise real errors rather than
|
|
41 |
# using 'assert' to process user input, or raising ValueError
|
|
42 |
# rather than a more specific error.
|
|
43 |
for line in unicode_iter: |
|
44 |
if line is None or line == '': |
|
45 |
break # end of file |
|
46 |
if line == '\n': |
|
47 |
break # end of stanza |
|
48 |
real_l = line |
|
49 |
if line[0] == '\t': # continues previous value |
|
50 |
if tag is None: |
|
51 |
raise ValueError('invalid continuation line %r' % real_l) |
|
52 |
accum_value += '\n' + line[1:-1] |
|
53 |
else: # new tag:value line |
|
54 |
if tag is not None: |
|
55 |
stanza.add(tag, accum_value) |
|
56 |
try: |
|
57 |
colon_index = line.index(': ') |
|
58 |
except ValueError: |
|
59 |
raise ValueError('tag/value separator not found in line %r' |
|
60 |
% real_l) |
|
61 |
tag = str(line[:colon_index]) |
|
62 |
if not _valid_tag(tag): |
|
63 |
raise ValueError("invalid rio tag %r" % (tag,)) |
|
64 |
accum_value = line[colon_index+2:-1] |
|
65 |
||
66 |
if tag is not None: # add last tag-value |
|
67 |
stanza.add(tag, accum_value) |
|
68 |
return stanza |
|
69 |
else: # didn't see any content |
|
70 |
return None |
|
71 |
||
72 |
||
73 |