Rewrite of normpath() by Corran Webster, so trailing :s are removed

(except for : and volume:, where they are needed).
This commit is contained in:
Jack Jansen 2000-08-06 21:18:35 +00:00
parent 2afffd42fa
commit 2fc0109375
1 changed files with 22 additions and 31 deletions

View File

@ -179,39 +179,30 @@ def expanduser(path):
norm_error = 'macpath.norm_error: path cannot be normalized' norm_error = 'macpath.norm_error: path cannot be normalized'
def normpath(s): def normpath(s):
"""Normalize a pathname: get rid of '::' sequences by backing up, """Normalize a pathname. Will return the same result for
e.g., 'foo:bar::bletch' becomes 'foo:bletch'. equivalent paths."""
Raise the exception norm_error below if backing up is impossible,
e.g., for '::foo'."""
# XXX The Unix version doesn't raise an exception but simply
# returns an unnormalized path. Should do so here too.
import string if ":" not in s:
if ':' not in s: return ":"+s
return ':' + s
f = string.splitfields(s, ':') comps = string.splitfields(s, ":")
pre = [] i = 1
post = [] while i < len(comps)-1:
if not f[0]: if comps[i] == "" and comps[i-1] != "":
pre = f[:1] if i > 1:
f = f[1:] del comps[i-1:i+1]
if not f[len(f)-1]: i = i-1
post = f[-1:]
f = f[:-1]
res = []
for seg in f:
if seg:
res.append(seg)
else: else:
if not res: raise norm_error, 'path starts with ::' # best way to handle this is to raise an exception
del res[len(res)-1] raise norm_error, 'Cannot use :: immedeately after volume name'
if not (pre or res): else:
raise norm_error, 'path starts with volume::' i = i + 1
if pre: res = pre + res
if post: res = res + post s = string.join(comps, ":")
s = res[0]
for seg in res[1:]: # remove trailing ":" except for ":" and "Volume:"
s = s + ':' + seg if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
s = s[:-1]
return s return s