Author: jmorliaguet Date: Wed Jun 21 23:01:44 2006 New Revision: 3487 Modified: cpsskins/branches/paris-sprint-2006/setup/archives.py
Log: - return the list of items in a directory: for instance: archive['themes/'] returns ['themes/default.xml', 'themes/sci.xml', 'themes/sci/', 'themes/default/'] Modified: cpsskins/branches/paris-sprint-2006/setup/archives.py ============================================================================== --- cpsskins/branches/paris-sprint-2006/setup/archives.py (original) +++ cpsskins/branches/paris-sprint-2006/setup/archives.py Wed Jun 21 23:01:44 2006 @@ -20,6 +20,7 @@ import os import time +from sets import Set from StringIO import StringIO from tarfile import TarFile from tarfile import TarInfo @@ -59,6 +60,9 @@ def __getitem__(self, k): if self.mode == 'w': raise IOError("This tar archive is write-only.") + # if the item is a directory return the list of files in the directory + if k[-1:] == '/': + return self._dir(k) file = self.archive.extractfile(k) return file.read() @@ -75,6 +79,26 @@ def keys(self): return self.archive.getnames() + def items(self): + return [(k, self[k]) for k in self.keys()] + + def _dir(self, k): + """Return the list of items in a directory including directories""" + depth = k.count('/') + items = [] + dirs = Set() + for f, v in self.items(): + f_depth = f.count('/') + if not f.startswith(k): + continue + if f_depth == depth +1: + dirs.add('/'.join(f.split('/')[:f_depth]) + '/') + if f.count('/') > depth: + continue + items.append(f) + items.extend(list(dirs)) + return items + class FileSystemArchive(object): """An archive that reads data from the file-system. """ -- http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins