183
186
For each name, the version number.
189
Descriptive name of this weave; typically the filename if known.
186
__slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map']
193
__slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
196
def __init__(self, weave_name=None):
190
198
self._parents = []
193
201
self._name_map = {}
202
self._weave_name = weave_name
196
205
def __eq__(self, other):
205
214
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)
208
225
def lookup(self, name):
226
"""Convert symbolic version name to index."""
210
228
return self._name_map[name]
212
raise WeaveError("name %s not present in weave" % 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)
215
255
def add(self, name, parents, text):
230
270
assert isinstance(name, basestring)
231
271
if name in self._name_map:
232
raise WeaveError("name %r already present in weave" % name)
272
return self._check_repeated_add(name, parents, text)
274
parents = map(self.maybe_lookup, parents)
234
275
self._check_versions(parents)
235
276
## self._check_lines(text)
236
277
new_version = len(self._parents)
279
sha1 = sha_strings(text)
243
281
# if we abort after here the (in-memory) weave will be corrupt because only
244
282
# some fields are updated
350
388
raise ValueError("version %d not present in weave" % v)
391
def parents(self, version):
392
return self._parents[version]
353
395
def minimal_parents(self, version):
354
396
"""Find the minimal set of parents for the version."""
355
397
included = self._parents[version]
393
435
raise IndexError("invalid version number %r" % i)
396
def annotate(self, index):
397
return list(self.annotate_iter(index))
400
def annotate_iter(self, version):
438
def annotate(self, name_or_index):
439
return list(self.annotate_iter(name_or_index))
442
def annotate_iter(self, name_or_index):
401
443
"""Yield list of (index-id, line) pairs for the specified version.
403
445
The index indicates when the line originated in the weave."""
404
for origin, lineno, text in self._extract([version]):
446
incls = [self.maybe_lookup(name_or_index)]
447
for origin, lineno, text in self._extract(incls):
405
448
yield origin, text
504
def get_iter(self, version):
551
def get_iter(self, name_or_index):
505
552
"""Yield lines for the specified version."""
506
for origin, lineno, line in self._extract([version]):
553
incls = [self.maybe_lookup(name_or_index)]
554
for origin, lineno, line in self._extract(incls):
510
def get(self, index):
511
return list(self.get_iter(index))
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))
514
569
def mash_iter(self, included):