home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / !Python / Lib / Lib1 / py / riscospath < prev    next >
Encoding:
Text File  |  1996-08-12  |  3.4 KB  |  161 lines

  1. # module 'riscospath' -- pathname (or -related) operations for RiscOS
  2. # hacked from the mac version.
  3.  
  4. import os
  5. import string
  6. import riscos
  7. from stat import *
  8.  
  9.  
  10. # Normalize the case of a pathname.  Dummy in Posix, but string.lower here.
  11.  
  12. normcase = string.lower
  13.  
  14.  
  15. # Return true if a path is absolute.
  16.  
  17. def isabs(s):
  18.     return '$' in s
  19.  
  20.  
  21. # Join two pathnames.
  22. # The result is equivalent to what the second pathname would refer to
  23. # if the first pathname were the current directory.
  24.  
  25. def join(s, t):
  26.     if (not s) or isabs(t): return t
  27.     if s[-1] != '.': s=s+'.' 
  28.     return s + t
  29.  
  30.  
  31. # Split a pathname in two parts: the directory leading up to the final bit,
  32. # and the basename (the filename, without colons, in that directory).
  33. # The result (s, t) is such that join(s, t) yields the original argument.
  34.  
  35. def split(s):
  36.     if '.' not in s: return '', s
  37.     stop = 0
  38.     for i in range(len(s)):
  39.         if s[i] == '.': stop = i+1
  40.     return s[:stop-1], s[stop:]
  41.  
  42.  
  43. # Short interfaces to split()
  44.  
  45. def dirname(s): return split(s)[0]
  46. def basename(s): return split(s)[1]
  47.  
  48. # Split a path in root and extension.
  49. # The extension is everything starting at the first '/' in the last
  50. # pathname component; the root is everything before that.
  51. # It is always true that root + ext == p.
  52.  
  53. def splitext(p):
  54.     root, ext = '', ''
  55.     for c in p:
  56.         if c in '.':
  57.             root, ext = root + ext + c, ''
  58.         elif c == '/' or ext:
  59.             ext = ext + c
  60.         else:
  61.             root = root + c
  62.     return root, ext
  63.  
  64. # Split into parts before and after a '$' ??
  65.  
  66. def splitdrive(p):
  67.   if '$' in p:
  68.      n=string.index(p,'$')
  69.      return p[:n],p[n:]
  70.   else:
  71.      return '',p
  72.  
  73. # Return the longest prefix of all list elements.
  74.  
  75. def commonprefix(m):
  76.     if not m: return ''
  77.     prefix = m[0]
  78.     for item in m:
  79.         for i in range(len(prefix)):
  80.             if prefix[:i+1] <> item[:i+1]:
  81.                 prefix = prefix[:i]
  82.                 if i == 0: return ''
  83.                 break
  84.     return prefix
  85.  
  86. # Is a path a symbolic link?
  87. # This will always return false on systems where posix.lstat doesn't exist.
  88.  
  89. def islink(path):
  90.     return false
  91.  
  92. # Return true if the pathname refers to an existing directory.
  93.  
  94. def isdir(s):
  95.     try:
  96.         st = riscos.stat(s)
  97.     except riscos.error:
  98.         return 0
  99.     return (st[ST_MODE]&S_IFDIR)!=0
  100.  
  101.  
  102. # Return true if the pathname refers to an existing regular file.
  103.  
  104. def isfile(s):
  105.     try:
  106.         st = riscos.stat(s)
  107.     except riscos.error:
  108.         return 0
  109.     return (st[ST_MODE]&S_IFREG)!=0
  110.  
  111. # Return true if the pathname refers to an existing image file.
  112.  
  113. def isimage(s):
  114.     try:
  115.         st = riscos.stat(s)
  116.     except riscos.error:
  117.         return 0
  118.     return (st[ST_OBTYPE]==3)
  119.  
  120. # Return true if the pathname refers to an existing file or directory.
  121.  
  122. def exists(s):
  123.     try:
  124.         st = riscos.stat(s)
  125.     except riscos.error:
  126.         return 0
  127.     return 1
  128.  
  129.  
  130. # Normalize path, removing things like ...:A:..:... 
  131.  
  132. def normpath(s):
  133.     try:
  134.     return string.lower(riscos.expand(s))
  135.     except:
  136.         return string.lower(s)
  137.  
  138. def expandvars(s):
  139.     try:
  140.         return riscos.expand(s)
  141.     except:
  142.         return s
  143.  
  144. # Directory tree walk.
  145. # For each directory under top (including top itself),
  146. # func(arg, dirname, filenames) is called, where
  147. # dirname is the name of the directory and filenames is the list
  148. # files files (and subdirectories etc.) in the directory.
  149. # The func may modify the filenames list, to implement a filter,
  150. # or to impose a different order of visiting.
  151.  
  152. def walk(top, func, arg):
  153.     try:
  154.         names = os.listdir(top)
  155.     except os.error:
  156.         return
  157.     func(arg, top, names)
  158.     for name in names:
  159.         name = join(top, name)
  160.         if isdir(name):
  161.             walk(name, func, arg)