/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 breezy/_rio_py.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-19 23:18:42 UTC
  • mto: (7490.3.4 work)
  • mto: This revision was merged to the branch mainline in revision 7495.
  • Revision ID: jelmer@jelmer.uk-20200219231842-agwjh2db66cpajqg
Consistent return values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
"""Python implementation of _read_stanza_*."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import re
 
22
 
 
23
from .rio import (
 
24
    Stanza,
 
25
    )
 
26
 
 
27
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$')
 
28
 
 
29
 
 
30
def _valid_tag(tag):
 
31
    if not isinstance(tag, str):
 
32
        raise TypeError(tag)
 
33
    return bool(_tag_re.match(tag))
 
34
 
 
35
 
 
36
def _read_stanza_utf8(line_iter):
 
37
    def iter_unicode_lines():
 
38
        for line in line_iter:
 
39
            if not isinstance(line, bytes):
 
40
                raise TypeError(line)
 
41
            yield line.decode('utf-8')
 
42
    return _read_stanza_unicode(iter_unicode_lines())
 
43
 
 
44
 
 
45
def _read_stanza_unicode(unicode_iter):
 
46
    stanza = Stanza()
 
47
    tag = None
 
48
    accum_value = None
 
49
 
 
50
    # TODO: jam 20060922 This code should raise real errors rather than
 
51
    #       using 'assert' to process user input, or raising ValueError
 
52
    #       rather than a more specific error.
 
53
 
 
54
    for line in unicode_iter:
 
55
        if line is None or line == u'':
 
56
            break       # end of file
 
57
        if line == u'\n':
 
58
            break       # end of stanza
 
59
        real_l = line
 
60
        if line[0] == u'\t':  # continues previous value
 
61
            if tag is None:
 
62
                raise ValueError('invalid continuation line %r' % real_l)
 
63
            accum_value.append(u'\n' + line[1:-1])
 
64
        else:  # new tag:value line
 
65
            if tag is not None:
 
66
                stanza.add(tag, u''.join(accum_value))
 
67
            try:
 
68
                colon_index = line.index(u': ')
 
69
            except ValueError:
 
70
                raise ValueError('tag/value separator not found in line %r'
 
71
                                 % real_l)
 
72
            tag = str(line[:colon_index])
 
73
            if not _valid_tag(tag):
 
74
                raise ValueError("invalid rio tag %r" % (tag,))
 
75
            accum_value = [line[colon_index + 2:-1]]
 
76
 
 
77
    if tag is not None:  # add last tag-value
 
78
        stanza.add(tag, u''.join(accum_value))
 
79
        return stanza
 
80
    else:     # didn't see any content
 
81
        return None