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