0001 """Read and cache directory listings. 0002 0003 The listdir() routine returns a sorted list of the files in a directory, 0004 using a cache to avoid reading the directory more often than necessary. 0005 The annotate() routine appends slashes to directories.""" 0006 0007 import os 0008 0009 __all__ = ["listdir", "opendir", "annotate", "reset"] 0010 0011 cache = {} 0012 0013 def reset(): 0014 """Reset the cache completely.""" 0015 global cache 0016 cache = {} 0017 0018 def listdir(path): 0019 """List directory contents, using cache.""" 0020 try: 0021 cached_mtime, list = cache[path] 0022 del cache[path] 0023 except KeyError: 0024 cached_mtime, list = -1, [] 0025 mtime = os.stat(path).st_mtime 0026 if mtime != cached_mtime: 0027 list = os.listdir(path) 0028 list.sort() 0029 cache[path] = mtime, list 0030 return list 0031 0032 opendir = listdir # XXX backward compatibility 0033 0034 def annotate(head, list): 0035 """Add '/' suffixes to directories.""" 0036 for i in range(len(list)): 0037 if os.path.isdir(os.path.join(head, list[i])): 0038 list[i] = list[i] + '/' 0039
Generated by PyXR 0.9.4