aboutsummaryrefslogtreecommitdiff
path: root/lib/dircache.py
diff options
context:
space:
mode:
authorSkip Montanaro <[email protected]>2021-02-16 14:40:46 -0600
committerSkip Montanaro <[email protected]>2021-02-16 14:40:46 -0600
commita19a216bc60160c162e616145ef091dd18ce4e61 (patch)
treefa4bdff21f9b04a125c84a2bfab8a1c738359e15 /lib/dircache.py
downloadpython-0.9.1-patched-QoL-a19a216bc60160c162e616145ef091dd18ce4e61.tar.xz
python-0.9.1-patched-QoL-a19a216bc60160c162e616145ef091dd18ce4e61.zip
Python 0.9.1 as posted in alt.sources
Diffstat (limited to 'lib/dircache.py')
-rw-r--r--lib/dircache.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/dircache.py b/lib/dircache.py
new file mode 100644
index 0000000..c14db3e
--- /dev/null
+++ b/lib/dircache.py
@@ -0,0 +1,36 @@
+# Module 'dircache'
+#
+# Return a sorted list of the files in a POSIX directory, using a cache
+# to avoid reading the directory more often than necessary.
+# Also contains a subroutine to append slashes to directories.
+
+import posix
+import path
+
+cache = {}
+
+def listdir(path): # List directory contents, using cache
+ try:
+ cached_mtime, list = cache[path]
+ del cache[path]
+ except RuntimeError:
+ cached_mtime, list = -1, []
+ try:
+ mtime = posix.stat(path)[8]
+ except posix.error:
+ return []
+ if mtime <> cached_mtime:
+ try:
+ list = posix.listdir(path)
+ except posix.error:
+ return []
+ list.sort()
+ cache[path] = mtime, list
+ return list
+
+opendir = listdir # XXX backward compatibility
+
+def annotate(head, list): # Add '/' suffixes to directories
+ for i in range(len(list)):
+ if path.isdir(path.cat(head, list[i])):
+ list[i] = list[i] + '/'