1 """Pathname and path-related operations for the Macintosh."""
6 from genericpath
import *
8 __all__
= ["normcase","isabs","join","splitdrive","split","splitext",
9 "basename","dirname","commonprefix","getsize","getmtime",
10 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
11 "walk","expanduser","expandvars","normpath","abspath",
12 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
13 "devnull","realpath","supports_unicode_filenames"]
15 # strings representing various path-related bits and pieces
25 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
32 """Return true if a path is absolute.
33 On the Mac, relative paths begin with a colon,
34 but as a special case, paths with no colons at all are also relative.
35 Anything else is absolute (the string up to the first colon is the
38 return ':' in s
and s
[0] != ':'
44 if (not s
) or isabs(t
):
58 """Split a pathname into two parts: the directory leading up to the final
59 bit, and the basename (the filename, without colons, in that directory).
60 The result (s, t) is such that join(s, t) yields the original argument."""
62 if ':' not in s
: return '', s
64 for i
in range(len(s
)):
65 if s
[i
] == ':': colon
= i
+ 1
66 path
, file = s
[:colon
-1], s
[colon
:]
67 if path
and not ':' in path
:
73 return genericpath
._splitext
(p
, sep
, altsep
, extsep
)
74 splitext
.__doc
__ = genericpath
._splitext
.__doc
__
77 """Split a pathname into a drive specification and the rest of the
78 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
79 empty (don't use the volume name -- it doesn't have the same
80 syntactic and semantic oddities as DOS drive letters, such as there
81 being a separate current directory per drive)."""
86 # Short interfaces to split()
88 def dirname(s
): return split(s
)[0]
89 def basename(s
): return split(s
)[1]
95 return len(components
) == 2 and components
[1] == ''
98 """Return true if the pathname refers to a symbolic link."""
102 return Carbon
.File
.ResolveAliasFile(s
, 0)[2]
106 # Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
110 """Test whether a path exists. Returns True for broken symbolic links"""
118 def expandvars(path
):
119 """Dummy to retain interface-compatibility with other operating systems."""
123 def expanduser(path
):
124 """Dummy to retain interface-compatibility with other operating systems."""
127 class norm_error(Exception):
128 """Path cannot be normalized"""
131 """Normalize a pathname. Will return the same result for
139 while i
< len(comps
)-1:
140 if comps
[i
] == "" and comps
[i
-1] != "":
145 # best way to handle this is to raise an exception
146 raise norm_error
, 'Cannot use :: immediately after volume name'
152 # remove trailing ":" except for ":" and "Volume:"
153 if s
[-1] == ":" and len(comps
) > 2 and s
!= ":"*len(s
):
158 def walk(top
, func
, arg
):
159 """Directory tree walk with callback function.
161 For each directory in the directory tree rooted at top (including top
162 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
163 dirname is the name of the directory, and fnames a list of the names of
164 the files and subdirectories in dirname (excluding '.' and '..'). func
165 may modify the fnames list in-place (e.g. via del or slice assignment),
166 and walk will only recurse into the subdirectories whose names remain in
167 fnames; this can be used to implement a filter, or to impose a specific
168 order of visiting. No semantics are defined for, or required of, arg,
169 beyond that arg is always passed to func. It can be used, e.g., to pass
170 a filename pattern, or a mutable object designed to accumulate
171 statistics. Passing None for arg is common."""
174 names
= os
.listdir(top
)
177 func(arg
, top
, names
)
179 name
= join(top
, name
)
180 if isdir(name
) and not islink(name
):
181 walk(name
, func
, arg
)
185 """Return an absolute path."""
187 path
= join(os
.getcwd(), path
)
188 return normpath(path
)
190 # realpath is a no-op on systems without islink support
199 components
= path
.split(':')
200 path
= components
[0] + ':'
201 for c
in components
[1:]:
203 path
= Carbon
.File
.FSResolveAliasFile(path
, 1)[0].as_pathname()
206 supports_unicode_filenames
= False