diff options
author | Guido van Rossum <guido@python.org> | 1990-12-26 15:40:07 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1990-12-26 15:40:07 (GMT) |
commit | 217a5fa3c33ae58c1fe420f94eeb7e806961c3c1 (patch) | |
tree | b0bdfd34e5a72346a63106565acc7622a28b2a30 /Mac | |
parent | 66a07c07a5b7b1ce7150d5c5c1a0998d062e452e (diff) | |
download | cpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.zip cpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.tar.gz cpython-217a5fa3c33ae58c1fe420f94eeb7e806961c3c1.tar.bz2 |
Initial revision
Diffstat (limited to 'Mac')
-rw-r--r-- | Mac/Lib/maccache.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Mac/Lib/maccache.py b/Mac/Lib/maccache.py new file mode 100644 index 0000000..dc6502b --- /dev/null +++ b/Mac/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' |