186
183
For each name, the version number.
189
Descriptive name of this weave; typically the filename if known.
193
__slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
186
__slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map']
196
def __init__(self, weave_name=None):
198
190
self._parents = []
201
193
self._name_map = {}
202
self._weave_name = weave_name
205
196
def __eq__(self, other):
214
205
return not self.__eq__(other)
217
def maybe_lookup(self, name_or_index):
218
"""Convert possible symbolic name to index, or pass through indexes."""
219
if isinstance(name_or_index, (int, long)):
222
return self.lookup(name_or_index)
225
208
def lookup(self, name):
226
"""Convert symbolic version name to index."""
228
210
return self._name_map[name]
230
raise WeaveError("name %r not present in weave %r" %
231
(name, self._weave_name))
234
def idx_to_name(self, version):
235
return self._names[version]
238
def _check_repeated_add(self, name, parents, text):
239
"""Check that a duplicated add is OK.
241
If it is, return the (old) index; otherwise raise an exception.
243
idx = self.lookup(name)
244
if sorted(self._parents[idx]) != sorted(parents):
245
raise WeaveError("name \"%s\" already present in weave "
246
"with different parents" % name)
247
new_sha1 = sha_strings(text)
248
if new_sha1 != self._sha1s[idx]:
249
raise WeaveError("name \"%s\" already present in weave "
250
"with different text" % name)
212
raise WeaveError("name %s not present in weave" % name)
255
215
def add(self, name, parents, text):
270
230
assert isinstance(name, basestring)
271
231
if name in self._name_map:
272
return self._check_repeated_add(name, parents, text)
274
parents = map(self.maybe_lookup, parents)
232
raise WeaveError("name %r already present in weave" % name)
275
234
self._check_versions(parents)
276
235
## self._check_lines(text)
277
236
new_version = len(self._parents)
279
sha1 = sha_strings(text)
281
243
# if we abort after here the (in-memory) weave will be corrupt because only
282
244
# some fields are updated
388
350
raise ValueError("version %d not present in weave" % v)
391
def parents(self, version):
392
return self._parents[version]
395
353
def minimal_parents(self, version):
396
354
"""Find the minimal set of parents for the version."""
397
355
included = self._parents[version]
435
393
raise IndexError("invalid version number %r" % i)
438
def annotate(self, name_or_index):
439
return list(self.annotate_iter(name_or_index))
442
def annotate_iter(self, name_or_index):
396
def annotate(self, index):
397
return list(self.annotate_iter(index))
400
def annotate_iter(self, version):
443
401
"""Yield list of (index-id, line) pairs for the specified version.
445
403
The index indicates when the line originated in the weave."""
446
incls = [self.maybe_lookup(name_or_index)]
447
for origin, lineno, text in self._extract(incls):
404
for origin, lineno, text in self._extract([version]):
448
405
yield origin, text
551
def get_iter(self, name_or_index):
504
def get_iter(self, version):
552
505
"""Yield lines for the specified version."""
553
incls = [self.maybe_lookup(name_or_index)]
554
for origin, lineno, line in self._extract(incls):
506
for origin, lineno, line in self._extract([version]):
558
def get_text(self, version):
559
assert isinstance(version, int)
561
s.writelines(self.get_iter(version))
565
def get(self, name_or_index):
566
return list(self.get_iter(name_or_index))
510
def get(self, index):
511
return list(self.get_iter(index))
569
514
def mash_iter(self, included):