/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: 2017-06-10 12:50:32 UTC
  • mfrom: (6679 work)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610125032-xb5rd5fjskjallos
Merge trunk.

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'.
32
34
 
33
35
import re
34
36
 
35
 
from bzrlib import osutils
36
 
from bzrlib.iterablefile import IterableFile
 
37
from . import osutils
 
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.
72
77
    """Produce a rio IterableFile from an iterable of stanzas"""
73
78
    def str_iter():
74
79
        if header is not None:
75
 
            yield header + '\n'
 
80
            yield header + b'\n'
76
81
        first_stanza = True
77
82
        for s in stanzas:
78
83
            if first_stanza is not True:
79
 
                yield '\n'
 
84
                yield b'\n'
80
85
            for line in s.to_lines():
81
86
                yield line
82
87
            first_stanza = False
119
124
        """Append a name and value to the stanza."""
120
125
        if not valid_tag(tag):
121
126
            raise ValueError("invalid tag %r" % (tag,))
122
 
        if isinstance(value, str):
123
 
            value = unicode(value)
124
 
        elif isinstance(value, unicode):
 
127
        if isinstance(value, bytes):
 
128
            value = value.decode('ascii')
 
129
        elif isinstance(value, text_type):
125
130
            pass
126
 
        ## elif isinstance(value, (int, long)):
127
 
        ##    value = str(value)           # XXX: python2.4 without L-suffix
128
131
        else:
129
132
            raise TypeError("invalid type for rio value: %r of type %s"
130
133
                            % (value, type(value)))
173
176
        result = []
174
177
        for tag, value in self.items:
175
178
            if value == '':
176
 
                result.append(tag + ': \n')
 
179
                result.append(tag.encode('ascii') + b': \n')
177
180
            elif '\n' in value:
178
181
                # don't want splitlines behaviour on empty lines
179
182
                val_lines = value.split('\n')
180
 
                result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
 
183
                result.append(tag + b': ' + val_lines[0].encode('utf-8') + b'\n')
181
184
                for line in val_lines[1:]:
182
 
                    result.append('\t' + line.encode('utf-8') + '\n')
 
185
                    result.append(b'\t' + line.encode('utf-8') + b'\n')
183
186
            else:
184
 
                result.append(tag + ': ' + value.encode('utf-8') + '\n')
 
187
                result.append(tag.encode('ascii') + b': ' + value.encode('utf-8') + b'\n')
185
188
        return result
186
189
 
187
190
    def to_string(self):
188
191
        """Return stanza as a single string"""
189
 
        return ''.join(self.to_lines())
 
192
        return b''.join(self.to_lines())
190
193
 
191
194
    def to_unicode(self):
192
195
        """Return stanza as a single Unicode string.
373
376
 
374
377
 
375
378
try:
376
 
    from bzrlib._rio_pyx import (
 
379
    from ._rio_pyx import (
377
380
        _read_stanza_utf8,
378
381
        _read_stanza_unicode,
379
382
        _valid_tag,
380
383
        )
381
 
except ImportError, e:
 
384
except ImportError as e:
382
385
    osutils.failed_to_load_extension(e)
383
 
    from bzrlib._rio_py import (
 
386
    from ._rio_py import (
384
387
       _read_stanza_utf8,
385
388
       _read_stanza_unicode,
386
389
       _valid_tag,