~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-31 03:24:21 UTC
  • Revision ID: mbp@sourcefrog.net-20050331032421-a17c894e5d2382c4
add new explicit RootEntry to inventory (in-core only)

Show diffs side-by-side

added added

removed removed

Lines of Context:
199
199
 
200
200
 
201
201
 
 
202
class RootEntry(InventoryEntry):
 
203
    def __init__(self, file_id):
 
204
        self.file_id = file_id
 
205
        self.children = {}
 
206
        self.kind = 'root_directory'
 
207
        self.parent_id = None
 
208
 
 
209
    def __cmp__(self, other):
 
210
        if self is other:
 
211
            return 0
 
212
        if not isinstance(other, RootEntry):
 
213
            return NotImplemented
 
214
        return cmp(self.file_id, other.file_id) \
 
215
               or cmp(self.children, other.children)
 
216
 
 
217
 
 
218
 
202
219
class Inventory(XMLMixin):
203
220
    """Inventory of versioned files in a tree.
204
221
 
217
234
    returned quickly.
218
235
 
219
236
    InventoryEntry objects must not be modified after they are
220
 
    inserted.
 
237
    inserted, other than through the Inventory API.
221
238
 
222
239
    >>> inv = Inventory()
223
240
    >>> inv.write_xml(sys.stdout)
263
280
        If a working directory is specified, the inventory is read
264
281
        from there.  If the file is specified, read from that. If not,
265
282
        the inventory is created empty.
 
283
 
 
284
        The inventory is created with a default root directory, with
 
285
        an id of None.
266
286
        """
267
 
        self._root = InventoryEntry(None, '', kind='directory')
268
 
        self._byid = {None: self._root}
 
287
        self.root = RootEntry(None)
 
288
        self._byid = {None: self.root}
269
289
 
270
290
 
271
291
    def __iter__(self):
277
297
        return len(self._byid)
278
298
 
279
299
 
280
 
    def iter_entries(self, parent_id=None):
 
300
    def iter_entries(self, from_dir=None):
281
301
        """Return (path, entry) pairs, in order by name."""
282
 
        kids = self[parent_id].children.items()
 
302
        if from_dir == None:
 
303
            assert self.root
 
304
            from_dir = self.root
 
305
        elif isinstance(from_dir, basestring):
 
306
            from_dir = self._byid[from_dir]
 
307
            
 
308
        kids = from_dir.children.items()
283
309
        kids.sort()
284
310
        for name, ie in kids:
285
311
            yield name, ie
286
312
            if ie.kind == 'directory':
287
 
                for cn, cie in self.iter_entries(parent_id=ie.file_id):
288
 
                    yield joinpath([name, cn]), cie
289
 
 
290
 
 
291
 
    def directories(self):
 
313
                for cn, cie in self.iter_entries(from_dir=ie.file_id):
 
314
                    yield '/'.join((name, cn)), cie
 
315
                    
 
316
 
 
317
 
 
318
    def directories(self, from_dir=None):
292
319
        """Return (path, entry) pairs for all directories.
293
320
        """
294
 
        yield '', self._root
 
321
        assert self.root
 
322
        yield '', self.root
295
323
        for path, entry in self.iter_entries():
296
324
            if entry.kind == 'directory':
297
325
                yield path, entry
323
351
 
324
352
 
325
353
    def get_child(self, parent_id, filename):
326
 
        if parent_id == None:
327
 
            return self._root.children.get(filename)
328
 
        else:
329
 
            return self[parent_id].children.get(filename)
 
354
        return self[parent_id].children.get(filename)
330
355
 
331
356
 
332
357
    def add(self, entry):
337
362
        if entry.file_id in self._byid:
338
363
            bailout("inventory already contains entry with id {%s}" % entry.file_id)
339
364
 
340
 
        parent = self._byid[entry.parent_id]
341
 
        if parent.kind != 'directory':
342
 
            bailout("attempt to add under non-directory {%s}" % parent.file_id)
 
365
        try:
 
366
            parent = self._byid[entry.parent_id]
 
367
        except KeyError:
 
368
            bailout("parent_id %r not in inventory" % entry.parent_id)
343
369
 
344
370
        if parent.children.has_key(entry.name):
345
371
            bailout("%s is already versioned" %