macpath.py 2.3 KB
Newer Older
1
# module 'macpath' -- pathname (or -related) operations for the Macintosh
Guido van Rossum's avatar
Guido van Rossum committed
2 3 4 5 6

import mac

from stat import *

7 8 9 10 11 12 13

# Return true if a path is absolute.
# On the Mac, relative paths begin with a colon,
# but as a special case, paths with no colons at all are also relative.
# Anything else is absolute (the string up to the first colon is the
# volume name).

Guido van Rossum's avatar
Guido van Rossum committed
14 15 16
def isabs(s):
	return ':' in s and s[0] <> ':'

17 18 19 20 21

# Concatenate two pathnames.
# The result is equivalent to what the second pathname would refer to
# if the first pathname were the current directory.

Guido van Rossum's avatar
Guido van Rossum committed
22 23 24 25 26 27 28 29 30
def cat(s, t):
	if (not s) or isabs(t): return t
	if t[:1] = ':': t = t[1:]
	if ':' not in s:
		s = ':' + s
	if s[-1:] <> ':':
		s = s + ':'
	return s + t

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

# Split a pathname in two parts: the directory leading up to the final bit,
# and the basename (the filename, without colons, in that directory).
# The result (s, t) is such that cat(s, t) yields the original argument.

def split(s):
	if ':' not in s: return '', s
	colon = 0
	for i in range(len(s)):
		if s[i] = ':': colon = i+1
	return s[:colon], s[colon:]


# Normalize a pathname: get rid of '::' sequences by backing up,
# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
# Raise the exception norm_error below if backing up is impossible,
# e.g., for '::foo'.

norm_error = 'macpath.norm_error: path cannot be normalized'
Guido van Rossum's avatar
Guido van Rossum committed
50 51

def norm(s):
52
	import string
Guido van Rossum's avatar
Guido van Rossum committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	if ':' not in s:
		return ':' + s
	f = string.splitfields(s, ':')
	pre = []
	post = []
	if not f[0]:
		pre = f[:1]
		f = f[1:]
	if not f[len(f)-1]:
		post = f[-1:]
		f = f[:-1]
	res = []
	for seg in f:
		if seg:
			res.append(seg)
		else:
69
			if not res: raise norm_error, 'path starts with ::'
Guido van Rossum's avatar
Guido van Rossum committed
70 71
			del res[len(res)-1]
			if not (pre or res):
72
				raise norm_error, 'path starts with volume::'
Guido van Rossum's avatar
Guido van Rossum committed
73 74 75 76 77 78 79
	if pre: res = pre + res
	if post: res = res + post
	s = res[0]
	for seg in res[1:]:
		s = s + ':' + seg
	return s

80 81 82

# Return true if the pathname refers to an existing directory.

Guido van Rossum's avatar
Guido van Rossum committed
83 84 85 86 87 88 89
def isdir(s):
	try:
		st = mac.stat(s)
	except mac.error:
		return 0
	return S_ISDIR(st[ST_MODE])

90 91 92

# Return true if the pathname refers to an existing regular file.

Guido van Rossum's avatar
Guido van Rossum committed
93 94 95 96 97 98 99
def isfile(s):
	try:
		st = mac.stat(s)
	except mac.error:
		return 0
	return S_ISREG(st[ST_MODE])

100 101 102

# Return true if the pathname refers to an existing file or directory.

Guido van Rossum's avatar
Guido van Rossum committed
103 104 105 106 107 108
def exists(s):
	try:
		st = mac.stat(s)
	except mac.error:
		return 0
	return 1