home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0372 / python-core-2.2.2 / lib / dircache.py < prev    next >
Text File  |  2004-10-09  |  1KB  |  45 lines

  1. """Read and cache directory listings.
  2.  
  3. The listdir() routine returns a sorted list of the files in a directory,
  4. using a cache to avoid reading the directory more often than necessary.
  5. The annotate() routine appends slashes to directories."""
  6.  
  7. import os
  8.  
  9. __all__ = ["listdir", "opendir", "annotate", "reset"]
  10.  
  11. cache = {}
  12.  
  13. def reset():
  14.     """Reset the cache completely."""
  15.     global cache
  16.     cache = {}
  17.  
  18. def listdir(path):
  19.     """List directory contents, using cache."""
  20.     try:
  21.         cached_mtime, list = cache[path]
  22.         del cache[path]
  23.     except KeyError:
  24.         cached_mtime, list = -1, []
  25.     try:
  26.         mtime = os.stat(path)[8]
  27.     except os.error:
  28.         return []
  29.     if mtime != cached_mtime:
  30.         try:
  31.             list = os.listdir(path)
  32.         except os.error:
  33.             return []
  34.         list.sort()
  35.     cache[path] = mtime, list
  36.     return list
  37.  
  38. opendir = listdir # XXX backward compatibility
  39.  
  40. def annotate(head, list):
  41.     """Add '/' suffixes to directories."""
  42.     for i in range(len(list)):
  43.         if os.path.isdir(os.path.join(head, list[i])):
  44.             list[i] = list[i] + '/'
  45.