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