~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-05-19 08:31:06 UTC
  • Revision ID: mbp@sourcefrog.net-20050519083106-ebe71562d3bda4a7
- fix typo

Show diffs side-by-side

added added

removed removed

Lines of Context:
210
210
 
211
211
    from_element = classmethod(from_element)
212
212
 
213
 
    def __eq__(self, other):
 
213
    def __cmp__(self, other):
 
214
        if self is other:
 
215
            return 0
214
216
        if not isinstance(other, InventoryEntry):
215
217
            return NotImplemented
216
218
 
217
 
        return (self.file_id == other.file_id) \
218
 
               and (self.name == other.name) \
219
 
               and (self.text_sha1 == other.text_sha1) \
220
 
               and (self.text_size == other.text_size) \
221
 
               and (self.text_id == other.text_id) \
222
 
               and (self.parent_id == other.parent_id) \
223
 
               and (self.kind == other.kind)
224
 
 
225
 
 
226
 
    def __ne__(self, other):
227
 
        return not (self == other)
228
 
 
229
 
    def __hash__(self):
230
 
        raise ValueError('not hashable')
 
219
        return cmp(self.file_id, other.file_id) \
 
220
               or cmp(self.name, other.name) \
 
221
               or cmp(self.text_sha1, other.text_sha1) \
 
222
               or cmp(self.text_size, other.text_size) \
 
223
               or cmp(self.text_id, other.text_id) \
 
224
               or cmp(self.parent_id, other.parent_id) \
 
225
               or cmp(self.kind, other.kind)
231
226
 
232
227
 
233
228
 
239
234
        self.parent_id = None
240
235
        self.name = ''
241
236
 
242
 
    def __eq__(self, other):
 
237
    def __cmp__(self, other):
 
238
        if self is other:
 
239
            return 0
243
240
        if not isinstance(other, RootEntry):
244
241
            return NotImplemented
245
 
        
246
 
        return (self.file_id == other.file_id) \
247
 
               and (self.children == other.children)
 
242
        return cmp(self.file_id, other.file_id) \
 
243
               or cmp(self.children, other.children)
248
244
 
249
245
 
250
246
 
329
325
            if ie.kind == 'directory':
330
326
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
331
327
                    yield os.path.join(name, cn), cie
332
 
 
333
 
 
334
 
    def entries(self):
335
 
        """Return list of (path, ie) for all entries except the root.
336
 
 
337
 
        This may be faster than iter_entries.
 
328
                    
 
329
 
 
330
 
 
331
    def directories(self):
 
332
        """Return (path, entry) pairs for all directories.
338
333
        """
339
 
        accum = []
340
 
        def descend(dir_ie, dir_path):
341
 
            kids = dir_ie.children.items()
342
 
            kids.sort()
343
 
            for name, ie in kids:
344
 
                child_path = os.path.join(dir_path, name)
345
 
                accum.append((child_path, ie))
 
334
        def descend(parent_ie):
 
335
            parent_name = parent_ie.name
 
336
            yield parent_name, parent_ie
 
337
 
 
338
            # directory children in sorted order
 
339
            dn = []
 
340
            for ie in parent_ie.children.itervalues():
346
341
                if ie.kind == 'directory':
347
 
                    descend(ie, child_path)
348
 
 
349
 
        descend(self.root, '')
350
 
        return accum
351
 
 
352
 
 
353
 
    def directories(self):
354
 
        """Return (path, entry) pairs for all directories, including the root.
355
 
        """
356
 
        accum = []
357
 
        def descend(parent_ie, parent_path):
358
 
            accum.append((parent_path, parent_ie))
 
342
                    dn.append((ie.name, ie))
 
343
            dn.sort()
359
344
            
360
 
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
361
 
            kids.sort()
 
345
            for name, child_ie in dn:
 
346
                for sub_name, sub_ie in descend(child_ie):
 
347
                    yield appendpath(parent_name, sub_name), sub_ie
362
348
 
363
 
            for name, child_ie in kids:
364
 
                child_path = os.path.join(parent_path, name)
365
 
                descend(child_ie, child_path)
366
 
        descend(self.root, '')
367
 
        return accum
 
349
        for name, ie in descend(self.root):
 
350
            yield name, ie
368
351
        
369
352
 
370
353
 
470
453
        del self[ie.parent_id].children[ie.name]
471
454
 
472
455
 
 
456
    def id_set(self):
 
457
        from bzrlib import frozenset
 
458
        return frozenset(self._byid)
 
459
 
 
460
 
473
461
    def to_element(self):
474
462
        """Convert to XML Element"""
475
463
        e = Element('inventory')
498
486
    from_element = classmethod(from_element)
499
487
 
500
488
 
501
 
    def __eq__(self, other):
 
489
    def __cmp__(self, other):
502
490
        """Compare two sets by comparing their contents.
503
491
 
504
492
        >>> i1 = Inventory()
512
500
        >>> i1 == i2
513
501
        True
514
502
        """
 
503
        if self is other:
 
504
            return 0
 
505
        
515
506
        if not isinstance(other, Inventory):
516
507
            return NotImplemented
517
508
 
518
 
        if len(self._byid) != len(other._byid):
519
 
            # shortcut: obviously not the same
520
 
            return False
521
 
 
522
 
        return self._byid == other._byid
523
 
 
524
 
 
525
 
    def __ne__(self, other):
526
 
        return not (self == other)
527
 
 
528
 
 
529
 
    def __hash__(self):
530
 
        raise ValueError('not hashable')
531
 
 
 
509
        if self.id_set() ^ other.id_set():
 
510
            return 1
 
511
 
 
512
        for file_id in self._byid:
 
513
            c = cmp(self[file_id], other[file_id])
 
514
            if c: return c
 
515
 
 
516
        return 0
532
517
 
533
518
 
534
519
    def get_idpath(self, file_id):