PageRenderTime 36ms CodeModel.GetById 23ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/dircache.py

http://unladen-swallow.googlecode.com/
Python | 41 lines | 27 code | 7 blank | 7 comment | 1 complexity | b07d84b1e65331e1e40bd961203a25d1 MD5 | raw file
 1"""Read and cache directory listings.
 2
 3The listdir() routine returns a sorted list of the files in a directory,
 4using a cache to avoid reading the directory more often than necessary.
 5The annotate() routine appends slashes to directories."""
 6from warnings import warnpy3k
 7warnpy3k("the dircache module has been removed in Python 3.0", stacklevel=2)
 8del warnpy3k
 9
10import os
11
12__all__ = ["listdir", "opendir", "annotate", "reset"]
13
14cache = {}
15
16def reset():
17    """Reset the cache completely."""
18    global cache
19    cache = {}
20
21def listdir(path):
22    """List directory contents, using cache."""
23    try:
24        cached_mtime, list = cache[path]
25        del cache[path]
26    except KeyError:
27        cached_mtime, list = -1, []
28    mtime = os.stat(path).st_mtime
29    if mtime != cached_mtime:
30        list = os.listdir(path)
31        list.sort()
32    cache[path] = mtime, list
33    return list
34
35opendir = listdir # XXX backward compatibility
36
37def annotate(head, list):
38    """Add '/' suffixes to directories."""
39    for i in range(len(list)):
40        if os.path.isdir(os.path.join(head, list[i])):
41            list[i] = list[i] + '/'