Nuke hard tabs.

This commit is contained in:
Tim Peters 2001-07-02 04:59:35 +00:00
parent d1c296537f
commit 683ecc7374
3 changed files with 241 additions and 243 deletions

View file

@ -42,4 +42,3 @@ class _Environ:
return value return value
else: else:
return failobj return failobj

View file

@ -19,13 +19,13 @@ as os.path.
import os, stat, string import os, stat, string
try: try:
import swi import swi
except ImportError: except ImportError:
class _swi: class _swi:
def swi(*a): def swi(*a):
raise AttributeError, 'This function only available under RISC OS' raise AttributeError, 'This function only available under RISC OS'
block= swi block= swi
swi= _swi() swi= _swi()
[_false, _true]= range(2) [_false, _true]= range(2)
@ -45,154 +45,154 @@ _allowMOSFSNames= _false
## Path manipulation, RISC OS stylee. ## Path manipulation, RISC OS stylee.
def _split(p): def _split(p):
"""
split filing system name (including special field) and drive specifier from rest
of path. This is needed by many riscospath functions.
""" """
split filing system name (including special field) and drive specifier from rest dash= _allowMOSFSNames and p[:1]=='-'
of path. This is needed by many riscospath functions. if dash:
""" q= string.find(p, '-', 1)+1
dash= _allowMOSFSNames and p[:1]=='-'
if dash:
q= string.find(p, '-', 1)+1
else:
if p[:1]==':':
q= 0
else: else:
q= string.find(p, ':')+1 # q= index of start of non-FS portion of path if p[:1]==':':
s= string.find(p, '#') q= 0
if s==-1 or s>q: else:
s= q # find end of main FS name, not including special field q= string.find(p, ':')+1 # q= index of start of non-FS portion of path
else: s= string.find(p, '#')
for c in p[dash:s]: if s==-1 or s>q:
if c not in string.letters: s= q # find end of main FS name, not including special field
q= 0 else:
break # disallow invalid non-special-field characters in FS name for c in p[dash:s]:
r= q if c not in string.letters:
if p[q:q+1]==':': q= 0
r= string.find(p, '.', q+1)+1 break # disallow invalid non-special-field characters in FS name
if r==0: r= q
r= len(p) # find end of drive name (if any) following FS name (if any) if p[q:q+1]==':':
return (p[:q], p[q:r], p[r:]) r= string.find(p, '.', q+1)+1
if r==0:
r= len(p) # find end of drive name (if any) following FS name (if any)
return (p[:q], p[q:r], p[r:])
def normcase(p): def normcase(p):
"""
Normalize the case of a pathname. This converts to lowercase as the native RISC
OS filesystems are case-insensitive. However, not all filesystems have to be,
and there's no simple way to find out what type an FS is argh.
""" """
Normalize the case of a pathname. This converts to lowercase as the native RISC return string.lower(p)
OS filesystems are case-insensitive. However, not all filesystems have to be,
and there's no simple way to find out what type an FS is argh.
"""
return string.lower(p)
def isabs(p): def isabs(p):
"""
Return whether a path is absolute. Under RISC OS, a file system specifier does
not make a path absolute, but a drive name or number does, and so does using the
symbol for root, URD, library, CSD or PSD. This means it is perfectly possible
to have an "absolute" URL dependent on the current working directory, and
equally you can have a "relative" URL that's on a completely different device to
the current one argh.
""" """
Return whether a path is absolute. Under RISC OS, a file system specifier does (fs, drive, path)= _split(p)
not make a path absolute, but a drive name or number does, and so does using the return drive!='' or path[:1] in _roots
symbol for root, URD, library, CSD or PSD. This means it is perfectly possible
to have an "absolute" URL dependent on the current working directory, and
equally you can have a "relative" URL that's on a completely different device to
the current one argh.
"""
(fs, drive, path)= _split(p)
return drive!='' or path[:1] in _roots
def join(a, *p): def join(a, *p):
"""
Join path elements with the directory separator, replacing the entire path when
an absolute or FS-changing path part is found.
""" """
Join path elements with the directory separator, replacing the entire path when j= a
an absolute or FS-changing path part is found. for b in p:
""" (fs, drive, path)= _split(b)
j= a if fs!='' or drive!='' or path[:1] in _roots:
for b in p: j= b
(fs, drive, path)= _split(b) else:
if fs!='' or drive!='' or path[:1] in _roots: j= j+'.'+b
j= b return j
else:
j= j+'.'+b
return j
def split(p): def split(p):
"""
Split a path in head (everything up to the last '.') and tail (the rest). FS
name must still be dealt with separately since special field may contain '.'.
""" """
Split a path in head (everything up to the last '.') and tail (the rest). FS (fs, drive, path)= _split(p)
name must still be dealt with separately since special field may contain '.'. q= string.rfind(path, '.')
""" if q!=-1:
(fs, drive, path)= _split(p) return (fs+drive+path[:q], path[q+1:])
q= string.rfind(path, '.') return ('', p)
if q!=-1:
return (fs+drive+path[:q], path[q+1:])
return ('', p)
def splitext(p): def splitext(p):
"""
Split a path in root and extension. This assumes the 'using slash for dot and
dot for slash with foreign files' convention common in RISC OS is in force.
""" """
Split a path in root and extension. This assumes the 'using slash for dot and (tail, head)= split(p)
dot for slash with foreign files' convention common in RISC OS is in force. if '/' in head:
""" q= len(head)-string.rfind(head, '/')
(tail, head)= split(p) return (p[:-q], p[-q:])
if '/' in head: return (p, '')
q= len(head)-string.rfind(head, '/')
return (p[:-q], p[-q:])
return (p, '')
def splitdrive(p): def splitdrive(p):
"""
Split a pathname into a drive specification (including FS name) and the rest of
the path. The terminating dot of the drive name is included in the drive
specification.
""" """
Split a pathname into a drive specification (including FS name) and the rest of (fs, drive, path)= _split(p)
the path. The terminating dot of the drive name is included in the drive return (fs+drive, p)
specification.
"""
(fs, drive, path)= _split(p)
return (fs+drive, p)
def basename(p): def basename(p):
"""
Return the tail (basename) part of a path.
""" """
Return the tail (basename) part of a path. return split(p)[1]
"""
return split(p)[1]
def dirname(p): def dirname(p):
"""
Return the head (dirname) part of a path.
""" """
Return the head (dirname) part of a path. return split(p)[0]
"""
return split(p)[0]
def commonprefix(ps): def commonprefix(ps):
"""
Return the longest prefix of all list elements. Purely string-based; does not
separate any path parts. Why am I in os.path?
""" """
Return the longest prefix of all list elements. Purely string-based; does not if len(ps)==0:
separate any path parts. Why am I in os.path? return ''
""" prefix= ps[0]
if len(ps)==0: for p in ps[1:]:
return '' prefix= prefix[:len(p)]
prefix= ps[0] for i in range(len(prefix)):
for p in ps[1:]: if prefix[i] <> p[i]:
prefix= prefix[:len(p)] prefix= prefix[:i]
for i in range(len(prefix)): if i==0:
if prefix[i] <> p[i]: return ''
prefix= prefix[:i] break
if i==0: return prefix
return ''
break
return prefix
## File access functions. Why are we in os.path? ## File access functions. Why are we in os.path?
def getsize(p): def getsize(p):
"""
Return the size of a file, reported by os.stat().
""" """
Return the size of a file, reported by os.stat(). st= os.stat(p)
""" return st[stat.ST_SIZE]
st= os.stat(p)
return st[stat.ST_SIZE]
def getmtime(p): def getmtime(p):
"""
Return the last modification time of a file, reported by os.stat().
""" """
Return the last modification time of a file, reported by os.stat(). st = os.stat(p)
""" return st[stat.ST_MTIME]
st = os.stat(p)
return st[stat.ST_MTIME]
getatime= getmtime getatime= getmtime
@ -200,40 +200,40 @@ getatime= getmtime
# RISC OS-specific file access functions # RISC OS-specific file access functions
def exists(p): def exists(p):
"""
Test whether a path exists.
""" """
Test whether a path exists. try:
""" return swi.swi('OS_File', '5s;i', p)!=0
try: except swi.error:
return swi.swi('OS_File', '5s;i', p)!=0 return 0
except swi.error:
return 0
def isdir(p): def isdir(p):
"""
Is a path a directory? Includes image files.
""" """
Is a path a directory? Includes image files. try:
""" return swi.swi('OS_File', '5s;i', p) in [2, 3]
try: except swi.error:
return swi.swi('OS_File', '5s;i', p) in [2, 3] return 0
except swi.error:
return 0
def isfile(p): def isfile(p):
"""
Test whether a path is a file, including image files.
""" """
Test whether a path is a file, including image files. try:
""" return swi.swi('OS_File', '5s;i', p) in [1, 3]
try: except swi.error:
return swi.swi('OS_File', '5s;i', p) in [1, 3] return 0
except swi.error:
return 0
def islink(p): def islink(p):
"""
RISC OS has no links or mounts.
""" """
RISC OS has no links or mounts. return _false
"""
return _false
ismount= islink ismount= islink
@ -246,23 +246,23 @@ ismount= islink
# assume it's running on RISC OS (unlike normpath). # assume it's running on RISC OS (unlike normpath).
def samefile(fa, fb): def samefile(fa, fb):
"""
Test whether two pathnames reference the same actual file.
""" """
Test whether two pathnames reference the same actual file. l= 512
""" b= swi.block(l)
l= 512 swi.swi('OS_FSControl', 'isb..i', 37, fa, b, l)
b= swi.block(l) fa= b.ctrlstring()
swi.swi('OS_FSControl', 'isb..i', 37, fa, b, l) swi.swi('OS_FSControl', 'isb..i', 37, fb, b, l)
fa= b.ctrlstring() fb= b.ctrlstring()
swi.swi('OS_FSControl', 'isb..i', 37, fb, b, l) return fa==fb
fb= b.ctrlstring()
return fa==fb
def sameopenfile(a, b): def sameopenfile(a, b):
"""
Test whether two open file objects reference the same file.
""" """
Test whether two open file objects reference the same file. return os.fstat(a)[stat.ST_INO]==os.fstat(b)[stat.ST_INO]
"""
return os.fstat(a)[stat.ST_INO]==os.fstat(b)[stat.ST_INO]
## Path canonicalisation ## Path canonicalisation
@ -271,93 +271,92 @@ Test whether two open file objects reference the same file.
# practice never used, for anything. # practice never used, for anything.
def expanduser(p): def expanduser(p):
(fs, drive, path)= _split(p) (fs, drive, path)= _split(p)
l= 512 l= 512
b= swi.block(l) b= swi.block(l)
if path[:1]!='@': if path[:1]!='@':
return p return p
if fs=='': if fs=='':
fsno= swi.swi('OS_Args', '00;i') fsno= swi.swi('OS_Args', '00;i')
swi.swi('OS_FSControl', 'iibi', 33, fsno, b, l) swi.swi('OS_FSControl', 'iibi', 33, fsno, b, l)
fsname= b.ctrlstring() fsname= b.ctrlstring()
else:
if fs[:1]=='-':
fsname= fs[1:-1]
else: else:
fsname= fs[:-1] if fs[:1]=='-':
fsname= string.split(fsname, '#', 1)[0] # remove special field from fs fsname= fs[1:-1]
x= swi.swi('OS_FSControl', 'ib2s.i;.....i', 54, b, fsname, l) else:
if x<l: fsname= fs[:-1]
urd= b.tostring(0, l-x-1) fsname= string.split(fsname, '#', 1)[0] # remove special field from fs
else: # no URD! try CSD x= swi.swi('OS_FSControl', 'ib2s.i;.....i', 54, b, fsname, l)
x= swi.swi('OS_FSControl', 'ib0s.i;.....i', 54, b, fsname, l)
if x<l: if x<l:
urd= b.tostring(0, l-x-1) urd= b.tostring(0, l-x-1)
else: # no CSD! use root else: # no URD! try CSD
urd= '$' x= swi.swi('OS_FSControl', 'ib0s.i;.....i', 54, b, fsname, l)
return fsname+':'+urd+path[1:] if x<l:
urd= b.tostring(0, l-x-1)
else: # no CSD! use root
urd= '$'
return fsname+':'+urd+path[1:]
# Environment variables are in angle brackets. # Environment variables are in angle brackets.
def expandvars(p): def expandvars(p):
"""
Expand environment variables using OS_GSTrans.
""" """
Expand environment variables using OS_GSTrans. l= 512
""" b= swi.block(l)
l= 512 return b.tostring(0, swi.swi('OS_GSTrans', 'sbi;..i', p, b, l))
b= swi.block(l)
return b.tostring(0, swi.swi('OS_GSTrans', 'sbi;..i', p, b, l))
# Return an absolute path. # Return an absolute path.
def abspath(p): def abspath(p):
return normpath(join(os.getcwd(), p)) return normpath(join(os.getcwd(), p))
# Normalize a path. Only special path element under RISC OS is "^" for "..". # Normalize a path. Only special path element under RISC OS is "^" for "..".
def normpath(p): def normpath(p):
"""
Normalize path, eliminating up-directory ^s.
""" """
Normalize path, eliminating up-directory ^s. (fs, drive, path)= _split(p)
""" rhs= ''
(fs, drive, path)= _split(p) ups= 0
rhs= '' while path!='':
ups= 0 (path, el)= split(path)
while path!='': if el=='^':
(path, el)= split(path) ups= ups+1
if el=='^':
ups= ups+1
else:
if ups>0:
ups= ups-1
else:
if rhs=='':
rhs= el
else: else:
rhs= el+'.'+rhs if ups>0:
while ups>0: ups= ups-1
ups= ups-1 else:
rhs= '^.'+rhs if rhs=='':
return fs+drive+rhs rhs= el
else:
rhs= el+'.'+rhs
while ups>0:
ups= ups-1
rhs= '^.'+rhs
return fs+drive+rhs
# Directory tree walk. # Directory tree walk.
# Independent of host system. Why am I in os.path? # Independent of host system. Why am I in os.path?
def walk(top, func, arg): def walk(top, func, arg):
"""
walk(top,func,args) calls func(arg, d, files) for each directory "d" in the tree
rooted at "top" (including "top" itself). "files" is a list of all the files and
subdirs in directory "d".
""" """
walk(top,func,args) calls func(arg, d, files) for each directory "d" in the tree try:
rooted at "top" (including "top" itself). "files" is a list of all the files and names= os.listdir(top)
subdirs in directory "d". except os.error:
""" return
try: func(arg, top, names)
names= os.listdir(top) for name in names:
except os.error: name= join(top, name)
return if isdir(name) and not islink(name):
func(arg, top, names) walk(name, func, arg)
for name in names:
name= join(top, name)
if isdir(name) and not islink(name):
walk(name, func, arg)

