home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / registries / directory.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.1 KB  |  69 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import logging
  21.  
  22. from checkbox.properties import Path
  23. from checkbox.registry import Registry
  24.  
  25.  
  26. class DirectoryRegistry(Registry):
  27.     """Base registry for containing directories.
  28.  
  29.     The default behavior is to return the files within a directory
  30.     separated by newlines.
  31.  
  32.     Subclasses should define a directory parameter.
  33.     """
  34.  
  35.     directory = Path()
  36.  
  37.     def __init__(self, directory=None):
  38.         super(DirectoryRegistry, self).__init__()
  39.         if directory is not None:
  40.             self.directory = directory
  41.  
  42.     def __str__(self):
  43.         logging.info("Reading directory: %s", self.directory)
  44.         return "\n".join(os.listdir(self.directory))
  45.  
  46.     def items(self):
  47.         return []
  48.  
  49.  
  50. class RecursiveDirectoryRegistry(DirectoryRegistry):
  51.     """Variant of the DirectoryRegistry that recurses into subdirectories."""
  52.  
  53.     def __str__(self):
  54.         logging.info("Reading directory: %s", self.directory)
  55.         return "\n".join(self._listdir(self.directory))
  56.  
  57.     def _listdir(self, root, path=""):
  58.         files = []
  59.         try:
  60.             for file in os.listdir(os.path.join(root, path)):
  61.                 pathname = os.path.join(path, file)
  62.                 if os.path.isdir(os.path.join(root, pathname)):
  63.                     files.extend(self._listdir(root, pathname))
  64.                 else:
  65.                     files.append(pathname)
  66.         except OSError:
  67.             pass
  68.         return files
  69.