3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# Author: Martin Pool <mbp@canonical.com>
24
"""Store and retrieve weaves in files.
26
There is one format marker followed by a blank line, followed by a
27
series of version headers, followed by the weave itself.
29
Each version marker has 'v' and the version, then 'i' and the included
32
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
33
processing instructions. Lines of text are prefixed by '.' if the
34
line contains a newline, or ',' if not.
37
# TODO: When extracting a single version it'd be enough to just pass
38
# an iterator returning the weave lines...
40
FORMAT_1 = '# bzr weave file v1\n'
44
def write_weave(weave, f, format=None):
45
if format == None or format == 1:
46
return write_weave_v1(weave, f)
48
raise ValueError("unknown weave format %r" % format)
51
def write_weave_v1(weave, f):
52
"""Write weave to file f."""
55
for version, included in enumerate(weave._v):
56
print >>f, 'v', version
58
included = list(included)
60
assert included[0] >= 0
61
assert included[-1] < version
73
if isinstance(l, tuple):
75
print >>f, '%s %d' % l
80
assert l.find('\n', 0, -1) == -1
83
assert l.find('\n') == -1
91
return read_weave_v1(f)
95
from weave import Weave, WeaveFormatError
98
wfe = WeaveFormatError
101
raise WeaveFormatError('invalid weave file header: %r' % l)
106
if l.startswith('v '):
109
raise WeaveFormatError('version %d!=%d out of order'
113
l = f.readline()[:-1]
115
raise WeaveFormatError('unexpected line %r' % l)
117
included = map(int, l[2:].split(' '))
118
w._addversion(included)
121
assert f.readline() == '\n'
125
raise WeaveFormatError('unexpected line %r' % l)
131
elif l.startswith('. '):
132
w._l.append(l[2:]) # include newline
133
elif l.startswith(', '):
134
w._l.append(l[2:-1]) # exclude newline
136
assert l[0] in '{}[]', l
137
assert l[1] == ' ', l
138
w._l.append((l[0], int(l[2:])))