OLD | NEW |
1 """Common operations on Posix pathnames. | 1 """Common operations on Posix pathnames. |
2 | 2 |
3 Instead of importing this module directly, import os and refer to | 3 Instead of importing this module directly, import os and refer to |
4 this module as os.path. The "os.path" name is an alias for this | 4 this module as os.path. The "os.path" name is an alias for this |
5 module on Posix systems; on other systems (e.g. Mac, Windows), | 5 module on Posix systems; on other systems (e.g. Mac, Windows), |
6 os.path provides the same operations in a manner specific to that | 6 os.path provides the same operations in a manner specific to that |
7 platform, and is an alias to another module (e.g. macpath, ntpath). | 7 platform, and is an alias to another module (e.g. macpath, ntpath). |
8 | 8 |
9 Some of this can actually be useful on non-Posix systems too, e.g. | 9 Some of this can actually be useful on non-Posix systems too, e.g. |
10 for manipulation of the pathname component of URLs. | 10 for manipulation of the pathname component of URLs. |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 _varprog = None | 234 _varprog = None |
235 | 235 |
236 def expandvars(path): | 236 def expandvars(path): |
237 """Expand shell variables of form $var and ${var}. Unknown variables | 237 """Expand shell variables of form $var and ${var}. Unknown variables |
238 are left unchanged.""" | 238 are left unchanged.""" |
239 global _varprog | 239 global _varprog |
240 if '$' not in path: | 240 if '$' not in path: |
241 return path | 241 return path |
242 if not _varprog: | 242 if not _varprog: |
243 import re | 243 import re |
244 _varprog = re.compile(r'\$(\w+|\{[^}]*\})') | 244 _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII) |
245 i = 0 | 245 i = 0 |
246 while True: | 246 while True: |
247 m = _varprog.search(path, i) | 247 m = _varprog.search(path, i) |
248 if not m: | 248 if not m: |
249 break | 249 break |
250 i, j = m.span(0) | 250 i, j = m.span(0) |
251 name = m.group(1) | 251 name = m.group(1) |
252 if name.startswith('{') and name.endswith('}'): | 252 if name.startswith('{') and name.endswith('}'): |
253 name = name[1:-1] | 253 name = name[1:-1] |
254 if name in os.environ: | 254 if name in os.environ: |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 start_list = abspath(start).split(sep) | 356 start_list = abspath(start).split(sep) |
357 path_list = abspath(path).split(sep) | 357 path_list = abspath(path).split(sep) |
358 | 358 |
359 # Work out how much of the filepath is shared by start and path. | 359 # Work out how much of the filepath is shared by start and path. |
360 i = len(commonprefix([start_list, path_list])) | 360 i = len(commonprefix([start_list, path_list])) |
361 | 361 |
362 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] | 362 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
363 if not rel_list: | 363 if not rel_list: |
364 return curdir | 364 return curdir |
365 return join(*rel_list) | 365 return join(*rel_list) |
OLD | NEW |