~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/rio.py

  • Committer: Martin Pool
  • Date: 2006-02-22 04:29:54 UTC
  • mfrom: (1566 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1569.
  • Revision ID: mbp@sourcefrog.net-20060222042954-60333f08dd56a646
[merge] from bzr.dev before integration
Fix undefined ordering in sign_my_revisions breaking tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
import re
22
22
 
23
 
from bzrlib.iterablefile import IterableFile
24
 
 
25
23
# XXX: some redundancy is allowing to write stanzas in isolation as well as
26
24
# through a writer object.  
27
25
 
54
52
            else:
55
53
                yield s
56
54
 
57
 
 
58
 
def rio_file(stanzas, header=None):
59
 
    """Produce a rio IterableFile from an iterable of stanzas"""
60
 
    def str_iter():
61
 
        if header is not None:
62
 
            yield header + '\n'
63
 
        first_stanza = True
64
 
        for s in stanzas:
65
 
            if first_stanza is not True:
66
 
                yield '\n'
67
 
            for line in s.to_lines():
68
 
                yield line
69
 
            first_stanza = False
70
 
    return IterableFile(str_iter())
71
 
 
72
 
 
73
55
def read_stanzas(from_file):
74
56
    while True:
75
57
        s = read_stanza(from_file)
106
88
        """Append a name and value to the stanza."""
107
89
        assert valid_tag(tag), \
108
90
            ("invalid tag %r" % tag)
109
 
        if isinstance(value, str):
110
 
            value = unicode(value)
111
 
        elif isinstance(value, unicode):
 
91
        if isinstance(value, (str, unicode)):
112
92
            pass
113
93
        ## elif isinstance(value, (int, long)):
114
94
        ##    value = str(value)           # XXX: python2.4 without L-suffix
144
124
        return iter(self.items)
145
125
 
146
126
    def to_lines(self):
147
 
        """Generate sequence of lines for external version of this file.
148
 
        
149
 
        The lines are always utf-8 encoded strings.
150
 
        """
 
127
        """Generate sequence of lines for external version of this file."""
151
128
        if not self.items:
152
129
            # max() complains if sequence is empty
153
130
            return []
154
131
        result = []
155
132
        for tag, value in self.items:
156
 
            assert isinstance(tag, str), type(tag)
157
 
            assert isinstance(value, unicode)
 
133
            assert isinstance(value, (str, unicode))
158
134
            if value == '':
159
135
                result.append(tag + ': \n')
160
136
            elif '\n' in value:
161
137
                # don't want splitlines behaviour on empty lines
162
138
                val_lines = value.split('\n')
163
 
                result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
 
139
                result.append(tag + ': ' + val_lines[0] + '\n')
164
140
                for line in val_lines[1:]:
165
 
                    result.append('\t' + line.encode('utf-8') + '\n')
 
141
                    result.append('\t' + line + '\n')
166
142
            else:
167
 
                result.append(tag + ': ' + value.encode('utf-8') + '\n')
 
143
                result.append(tag + ': ' + value + '\n')
168
144
        return result
169
145
 
170
146
    def to_string(self):
220
196
 
221
197
    Only the stanza lines and the trailing blank (if any) are consumed
222
198
    from the line_iter.
223
 
 
224
 
    The raw lines must be in utf-8 encoding.
225
199
    """
226
200
    items = []
227
201
    stanza = Stanza()
232
206
            break       # end of file
233
207
        if line == '\n':
234
208
            break       # end of stanza
235
 
        line = line.decode('utf-8')
236
209
        assert line[-1] == '\n'
237
210
        real_l = line
238
211
        if line[0] == '\t': # continues previous value
246
219
                colon_index = line.index(': ')
247
220
            except ValueError:
248
221
                raise ValueError('tag/value separator not found in line %r' % real_l)
249
 
            tag = str(line[:colon_index])
 
222
            tag = line[:colon_index]
250
223
            assert valid_tag(tag), \
251
224
                    "invalid rio tag %r" % tag
252
225
            accum_value = line[colon_index+2:-1]