aboutsummaryrefslogtreecommitdiff
path: root/lib/macglob.py
blob: f18c3966c7c18c4ec697fea986b9834bacd50647 (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
# Module 'macglob' -- version of 'glob' for the Macintosh.

# XXX At least one bug is left: a pattern like '*:' is treated
# XXX as a relative pathname (and returns as if it was ':*:').

import mac
import macpath
import fnmatch

def glob(pathname):
	if not has_magic(pathname): return [pathname]
	dirname, basename = macpath.split(pathname)
	if has_magic(dirname):
		if dirname[-1:] = ':': dirname = dirname[:-1]
		list = glob(dirname)
	else:
		list = [dirname]
	if not has_magic(basename):
		result = []
		for dirname in list:
			if basename or macpath.isdir(dirname):
				name = macpath.cat(dirname, basename)
				if macpath.exists(name):
					result.append(name)
	else:
		result = []
		for dirname in list:
			sublist = glob1(dirname, basename)
			for name in sublist:
				result.append(macpath.cat(dirname, name))
	return result

def glob1(dirname, pattern):
	if not dirname: dirname = ':'
	try:
		names = mac.listdir(dirname)
	except mac.error:
		return []
	result = []
	for name in names:
		if name[0] <> '.' or pattern[0] = '.':
			if fnmatch.fnmatch(name, pattern): result.append(name)
	return result

def has_magic(s):
	return '*' in s or '?' in s or '[' in s