/brz/remove-bazaar

To get this branch, use:
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
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .rio import (
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
22
    Stanza,
23
    )
24
25
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$')
7143.15.2 by Jelmer Vernooij
Run autopep8.
26
27
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
28
def _valid_tag(tag):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
29
    if not isinstance(tag, str):
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
30
        raise TypeError(tag)
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
31
    return bool(_tag_re.match(tag))
32
33
34
def _read_stanza_utf8(line_iter):
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
35
    def iter_unicode_lines():
36
        for line in line_iter:
6677.1.1 by Martin
Go back to native str for urls and many other py3 changes
37
            if not isinstance(line, bytes):
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
38
                raise TypeError(line)
7490.74.1 by Jelmer Vernooij
Use surrogateescape in rio.
39
            yield line.decode('utf-8', 'surrogateescape')
4354.3.15 by Jelmer Vernooij
Extend valid_tags tests a bit, test that stanza pairs contain the right types.
40
    return _read_stanza_unicode(iter_unicode_lines())
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
41
42
43
def _read_stanza_unicode(unicode_iter):
44
    stanza = Stanza()
45
    tag = None
46
    accum_value = None
47
48
    # TODO: jam 20060922 This code should raise real errors rather than
49
    #       using 'assert' to process user input, or raising ValueError
50
    #       rather than a more specific error.
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
51
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
52
    for line in unicode_iter:
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
53
        if line is None or line == u'':
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
54
            break       # end of file
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
55
        if line == u'\n':
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
56
            break       # end of stanza
57
        real_l = line
7143.15.2 by Jelmer Vernooij
Run autopep8.
58
        if line[0] == u'\t':  # continues previous value
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
59
            if tag is None:
60
                raise ValueError('invalid continuation line %r' % real_l)
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
61
            accum_value.append(u'\n' + line[1:-1])
7143.15.2 by Jelmer Vernooij
Run autopep8.
62
        else:  # new tag:value line
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
63
            if tag is not None:
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
64
                stanza.add(tag, u''.join(accum_value))
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
65
            try:
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
66
                colon_index = line.index(u': ')
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
67
            except ValueError:
68
                raise ValueError('tag/value separator not found in line %r'
69
                                 % real_l)
70
            tag = str(line[:colon_index])
71
            if not _valid_tag(tag):
72
                raise ValueError("invalid rio tag %r" % (tag,))
7143.15.2 by Jelmer Vernooij
Run autopep8.
73
            accum_value = [line[colon_index + 2:-1]]
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
74
7143.15.2 by Jelmer Vernooij
Run autopep8.
75
    if tag is not None:  # add last tag-value
4354.3.18 by Jelmer Vernooij
Merge bzr.dev.
76
        stanza.add(tag, u''.join(accum_value))
4354.3.1 by Jelmer Vernooij
Move core RIO parsing functionality to _rio_py.py.
77
        return stanza
78
    else:     # didn't see any content
79
        return None