Mercurial > cpython-lru_cache-weakref
annotate Lib/macpath.py @ 3216:d00a79ebb027 legacy-trunk
Added missing imgpbm and imgsgi modules
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Thu, 12 Oct 1995 10:23:27 +0000 |
| parents | a980d076052d |
| children | 47763ac44c48 |
| rev | line source |
|---|---|
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
1 # module 'macpath' -- pathname (or -related) operations for the Macintosh |
| 103 | 2 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
3 import string |
| 103 | 4 import mac |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
5 from stat import * |
| 103 | 6 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
7 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
8 # Normalize the case of a pathname. Dummy in Posix, but string.lower here. |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
9 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
10 normcase = string.lower |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
11 |
| 103 | 12 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
13 # Return true if a path is absolute. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
14 # On the Mac, relative paths begin with a colon, |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
15 # but as a special case, paths with no colons at all are also relative. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
16 # Anything else is absolute (the string up to the first colon is the |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
17 # volume name). |
| 103 | 18 |
| 19 def isabs(s): | |
| 20 return ':' in s and s[0] <> ':' | |
| 21 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
22 |
| 360 | 23 # Join two pathnames. |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
24 # The result is equivalent to what the second pathname would refer to |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
25 # if the first pathname were the current directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
26 |
| 360 | 27 def join(s, t): |
| 103 | 28 if (not s) or isabs(t): return t |
| 559 | 29 if t[:1] == ':': t = t[1:] |
| 103 | 30 if ':' not in s: |
| 31 s = ':' + s | |
| 32 if s[-1:] <> ':': | |
| 33 s = s + ':' | |
| 34 return s + t | |
| 35 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
36 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
37 # Split a pathname in two parts: the directory leading up to the final bit, |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
38 # and the basename (the filename, without colons, in that directory). |
| 360 | 39 # The result (s, t) is such that join(s, t) yields the original argument. |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
40 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
41 def split(s): |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
42 if ':' not in s: return '', s |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
43 colon = 0 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
44 for i in range(len(s)): |
| 559 | 45 if s[i] == ':': colon = i+1 |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
46 return s[:colon-1], s[colon:] |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
47 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
48 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
49 # Split a pathname into a drive specification and the rest of the |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
50 # path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
51 # empty (don't use the volume name -- it doesn't have the same |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
52 # syntactic and semantic oddities as DOS drive letters, such as there |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
53 # being a separate current directory per drive). |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
54 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
55 def splitdrive(p): |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
56 return '', p |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
57 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
58 |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
59 # Short interfaces to split() |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
60 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
61 def dirname(s): return split(s)[0] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
62 def basename(s): return split(s)[1] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
63 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
64 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
65 # Return true if the pathname refers to an existing directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
66 |
| 103 | 67 def isdir(s): |
| 68 try: | |
| 69 st = mac.stat(s) | |
| 70 except mac.error: | |
| 71 return 0 | |
| 72 return S_ISDIR(st[ST_MODE]) | |
| 73 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
74 |
| 2260 | 75 # Return true if the pathname refers to a symbolic link. |
| 76 # (Always false on the Mac, until we understand Aliases.) | |
| 77 | |
| 78 def islink(s): | |
| 79 return 0 | |
| 80 | |
| 81 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
82 # Return true if the pathname refers to an existing regular file. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
83 |
| 103 | 84 def isfile(s): |
| 85 try: | |
| 86 st = mac.stat(s) | |
| 87 except mac.error: | |
| 88 return 0 | |
| 89 return S_ISREG(st[ST_MODE]) | |
| 90 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
91 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
92 # Return true if the pathname refers to an existing file or directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
93 |
| 103 | 94 def exists(s): |
| 95 try: | |
| 96 st = mac.stat(s) | |
| 97 except mac.error: | |
| 98 return 0 | |
| 99 return 1 | |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
100 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
101 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
102 # Normalize a pathname: get rid of '::' sequences by backing up, |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
103 # e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
104 # Raise the exception norm_error below if backing up is impossible, |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
105 # e.g., for '::foo'. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
106 # XXX The Unix version doesn't raise an exception but simply |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
107 # returns an unnormalized path. Should do so here too. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
108 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
109 norm_error = 'macpath.norm_error: path cannot be normalized' |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
110 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
111 def normpath(s): |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
112 import string |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
113 if ':' not in s: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
114 return ':' + s |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
115 f = string.splitfields(s, ':') |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
116 pre = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
117 post = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
118 if not f[0]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
119 pre = f[:1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
120 f = f[1:] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
121 if not f[len(f)-1]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
122 post = f[-1:] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
123 f = f[:-1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
124 res = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
125 for seg in f: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
126 if seg: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
127 res.append(seg) |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
128 else: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
129 if not res: raise norm_error, 'path starts with ::' |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
130 del res[len(res)-1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
131 if not (pre or res): |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
132 raise norm_error, 'path starts with volume::' |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
133 if pre: res = pre + res |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
134 if post: res = res + post |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
135 s = res[0] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
136 for seg in res[1:]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
137 s = s + ':' + seg |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
138 return s |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
139 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
140 |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
141 # Directory tree walk. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
142 # For each directory under top (including top itself), |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
143 # func(arg, dirname, filenames) is called, where |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
144 # dirname is the name of the directory and filenames is the list |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
145 # of files (and subdirectories etc.) in the directory. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
146 # The func may modify the filenames list, to implement a filter, |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
147 # or to impose a different order of visiting. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
148 |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
149 def walk(top, func, arg): |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
150 try: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
151 names = mac.listdir(top) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
152 except mac.error: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
153 return |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
154 func(arg, top, names) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
155 for name in names: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
156 name = join(top, name) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
157 if isdir(name): |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
158 walk(name, func, arg) |
