0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
1 |
#! /usr/bin/python
|
2 |
||
3 |
# Copyright (C) 2005 Canonical Ltd
|
|
4 |
||
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.
|
|
9 |
||
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.
|
|
14 |
||
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
|
|
18 |
||
19 |
# Author: Martin Pool <mbp@canonical.com>
|
|
20 |
||
21 |
||
22 |
||
23 |
||
24 |
"""Store and retrieve weaves in files.
|
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
25 |
|
26 |
There is one format marker followed by a blank line, followed by a
|
|
27 |
series of version headers, followed by the weave itself.
|
|
28 |
||
29 |
Each version marker has 'v' and the version, then 'i' and the included
|
|
30 |
previous versions.
|
|
31 |
||
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.
|
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
35 |
"""
|
36 |
||
37 |
# TODO: When extracting a single version it'd be enough to just pass
|
|
38 |
# an iterator returning the weave lines...
|
|
39 |
||
40 |
FORMAT_1 = '# bzr weave file v1' |
|
41 |
||
42 |
||
43 |
||
44 |
||
45 |
def write_weave_v1(weave, f): |
|
46 |
"""Write weave to file f."""
|
|
47 |
print >>f, FORMAT_1 |
|
48 |
print >>f |
|
49 |
||
50 |
for version, verinfo in enumerate(weave._v): |
|
51 |
print >>f, 'v', version |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
52 |
if verinfo.included: |
53 |
included = list(verinfo.included) |
|
54 |
included.sort() |
|
55 |
assert included[0] >= 0 |
|
56 |
assert included[-1] < version |
|
57 |
print >>f, 'i', |
|
58 |
for i in included: |
|
59 |
print >>f, i, |
|
60 |
print >>f |
|
61 |
else: |
|
62 |
print >>f, 'i' |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
63 |
print >>f |
64 |
||
65 |
print >>f, 'w' |
|
66 |
||
67 |
for l in weave._l: |
|
68 |
if isinstance(l, tuple): |
|
69 |
assert len(l) == 2 |
|
70 |
assert l[0] in '{}[]' |
|
71 |
print >>f, '%s %d' % l |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
72 |
else: # text line |
73 |
if not l: |
|
74 |
print >>f, ', ' |
|
75 |
elif l[-1] == '\n': |
|
76 |
assert '\n' not in l[:-1] |
|
77 |
print >>f, '.', l, |
|
78 |
else: |
|
79 |
print >>f, ',', l |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
80 |
|
81 |
print >>f, 'W' |
|
82 |
||
83 |
||
84 |
def read_weave_v1(f): |
|
85 |
from weave import Weave, VerInfo |
|
86 |
w = Weave() |
|
87 |
||
88 |
assert f.readline() == FORMAT_1+'\n' |
|
89 |
assert f.readline() == '\n' |
|
90 |
||
91 |
while True: |
|
92 |
l = f.readline() |
|
93 |
if l[0] == 'v': |
|
94 |
l = f.readline()[:-1] |
|
95 |
if l[0] != 'i': |
|
96 |
raise Exception(`l`) |
|
97 |
if len(l) > 2: |
|
98 |
included = map(int, l[2:].split(' ')) |
|
99 |
w._v.append(VerInfo(included)) |
|
100 |
else: |
|
101 |
w._v.append(VerInfo()) |
|
102 |
assert f.readline() == '\n' |
|
103 |
elif l[0] == 'w': |
|
104 |
break
|
|
105 |
else: |
|
106 |
assert 0, l |
|
107 |
||
108 |
while True: |
|
109 |
l = f.readline() |
|
110 |
if l == 'W\n': |
|
111 |
break
|
|
112 |
elif l[:2] == '. ': |
|
0.1.72
by Martin Pool
Go back to weave lines normally having newlines at the end. |
113 |
w._l.append(l[2:]) # include newline |
114 |
elif l[:2] == ', ': |
|
115 |
w._l.append(l[2:-1]) # exclude newline |
|
0.1.69
by Martin Pool
Simple text-based format for storing weaves, cleaner than |
116 |
else: |
117 |
assert l[0] in '{}[]', l |
|
118 |
assert l[1] == ' ', l |
|
119 |
w._l.append((l[0], int(l[2:]))) |
|
120 |
||
121 |
return w |
|
122 |