/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

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
# \subsection{\emph{rio} - simple text metaformat}
18
20
#
19
21
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
34
36
 
35
37
from . import osutils
36
38
from .iterablefile import IterableFile
 
39
from .sixish import (
 
40
    text_type,
 
41
    )
37
42
 
38
43
# XXX: some redundancy is allowing to write stanzas in isolation as well as
39
44
# through a writer object.
40
45
 
41
 
 
42
46
class RioWriter(object):
43
 
 
44
47
    def __init__(self, to_file):
45
48
        self._soft_nl = False
46
49
        self._to_file = to_file
47
50
 
48
51
    def write_stanza(self, stanza):
49
52
        if self._soft_nl:
50
 
            self._to_file.write(b'\n')
 
53
            self._to_file.write('\n')
51
54
        stanza.write(self._to_file)
52
55
        self._soft_nl = True
53
56
 
58
61
    to_file can be anything that can be enumerated as a sequence of
59
62
    lines (with newlines.)
60
63
    """
61
 
 
62
64
    def __init__(self, from_file):
63
65
        self._from_file = from_file
64
66
 
87
89
 
88
90
 
89
91
def read_stanzas(from_file):
90
 
 
91
92
    while True:
92
93
        s = read_stanza(from_file)
93
94
        if s is None:
94
95
            break
95
 
        yield s
96
 
 
97
 
 
98
 
def read_stanzas_unicode(from_file):
99
 
 
100
 
    while True:
101
 
        s = read_stanza_unicode(from_file)
102
 
        if s is None:
103
 
            break
104
 
        yield s
105
 
 
 
96
        else:
 
97
            yield s
106
98
 
107
99
class Stanza(object):
108
100
    """One stanza for rio.
134
126
            raise ValueError("invalid tag %r" % (tag,))
135
127
        if isinstance(value, bytes):
136
128
            value = value.decode('ascii')
137
 
        elif isinstance(value, str):
 
129
        elif isinstance(value, text_type):
138
130
            pass
139
131
        else:
140
132
            raise TypeError("invalid type for rio value: %r of type %s"
184
176
        result = []
185
177
        for text_tag, text_value in self.items:
186
178
            tag = text_tag.encode('ascii')
187
 
            value = text_value.encode('utf-8', 'surrogateescape')
 
179
            value = text_value.encode('utf-8')
188
180
            if value == b'':
189
181
                result.append(tag + b': \n')
190
182
            elif b'\n' in value:
345
337
 
346
338
def _patch_stanza_iter(line_iter):
347
339
    map = {b'\\\\': b'\\',
348
 
           b'\\r': b'\r',
 
340
           b'\\r' : b'\r',
349
341
           b'\\\n': b''}
350
 
 
351
342
    def mapget(match):
352
343
        return map[match.group(0)]
353
344
 
395
386
except ImportError as e:
396
387
    osutils.failed_to_load_extension(e)
397
388
    from ._rio_py import (
398
 
        _read_stanza_utf8,
399
 
        _read_stanza_unicode,
400
 
        _valid_tag,
401
 
        )
 
389
       _read_stanza_utf8,
 
390
       _read_stanza_unicode,
 
391
       _valid_tag,
 
392
       )