214
211
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
214
def lookup(self, name):
226
"""Convert symbolic version name to index."""
228
216
return self._name_map[name]
230
raise WeaveError("name %r not present in weave %r" %
218
raise WeaveError("name %s not present in weave %s" %
231
219
(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)
255
222
def add(self, name, parents, text):
256
223
"""Add a single text on top of the weave.
270
237
assert isinstance(name, basestring)
271
238
if name in self._name_map:
272
return self._check_repeated_add(name, parents, text)
274
parents = map(self.maybe_lookup, parents)
239
raise WeaveError("name %r already present in weave" % name)
275
241
self._check_versions(parents)
276
242
## self._check_lines(text)
277
243
new_version = len(self._parents)
279
sha1 = sha_strings(text)
281
250
# if we abort after here the (in-memory) weave will be corrupt because only
282
251
# some fields are updated
388
357
raise ValueError("version %d not present in weave" % v)
391
def parents(self, version):
392
return self._parents[version]
395
360
def minimal_parents(self, version):
396
361
"""Find the minimal set of parents for the version."""
397
362
included = self._parents[version]
435
400
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):
403
def annotate(self, index):
404
return list(self.annotate_iter(index))
407
def annotate_iter(self, version):
443
408
"""Yield list of (index-id, line) pairs for the specified version.
445
410
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):
411
for origin, lineno, text in self._extract([version]):
448
412
yield origin, text
551
def get_iter(self, name_or_index):
515
def get_iter(self, version):
552
516
"""Yield lines for the specified version."""
553
incls = [self.maybe_lookup(name_or_index)]
554
for origin, lineno, line in self._extract(incls):
517
for origin, lineno, line in self._extract([version]):
562
525
return s.getvalue()
565
def get(self, name_or_index):
566
return list(self.get_iter(name_or_index))
528
def get(self, index):
529
return list(self.get_iter(index))
569
532
def mash_iter(self, included):