1 """Read and cache directory listings.
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."""
9 __all__
= ["listdir", "opendir", "annotate", "reset"]
14 """Reset the cache completely."""
19 """List directory contents, using cache."""
21 cached_mtime
, list = cache
[path
]
24 cached_mtime
, list = -1, []
26 mtime
= os
.stat(path
)[8]
29 if mtime
!= cached_mtime
:
31 list = os
.listdir(path
)
35 cache
[path
] = mtime
, list
38 opendir
= listdir
# XXX backward compatibility
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
] + '/'