1
# Copyright (C) 2005 by Canonical Ltd
3
# Distributed under the GNU General Public Licence v2
1
# Copyright (C) 2005 Canonical Ltd
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5
17
# \subsection{\emph{rio} - simple text metaformat}
7
19
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
18
30
# stream representation of an object and vice versa, and that this relation
19
31
# will continue to hold for future versions of bzr.
21
# In comments, $\min(1,10)$
35
from bzrlib.iterablefile import IterableFile
27
37
# XXX: some redundancy is allowing to write stanzas in isolation as well as
28
38
# through a writer object.
70
def rio_file(stanzas, header=None):
71
"""Produce a rio IterableFile from an iterable of stanzas"""
73
if header is not None:
77
if first_stanza is not True:
79
for line in s.to_lines():
82
return IterableFile(str_iter())
59
85
def read_stanzas(from_file):
61
87
s = read_stanza(from_file)
92
118
"""Append a name and value to the stanza."""
93
119
assert valid_tag(tag), \
94
120
("invalid tag %r" % tag)
95
if isinstance(value, (str, unicode)):
121
if isinstance(value, str):
122
value = unicode(value)
123
elif isinstance(value, unicode):
97
125
## elif isinstance(value, (int, long)):
98
126
## value = str(value) # XXX: python2.4 without L-suffix
100
raise ValueError("invalid value %r" % value)
128
raise TypeError("invalid type for rio value: %r of type %s"
129
% (value, type(value)))
101
130
self.items.append((tag, value))
103
132
def __contains__(self, find_tag):
127
156
return iter(self.items)
129
158
def to_lines(self):
130
"""Generate sequence of lines for external version of this file."""
159
"""Generate sequence of lines for external version of this file.
161
The lines are always utf-8 encoded strings.
131
163
if not self.items:
132
164
# max() complains if sequence is empty
135
167
for tag, value in self.items:
136
assert isinstance(value, (str, unicode))
168
assert isinstance(tag, str), type(tag)
169
assert isinstance(value, unicode)
138
171
result.append(tag + ': \n')
139
172
elif '\n' in value:
140
173
# don't want splitlines behaviour on empty lines
141
174
val_lines = value.split('\n')
142
result.append(tag + ': ' + val_lines[0] + '\n')
175
result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
143
176
for line in val_lines[1:]:
144
result.append('\t' + line + '\n')
177
result.append('\t' + line.encode('utf-8') + '\n')
146
result.append(tag + ': ' + value + '\n')
179
result.append(tag + ': ' + value.encode('utf-8') + '\n')
149
182
def to_string(self):
150
183
"""Return stanza as a single string"""
151
184
return ''.join(self.to_lines())
186
def to_unicode(self):
187
"""Return stanza as a single Unicode string.
189
This is most useful when adding a Stanza to a parent Stanza
195
for tag, value in self.items:
197
result.append(tag + ': \n')
199
# don't want splitlines behaviour on empty lines
200
val_lines = value.split('\n')
201
result.append(tag + ': ' + val_lines[0] + '\n')
202
for line in val_lines[1:]:
203
result.append('\t' + line + '\n')
205
result.append(tag + ': ' + value + '\n')
206
return u''.join(result)
153
208
def write(self, to_file):
154
209
"""Write stanza to a file"""
155
210
to_file.writelines(self.to_lines())
191
255
Only the stanza lines and the trailing blank (if any) are consumed
192
256
from the line_iter.
258
The raw lines must be in utf-8 encoding.
260
unicode_iter = (line.decode('utf-8') for line in line_iter)
261
return read_stanza_unicode(unicode_iter)
264
def read_stanza_unicode(unicode_iter):
265
"""Read a Stanza from a list of lines or a file.
267
The lines should already be in unicode form. This returns a single
268
stanza that was read. If there is a blank line at the end of the Stanza,
269
it is consumed. It is not an error for there to be no blank line at
270
the end of the iterable. If there is a blank line at the beginning,
271
this is treated as an empty Stanza and None is returned.
273
Only the stanza lines and the trailing blank (if any) are consumed
274
from the unicode_iter
276
:param unicode_iter: A iterable, yeilding Unicode strings. See read_stanza
277
if you have a utf-8 encoded string.
278
:return: A Stanza object if there are any lines in the file.
195
281
stanza = Stanza()
197
283
accum_value = None
198
for line in line_iter:
199
if line == None or line == '':
285
# TODO: jam 20060922 This code should raise real errors rather than
286
# using 'assert' to process user input, or raising ValueError
287
# rather than a more specific error.
289
for line in unicode_iter:
290
if line is None or line == '':
200
291
break # end of file
202
293
break # end of stanza
203
assert line[-1] == '\n'
294
assert line.endswith('\n')
205
296
if line[0] == '\t': # continues previous value
213
304
colon_index = line.index(': ')
214
305
except ValueError:
215
raise ValueError('tag/value separator not found in line %r' % real_l)
216
tag = line[:colon_index]
306
raise ValueError('tag/value separator not found in line %r'
308
tag = str(line[:colon_index])
217
309
assert valid_tag(tag), \
218
310
"invalid rio tag %r" % tag
219
311
accum_value = line[colon_index+2:-1]
220
313
if tag is not None: # add last tag-value
221
314
stanza.add(tag, accum_value)
223
316
else: # didn't see any content
320
def to_patch_lines(stanza, max_width=72):
321
"""Convert a stanza into RIO-Patch format lines.
323
RIO-Patch is a RIO variant designed to be e-mailed as part of a patch.
324
It resists common forms of damage such as newline conversion or the removal
325
of trailing whitespace, yet is also reasonably easy to read.
327
:param max_width: The maximum number of characters per physical line.
328
:return: a list of lines
331
max_rio_width = max_width - 4
333
for pline in stanza.to_lines():
334
for line in pline.split('\n')[:-1]:
335
line = re.sub('\\\\', '\\\\\\\\', line)
337
partline = line[:max_rio_width]
338
line = line[max_rio_width:]
339
if len(line) > 0 and line[0] != [' ']:
341
break_index = partline.rfind(' ', -20)
343
break_index = partline.rfind('-', -20)
346
break_index = partline.rfind('/', -20)
348
line = partline[break_index:] + line
349
partline = partline[:break_index]
352
partline = re.sub('\r', '\\\\r', partline)
356
elif re.search(' $', partline):
359
lines.append('# ' + partline + '\n')
365
def _patch_stanza_iter(line_iter):
370
return map[match.group(0)]
373
for line in line_iter:
374
if line.startswith('# '):
377
assert line.startswith('#')
379
if last_line is not None and len(line) > 2:
381
line = re.sub('\r', '', line)
382
line = re.sub('\\\\(.|\n)', mapget, line)
383
if last_line is None:
387
if last_line[-1] == '\n':
390
if last_line is not None:
394
def read_patch_stanza(line_iter):
395
"""Convert an iterable of RIO-Patch format lines into a Stanza.
397
RIO-Patch is a RIO variant designed to be e-mailed as part of a patch.
398
It resists common forms of damage such as newline conversion or the removal
399
of trailing whitespace, yet is also reasonably easy to read.
403
return read_stanza(_patch_stanza_iter(line_iter))