0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 08:52:25 +01:00
cpython/Mac/Lib/maccache.py

62 lines
985 B
Python
Raw Normal View History

1990-12-26 16:40:07 +01:00
# 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):
1991-08-16 15:27:45 +02:00
name = macpath.join(cwd, name)
1990-12-26 16:40:07 +01:00
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
1992-01-01 20:35:13 +01:00
if name[-1:] == ':': cache[name[:-1]] = list
1990-12-26 16:40:07 +01:00
else: cache[name+':'] = list
return list
def isdir(name):
st = _stat(name)
1992-01-01 20:35:13 +01:00
return type(st) == LISTTYPE
1990-12-26 16:40:07 +01:00
def isfile(name):
st = _stat(name)
1992-01-01 20:35:13 +01:00
return st == FILE
1990-12-26 16:40:07 +01:00
def exists(name):
st = _stat(name)
return st <> NONE
def listdir(name):
st = _stat(name)
1992-01-01 20:35:13 +01:00
if type(st) == LISTTYPE:
1990-12-26 16:40:07 +01:00
return st
else:
raise RuntimeError, 'list non-directory'