Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5e05e9

Browse files
committed
Added explanatory comments.
1 parent 67c9b8c commit b5e05e9

1 file changed

Lines changed: 44 additions & 6 deletions

File tree

Lib/macpath.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
# module 'macpath'
1+
# module 'macpath' -- pathname (or -related) operations for the Macintosh
22

33
import mac
44

5-
import string
6-
75
from stat import *
86

7+
8+
# Return true if a path is absolute.
9+
# On the Mac, relative paths begin with a colon,
10+
# but as a special case, paths with no colons at all are also relative.
11+
# Anything else is absolute (the string up to the first colon is the
12+
# volume name).
13+
914
def isabs(s):
1015
return ':' in s and s[0] <> ':'
1116

17+
18+
# Concatenate two pathnames.
19+
# The result is equivalent to what the second pathname would refer to
20+
# if the first pathname were the current directory.
21+
1222
def cat(s, t):
1323
if (not s) or isabs(t): return t
1424
if t[:1] = ':': t = t[1:]
@@ -18,9 +28,28 @@ def cat(s, t):
1828
s = s + ':'
1929
return s + t
2030

21-
norm_error = 'path cannot be normalized'
31+
32+
# Split a pathname in two parts: the directory leading up to the final bit,
33+
# and the basename (the filename, without colons, in that directory).
34+
# The result (s, t) is such that cat(s, t) yields the original argument.
35+
36+
def split(s):
37+
if ':' not in s: return '', s
38+
colon = 0
39+
for i in range(len(s)):
40+
if s[i] = ':': colon = i+1
41+
return s[:colon], s[colon:]
42+
43+
44+
# Normalize a pathname: get rid of '::' sequences by backing up,
45+
# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
46+
# Raise the exception norm_error below if backing up is impossible,
47+
# e.g., for '::foo'.
48+
49+
norm_error = 'macpath.norm_error: path cannot be normalized'
2250

2351
def norm(s):
52+
import string
2453
if ':' not in s:
2554
return ':' + s
2655
f = string.splitfields(s, ':')
@@ -37,31 +66,40 @@ def norm(s):
3766
if seg:
3867
res.append(seg)
3968
else:
40-
if not res: raise norm_error # starts with '::'
69+
if not res: raise norm_error, 'path starts with ::'
4170
del res[len(res)-1]
4271
if not (pre or res):
43-
raise norm_error # starts with 'vol::'
72+
raise norm_error, 'path starts with volume::'
4473
if pre: res = pre + res
4574
if post: res = res + post
4675
s = res[0]
4776
for seg in res[1:]:
4877
s = s + ':' + seg
4978
return s
5079

80+
81+
# Return true if the pathname refers to an existing directory.
82+
5183
def isdir(s):
5284
try:
5385
st = mac.stat(s)
5486
except mac.error:
5587
return 0
5688
return S_ISDIR(st[ST_MODE])
5789

90+
91+
# Return true if the pathname refers to an existing regular file.
92+
5893
def isfile(s):
5994
try:
6095
st = mac.stat(s)
6196
except mac.error:
6297
return 0
6398
return S_ISREG(st[ST_MODE])
6499

100+
101+
# Return true if the pathname refers to an existing file or directory.
102+
65103
def exists(s):
66104
try:
67105
st = mac.stat(s)

0 commit comments

Comments
 (0)