View file

@ -13,70 +13,70 @@ def url2pathname(pathname):
# #
tp = urllib.splittype(pathname)[0] tp = urllib.splittype(pathname)[0]
if tp and tp <> 'file': if tp and tp <> 'file':
raise RuntimeError, 'Cannot convert non-local URL to pathname' raise RuntimeError, 'Cannot convert non-local URL to pathname'
components = string.split(pathname, '/') components = string.split(pathname, '/')
# Remove . and embedded .. # Remove . and embedded ..
i = 0 i = 0
while i < len(components): while i < len(components):
if components[i] == '.': if components[i] == '.':
del components[i] del components[i]
elif components[i] == '..' and i > 0 and \ elif components[i] == '..' and i > 0 and \
components[i-1] not in ('', '..'): components[i-1] not in ('', '..'):
del components[i-1:i+1] del components[i-1:i+1]
i = i-1 i = i-1
elif components[i] == '' and i > 0 and components[i-1] <> '': elif components[i] == '' and i > 0 and components[i-1] <> '':
del components[i] del components[i]
else: else:
if components[i]<>'..' and string.find(components[i], '.')<>-1 : if components[i]<>'..' and string.find(components[i], '.')<>-1 :
components[i] = string.join(string.split(components[i],'.'),'/') components[i] = string.join(string.split(components[i],'.'),'/')
i = i+1 i = i+1
if not components[0]: if not components[0]:
# Absolute unix path, don't start with colon # Absolute unix path, don't start with colon
return string.join(components[1:], '.') return string.join(components[1:], '.')
else: else:
# relative unix path, start with colon. First replace # relative unix path, start with colon. First replace
# leading .. by empty strings (giving ::file) # leading .. by empty strings (giving ::file)
i = 0 i = 0
while i < len(components) and components[i] == '..': while i < len(components) and components[i] == '..':
components[i] = '^' components[i] = '^'
i = i + 1 i = i + 1
return string.join(components, '.') return string.join(components, '.')
def pathname2url(pathname): def pathname2url(pathname):
"convert mac pathname to /-delimited pathname" "convert mac pathname to /-delimited pathname"
if '/' in pathname: if '/' in pathname:
raise RuntimeError, "Cannot convert pathname containing slashes" raise RuntimeError, "Cannot convert pathname containing slashes"
components = string.split(pathname, ':') components = string.split(pathname, ':')
# Replace empty string ('::') by .. (will result in '/../' later) # Replace empty string ('::') by .. (will result in '/../' later)
for i in range(1, len(components)): for i in range(1, len(components)):
if components[i] == '': if components[i] == '':
components[i] = '..' components[i] = '..'
# Truncate names longer than 31 bytes # Truncate names longer than 31 bytes
components = map(lambda x: x[:31], components) components = map(lambda x: x[:31], components)
if os.path.isabs(pathname): if os.path.isabs(pathname):
return '/' + string.join(components, '/') return '/' + string.join(components, '/')
else: else:
return string.join(components, '/') return string.join(components, '/')
def test(): def test():
for url in ["index.html", for url in ["index.html",
"/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome", "/SCSI::SCSI4/$/Anwendung/Comm/Apps/!Fresco/Welcome",
"../index.html", "../index.html",
"bar/index.html", "bar/index.html",
"/foo/bar/index.html", "/foo/bar/index.html",
"/foo/bar/", "/foo/bar/",
"/"]: "/"]:
print `url`, '->', `url2pathname(url)` print `url`, '->', `url2pathname(url)`
for path in ["drive:", for path in ["drive:",
"drive:dir:", "drive:dir:",
"drive:dir:file", "drive:dir:file",
"drive:file", "drive:file",
"file", "file",
":file", ":file",
":dir:", ":dir:",
":dir:file"]: ":dir:file"]:
print `path`, '->', `pathname2url(path)` print `path`, '->', `pathname2url(path)`
if __name__ == '__main__': if __name__ == '__main__':
test() test()