~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/rio.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-22 05:29:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2032.
  • Revision ID: john@arbash-meinel.com-20060922052938-8a82e211a1b7540a
Make it easier to nest Stanzas with Unicode contents

Show diffs side-by-side

added added

removed removed

Lines of Context:
171
171
        """Return stanza as a single string"""
172
172
        return ''.join(self.to_lines())
173
173
 
 
174
    def to_unicode(self):
 
175
        """Return stanza as a single Unicode string.
 
176
 
 
177
        This is most useful when adding a Stanza to a parent Stanza
 
178
        """
 
179
        if not self.items:
 
180
            return u''
 
181
 
 
182
        result = []
 
183
        for tag, value in self.items:
 
184
            if value == '':
 
185
                result.append(tag + ': \n')
 
186
            elif '\n' in value:
 
187
                # don't want splitlines behaviour on empty lines
 
188
                val_lines = value.split('\n')
 
189
                result.append(tag + ': ' + val_lines[0] + '\n')
 
190
                for line in val_lines[1:]:
 
191
                    result.append('\t' + line + '\n')
 
192
            else:
 
193
                result.append(tag + ': ' + value + '\n')
 
194
        return u''.join(result)
 
195
 
174
196
    def write(self, to_file):
175
197
        """Write stanza to a file"""
176
198
        to_file.writelines(self.to_lines())
232
254
            break       # end of file
233
255
        if line == '\n':
234
256
            break       # end of stanza
235
 
        line = line.decode('utf-8')
 
257
        if not isinstance(line, unicode):
 
258
            line = line.decode('utf-8')
236
259
        assert line[-1] == '\n'
237
260
        real_l = line
238
261
        if line[0] == '\t': # continues previous value