1990-12-26 11:40:07 -04:00
|
|
|
# Module 'maccache'
|
|
|
|
#
|
|
|
|
# Maintain a cache of listdir(), isdir(), isfile() or exists() outcomes.
|
1992-03-31 15:05:44 -04:00
|
|
|
# XXX Should merge with module statcache
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1992-03-31 15:05:44 -04:00
|
|
|
import os
|
1990-12-26 11:40:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
# The cache.
|
|
|
|
# Keys are absolute pathnames;
|
|
|
|
# values are 0 (nothing), 1 (file) or [...] (dir).
|
|
|
|
#
|
|
|
|
cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
# Current working directory.
|
|
|
|
#
|
1992-03-31 15:05:44 -04:00
|
|
|
cwd = os.getcwd()
|
1990-12-26 11:40:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Constants.
|
|
|
|
#
|
|
|
|
NONE = 0
|
|
|
|
FILE = 1
|
|
|
|
LISTTYPE = type([])
|
|
|
|
|
|
|
|
def _stat(name):
|
1992-03-31 15:05:44 -04:00
|
|
|
name = os.path.join(cwd, name)
|
1990-12-26 11:40:07 -04:00
|
|
|
if cache.has_key(name):
|
|
|
|
return cache[name]
|
1992-03-31 15:05:44 -04:00
|
|
|
if os.path.isfile(name):
|
1990-12-26 11:40:07 -04:00
|
|
|
cache[name] = FILE
|
|
|
|
return FILE
|
|
|
|
try:
|
1992-03-31 15:05:44 -04:00
|
|
|
list = os.listdir(name)
|
1990-12-26 11:40:07 -04:00
|
|
|
except:
|
|
|
|
cache[name] = NONE
|
|
|
|
return NONE
|
|
|
|
cache[name] = list
|
1992-01-01 15:35:13 -04:00
|
|
|
if name[-1:] == ':': cache[name[:-1]] = list
|
1990-12-26 11:40:07 -04:00
|
|
|
else: cache[name+':'] = list
|
|
|
|
return list
|
|
|
|
|
|
|
|
def isdir(name):
|
|
|
|
st = _stat(name)
|
1992-01-01 15:35:13 -04:00
|
|
|
return type(st) == LISTTYPE
|
1990-12-26 11:40:07 -04:00
|
|
|
|
|
|
|
def isfile(name):
|
|
|
|
st = _stat(name)
|
1992-01-01 15:35:13 -04:00
|
|
|
return st == FILE
|
1990-12-26 11:40:07 -04:00
|
|
|
|
|
|
|
def exists(name):
|
|
|
|
st = _stat(name)
|
|
|
|
return st <> NONE
|
|
|
|
|
|
|
|
def listdir(name):
|
|
|
|
st = _stat(name)
|
1992-01-01 15:35:13 -04:00
|
|
|
if type(st) == LISTTYPE:
|
1990-12-26 11:40:07 -04:00
|
|
|
return st
|
|
|
|
else:
|
|
|
|
raise RuntimeError, 'list non-directory'
|