1 # module 'macpath' -- pathname (or -related) operations for the Macintosh
8 # Normalize the case of a pathname. Dummy in Posix, but string.lower here.
10 normcase
= string
.lower
13 # Return true if a path is absolute.
14 # On the Mac, relative paths begin with a colon,
15 # but as a special case, paths with no colons at all are also relative.
16 # Anything else is absolute (the string up to the first colon is the
20 return ':' in s
and s
[0] <> ':'
24 # The result is equivalent to what the second pathname would refer to
25 # if the first pathname were the current directory.
28 if (not s
) or isabs(t
): return t
29 if t
[:1] == ':': t
= t
[1:]
37 # Split a pathname in two parts: the directory leading up to the final bit,
38 # and the basename (the filename, without colons, in that directory).
39 # The result (s, t) is such that join(s, t) yields the original argument.
42 if ':' not in s
: return '', s
44 for i
in range(len(s
)):
45 if s
[i
] == ':': colon
= i
+1
46 return s
[:colon
-1], s
[colon
:]
49 # Split a pathname into a drive specification and the rest of the
50 # path. Useful on DOS/Windows/NT; on the Mac, the drive is always
51 # empty (don't use the volume name -- it doesn't have the same
52 # syntactic and semantic oddities as DOS drive letters, such as there
53 # being a separate current directory per drive).
59 # Short interfaces to split()
61 def dirname(s
): return split(s
)[0]
62 def basename(s
): return split(s
)[1]
65 # Return true if the pathname refers to an existing directory.
72 return S_ISDIR(st
[ST_MODE
])
75 # Return true if the pathname refers to a symbolic link.
76 # (Always false on the Mac, until we understand Aliases.)
82 # Return true if the pathname refers to an existing regular file.
89 return S_ISREG(st
[ST_MODE
])
92 # Return true if the pathname refers to an existing file or directory.
102 # dummy expandvars to retain interface-compatability with other
104 def expandvars(path
):
108 # dummy expanduser to retain interface-compatability with other
110 def expanduser(path
):
113 # Normalize a pathname: get rid of '::' sequences by backing up,
114 # e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
115 # Raise the exception norm_error below if backing up is impossible,
117 # XXX The Unix version doesn't raise an exception but simply
118 # returns an unnormalized path. Should do so here too.
120 norm_error
= 'macpath.norm_error: path cannot be normalized'
126 f
= string
.splitfields(s
, ':')
140 if not res
: raise norm_error
, 'path starts with ::'
143 raise norm_error
, 'path starts with volume::'
144 if pre
: res
= pre
+ res
145 if post
: res
= res
+ post
152 # Directory tree walk.
153 # For each directory under top (including top itself),
154 # func(arg, dirname, filenames) is called, where
155 # dirname is the name of the directory and filenames is the list
156 # of files (and subdirectories etc.) in the directory.
157 # The func may modify the filenames list, to implement a filter,
158 # or to impose a different order of visiting.
160 def walk(top
, func
, arg
):
162 names
= mac
.listdir(top
)
165 func(arg
, top
, names
)
167 name
= join(top
, name
)
169 walk(name
, func
, arg
)