~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to weavefile.py

  • Committer: Martin Pool
  • Date: 2005-06-30 07:20:43 UTC
  • mto: This revision was merged to the branch mainline in revision 852.
  • Revision ID: mbp@sourcefrog.net-20050630072043-de82477fcf66eeaa
Clean up assertions for weavefile

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
# TODO: When extracting a single version it'd be enough to just pass
38
38
# an iterator returning the weave lines...
39
39
 
40
 
FORMAT_1 = '# bzr weave file v1'
 
40
FORMAT_1 = '# bzr weave file v1\n'
41
41
 
42
42
 
43
43
 
44
44
 
45
45
def write_weave_v1(weave, f):
46
46
    """Write weave to file f."""
47
 
    print >>f, FORMAT_1
48
 
    print >>f
 
47
    print >>f, FORMAT_1,
49
48
 
50
49
    for version, verinfo in enumerate(weave._v):
51
50
        print >>f, 'v', version
66
65
 
67
66
    for l in weave._l:
68
67
        if isinstance(l, tuple):
69
 
            assert len(l) == 2
70
68
            assert l[0] in '{}[]'
71
69
            print >>f, '%s %d' % l
72
70
        else: # text line
73
71
            if not l:
74
72
                print >>f, ', '
75
73
            elif l[-1] == '\n':
76
 
                assert '\n' not in l[:-1]
 
74
                assert l.find('\n', 0, -1) == -1
77
75
                print >>f, '.', l,
78
76
            else:
 
77
                assert l.find('\n') == -1
79
78
                print >>f, ',', l
80
79
 
81
80
    print >>f, 'W'
82
81
 
83
82
 
84
83
def read_weave_v1(f):
85
 
    from weave import Weave, VerInfo
 
84
    from weave import Weave, VerInfo, WeaveFormatError
86
85
    w = Weave()
87
86
 
88
 
    assert f.readline() == FORMAT_1+'\n'
89
 
    assert f.readline() == '\n'
 
87
    wfe = WeaveFormatError
 
88
    l = f.readline()
 
89
    if l != FORMAT_1:
 
90
        raise WeaveFormatError('invalid weave file header: %r' % l)
90
91
 
 
92
    v_cnt = 0
91
93
    while True:
92
94
        l = f.readline()
93
 
        if l[0] == 'v':
 
95
        if l.startswith('v '):
 
96
            ver = int(l[2:])
 
97
            if ver != v_cnt:
 
98
                raise WeaveFormatError('version %d!=%d out of order'
 
99
                                       % (ver, v_cnt))
 
100
            v_cnt += 1
 
101
            
94
102
            l = f.readline()[:-1]
95
103
            if l[0] != 'i':
96
 
                raise Exception(`l`)
 
104
                raise WeaveFormatError('unexpected line %r' % l)
97
105
            if len(l) > 2:
98
106
                included = map(int, l[2:].split(' '))
99
107
                w._v.append(VerInfo(included))
100
108
            else:
101
109
                w._v.append(VerInfo())
102
110
            assert f.readline() == '\n'
103
 
        elif l[0] == 'w':
 
111
        elif l == 'w\n':
104
112
            break
105
113
        else:
106
 
            assert 0, l
 
114
            raise WeaveFormatError('unexpected line %r' % l)
107
115
 
108
116
    while True:
109
117
        l = f.readline()
110
118
        if l == 'W\n':
111
119
            break
112
 
        elif l[:2] == '. ':
 
120
        elif l.startswith('. '):
113
121
            w._l.append(l[2:])           # include newline
114
 
        elif l[:2] == ', ':
 
122
        elif l.startswith(', '):
115
123
            w._l.append(l[2:-1])        # exclude newline
116
124
        else:
117
125
            assert l[0] in '{}[]', l