aboutsummaryrefslogtreecommitdiff
path: root/lib/maccache.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/maccache.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/maccache.py')
-rw-r--r--lib/maccache.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/maccache.py b/lib/maccache.py
new file mode 100644
index 0000000..329e514
--- /dev/null
+++ b/lib/maccache.py
@@ -0,0 +1,61 @@
+# Module 'maccache'
+#
+# Maintain a cache of listdir(), isdir(), isfile() or exists() outcomes.
+
+import mac
+import macpath
+
+
+# The cache.
+# Keys are absolute pathnames;
+# values are 0 (nothing), 1 (file) or [...] (dir).
+#
+cache = {}
+
+
+# Current working directory.
+#
+cwd = mac.getcwd()
+
+
+# Constants.
+#
+NONE = 0
+FILE = 1
+LISTTYPE = type([])
+
+def _stat(name):
+ name = macpath.cat(cwd, name)
+ if cache.has_key(name):
+ return cache[name]
+ if macpath.isfile(name):
+ cache[name] = FILE
+ return FILE
+ try:
+ list = mac.listdir(name)
+ except:
+ cache[name] = NONE
+ return NONE
+ cache[name] = list
+ if name[-1:] = ':': cache[name[:-1]] = list
+ else: cache[name+':'] = list
+ return list
+
+def isdir(name):
+ st = _stat(name)
+ return type(st) = LISTTYPE
+
+def isfile(name):
+ st = _stat(name)
+ return st = FILE
+
+def exists(name):
+ st = _stat(name)
+ return st <> NONE
+
+def listdir(name):
+ st = _stat(name)
+ if type(st) = LISTTYPE:
+ return st
+ else:
+ raise RuntimeError, 'list non-directory'