Commit b4e460ac authored by Fred Drake's avatar Fred Drake

Avoid import of string module; it is only needed for expandvars().

Never assume that os.sep is for the module-specific platform; use the
right separator character directly.
Fix some minor style consistency nits.
parent c0ab93ef
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
import os import os
import stat import stat
import string
def normcase(s): def normcase(s):
...@@ -15,7 +14,7 @@ def normcase(s): ...@@ -15,7 +14,7 @@ def normcase(s):
single '_', but this has been removed. This functionality should single '_', but this has been removed. This functionality should
possibly be added as a new function.""" possibly be added as a new function."""
return string.lower(string.replace(s, "/", "\\")) return s.replace("/", "\\").lower()
def isabs(s): def isabs(s):
...@@ -39,7 +38,7 @@ def join(a, *p): ...@@ -39,7 +38,7 @@ def join(a, *p):
elif path == '' or path[-1:] in '/\\:': elif path == '' or path[-1:] in '/\\:':
path = path + b path = path + b
else: else:
path = path + os.sep + b path = path + "\\" + b
return path return path
...@@ -225,8 +224,6 @@ def expanduser(path): ...@@ -225,8 +224,6 @@ def expanduser(path):
return userhome + path[i:] return userhome + path[i:]
varchars = string.letters + string.digits + '_-'
def expandvars(path): def expandvars(path):
"""Expand paths containing shell variable substitutions. """Expand paths containing shell variable substitutions.
The following rules apply: The following rules apply:
...@@ -239,6 +236,8 @@ def expandvars(path): ...@@ -239,6 +236,8 @@ def expandvars(path):
if '$' not in path: if '$' not in path:
return path return path
import string
varchars = string.letters + string.digits + '_-'
res = '' res = ''
index = 0 index = 0
pathlen = len(path) pathlen = len(path)
...@@ -248,9 +247,9 @@ def expandvars(path): ...@@ -248,9 +247,9 @@ def expandvars(path):
path = path[index + 1:] path = path[index + 1:]
pathlen = len(path) pathlen = len(path)
try: try:
index = string.index(path, '\'') index = path.index('\'')
res = res + '\'' + path[:index + 1] res = res + '\'' + path[:index + 1]
except string.index_error: except ValueError:
res = res + path res = res + path
index = pathlen -1 index = pathlen -1
elif c == '$': # variable or '$$' elif c == '$': # variable or '$$'
...@@ -261,11 +260,11 @@ def expandvars(path): ...@@ -261,11 +260,11 @@ def expandvars(path):
path = path[index+2:] path = path[index+2:]
pathlen = len(path) pathlen = len(path)
try: try:
index = string.index(path, '}') index = path.index('}')
var = path[:index] var = path[:index]
if os.environ.has_key(var): if os.environ.has_key(var):
res = res + os.environ[var] res = res + os.environ[var]
except string.index_error: except ValueError:
res = res + path res = res + path
index = pathlen - 1 index = pathlen - 1
else: else:
...@@ -290,12 +289,12 @@ def normpath(path): ...@@ -290,12 +289,12 @@ def normpath(path):
"""Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. """Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
Also, components of the path are silently truncated to 8+3 notation.""" Also, components of the path are silently truncated to 8+3 notation."""
path = string.replace(path, "/", "\\") path = path.replace("/", "\\")
prefix, path = splitdrive(path) prefix, path = splitdrive(path)
while path[:1] == os.sep: while path[:1] == "\\":
prefix = prefix + os.sep prefix = prefix + "\\"
path = path[1:] path = path[1:]
comps = string.splitfields(path, os.sep) comps = path.split("\\")
i = 0 i = 0
while i < len(comps): while i < len(comps):
if comps[i] == '.': if comps[i] == '.':
...@@ -303,22 +302,22 @@ def normpath(path): ...@@ -303,22 +302,22 @@ def normpath(path):
elif comps[i] == '..' and i > 0 and \ elif comps[i] == '..' and i > 0 and \
comps[i-1] not in ('', '..'): comps[i-1] not in ('', '..'):
del comps[i-1:i+1] del comps[i-1:i+1]
i = i-1 i = i - 1
elif comps[i] == '' and i > 0 and comps[i-1] <> '': elif comps[i] == '' and i > 0 and comps[i-1] <> '':
del comps[i] del comps[i]
elif '.' in comps[i]: elif '.' in comps[i]:
comp = string.splitfields(comps[i], '.') comp = comps[i].split('.')
comps[i] = comp[0][:8] + '.' + comp[1][:3] comps[i] = comp[0][:8] + '.' + comp[1][:3]
i = i+1 i = i + 1
elif len(comps[i]) > 8: elif len(comps[i]) > 8:
comps[i] = comps[i][:8] comps[i] = comps[i][:8]
i = i+1 i = i + 1
else: else:
i = i+1 i = i + 1
# If the path is now empty, substitute '.' # If the path is now empty, substitute '.'
if not prefix and not comps: if not prefix and not comps:
comps.append('.') comps.append('.')
return prefix + string.joinfields(comps, os.sep) return prefix + "\\".join(comps)
......
"""Pathname and path-related operations for the Macintosh.""" """Pathname and path-related operations for the Macintosh."""
import string
import os import os
from stat import * from stat import *
# Normalize the case of a pathname. Dummy in Posix, but string.lower here. # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
normcase = string.lower def normcase(path):
return path.lower()
def isabs(s): def isabs(s):
...@@ -44,7 +44,7 @@ def split(s): ...@@ -44,7 +44,7 @@ def split(s):
if ':' not in s: return '', s if ':' not in s: return '', s
colon = 0 colon = 0
for i in range(len(s)): for i in range(len(s)):
if s[i] == ':': colon = i+1 if s[i] == ':': colon = i + 1
path, file = s[:colon-1], s[colon:] path, file = s[:colon-1], s[colon:]
if path and not ':' in path: if path and not ':' in path:
path = path + ':' path = path + ':'
...@@ -175,20 +175,20 @@ def normpath(s): ...@@ -175,20 +175,20 @@ def normpath(s):
if ":" not in s: if ":" not in s:
return ":"+s return ":"+s
comps = string.splitfields(s, ":") comps = s.split(":")
i = 1 i = 1
while i < len(comps)-1: while i < len(comps)-1:
if comps[i] == "" and comps[i-1] != "": if comps[i] == "" and comps[i-1] != "":
if i > 1: if i > 1:
del comps[i-1:i+1] del comps[i-1:i+1]
i = i-1 i = i - 1
else: else:
# best way to handle this is to raise an exception # best way to handle this is to raise an exception
raise norm_error, 'Cannot use :: immedeately after volume name' raise norm_error, 'Cannot use :: immedeately after volume name'
else: else:
i = i + 1 i = i + 1
s = string.join(comps, ":") s = ":".join(comps)
# remove trailing ":" except for ":" and "Volume:" # remove trailing ":" except for ":" and "Volume:"
if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s): if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
......
...@@ -7,7 +7,6 @@ module as os.path. ...@@ -7,7 +7,6 @@ module as os.path.
import os import os
import stat import stat
import string
# Normalize the case of a pathname and map slashes to backslashes. # Normalize the case of a pathname and map slashes to backslashes.
...@@ -18,7 +17,7 @@ def normcase(s): ...@@ -18,7 +17,7 @@ def normcase(s):
"""Normalize case of pathname. """Normalize case of pathname.
Makes all characters lowercase and all slashes into backslashes.""" Makes all characters lowercase and all slashes into backslashes."""
return string.lower(string.replace(s, "/", "\\")) return s.replace("/", "\\").lower()
# Return whether a path is absolute. # Return whether a path is absolute.
...@@ -44,7 +43,7 @@ def join(a, *p): ...@@ -44,7 +43,7 @@ def join(a, *p):
elif path == '' or path[-1:] in '/\\:': elif path == '' or path[-1:] in '/\\:':
path = path + b path = path + b
else: else:
path = path + os.sep + b path = path + "\\" + b
return path return path
...@@ -77,11 +76,11 @@ def splitunc(p): ...@@ -77,11 +76,11 @@ def splitunc(p):
# \\machine\mountpoint\directories... # \\machine\mountpoint\directories...
# directory ^^^^^^^^^^^^^^^ # directory ^^^^^^^^^^^^^^^
normp = normcase(p) normp = normcase(p)
index = string.find(normp, '\\', 2) index = normp.find('\\', 2)
if index == -1: if index == -1:
##raise RuntimeError, 'illegal UNC path: "' + p + '"' ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
return ("", p) return ("", p)
index = string.find(normp, '\\', index + 1) index = normp.find('\\', index + 1)
if index == -1: if index == -1:
index = len(p) index = len(p)
return p[:index], p[index:] return p[:index], p[index:]
...@@ -241,7 +240,7 @@ def ismount(path): ...@@ -241,7 +240,7 @@ def ismount(path):
if unc: if unc:
return rest in ("", "/", "\\") return rest in ("", "/", "\\")
p = splitdrive(path)[1] p = splitdrive(path)[1]
return len(p)==1 and p[0] in '/\\' return len(p) == 1 and p[0] in '/\\'
# Directory tree walk. # Directory tree walk.
...@@ -288,7 +287,7 @@ def expanduser(path): ...@@ -288,7 +287,7 @@ def expanduser(path):
return path return path
i, n = 1, len(path) i, n = 1, len(path)
while i < n and path[i] not in '/\\': while i < n and path[i] not in '/\\':
i = i+1 i = i + 1
if i == 1: if i == 1:
if os.environ.has_key('HOME'): if os.environ.has_key('HOME'):
userhome = os.environ['HOME'] userhome = os.environ['HOME']
...@@ -296,7 +295,7 @@ def expanduser(path): ...@@ -296,7 +295,7 @@ def expanduser(path):
return path return path
else: else:
try: try:
drive=os.environ['HOMEDRIVE'] drive = os.environ['HOMEDRIVE']
except KeyError: except KeyError:
drive = '' drive = ''
userhome = join(drive, os.environ['HOMEPATH']) userhome = join(drive, os.environ['HOMEPATH'])
...@@ -314,14 +313,14 @@ def expanduser(path): ...@@ -314,14 +313,14 @@ def expanduser(path):
# XXX With COMMAND.COM you can use any characters in a variable name, # XXX With COMMAND.COM you can use any characters in a variable name,
# XXX except '^|<>='. # XXX except '^|<>='.
varchars = string.letters + string.digits + '_-'
def expandvars(path): def expandvars(path):
"""Expand shell variables of form $var and ${var}. """Expand shell variables of form $var and ${var}.
Unknown variables are left unchanged.""" Unknown variables are left unchanged."""
if '$' not in path: if '$' not in path:
return path return path
import string
varchars = string.letters + string.digits + '_-'
res = '' res = ''
index = 0 index = 0
pathlen = len(path) pathlen = len(path)
...@@ -331,11 +330,11 @@ def expandvars(path): ...@@ -331,11 +330,11 @@ def expandvars(path):
path = path[index + 1:] path = path[index + 1:]
pathlen = len(path) pathlen = len(path)
try: try:
index = string.index(path, '\'') index = path.index('\'')
res = res + '\'' + path[:index + 1] res = res + '\'' + path[:index + 1]
except string.index_error: except ValueError:
res = res + path res = res + path
index = pathlen -1 index = pathlen - 1
elif c == '$': # variable or '$$' elif c == '$': # variable or '$$'
if path[index + 1:index + 2] == '$': if path[index + 1:index + 2] == '$':
res = res + c res = res + c
...@@ -344,11 +343,11 @@ def expandvars(path): ...@@ -344,11 +343,11 @@ def expandvars(path):
path = path[index+2:] path = path[index+2:]
pathlen = len(path) pathlen = len(path)
try: try:
index = string.index(path, '}') index = path.index('}')
var = path[:index] var = path[:index]
if os.environ.has_key(var): if os.environ.has_key(var):
res = res + os.environ[var] res = res + os.environ[var]
except string.index_error: except ValueError:
res = res + path res = res + path
index = pathlen - 1 index = pathlen - 1
else: else:
...@@ -375,27 +374,27 @@ def expandvars(path): ...@@ -375,27 +374,27 @@ def expandvars(path):
def normpath(path): def normpath(path):
"""Normalize path, eliminating double slashes, etc.""" """Normalize path, eliminating double slashes, etc."""
path = string.replace(path, "/", "\\") path = path.replace("/", "\\")
prefix, path = splitdrive(path) prefix, path = splitdrive(path)
while path[:1] == os.sep: while path[:1] == "\\":
prefix = prefix + os.sep prefix = prefix + "\\"
path = path[1:] path = path[1:]
comps = string.splitfields(path, os.sep) comps = path.split("\\")
i = 0 i = 0
while i < len(comps): while i < len(comps):
if comps[i] == '.': if comps[i] == '.':
del comps[i] del comps[i]
elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
del comps[i-1:i+1] del comps[i-1:i+1]
i = i-1 i = i - 1
elif comps[i] == '' and i > 0 and comps[i-1] <> '': elif comps[i] == '' and i > 0 and comps[i-1] <> '':
del comps[i] del comps[i]
else: else:
i = i+1 i = i + 1
# If the path is now empty, substitute '.' # If the path is now empty, substitute '.'
if not prefix and not comps: if not prefix and not comps:
comps.append('.') comps.append('.')
return prefix + string.joinfields(comps, os.sep) return prefix + "\\".join(comps)
# Return an absolute path. # Return an absolute path.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment