summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/maccache.py
blob: 47b673cf39a93c284fffa8d79b1511539d6b8bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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.join(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'