home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / macpath.py < prev    next >
Text File  |  1993-12-29  |  3KB  |  127 lines

  1. # module 'macpath' -- pathname (or -related) operations for the Macintosh
  2.  
  3. import string
  4. import mac
  5. from stat import *
  6.  
  7.  
  8. # Normalize the case of a pathname.  Dummy in Posix, but string.lower here.
  9.  
  10. normcase = string.lower
  11.  
  12.  
  13. # Return true if a path is absolute.
  14. # On the Mac, relative paths begin with a colon,
  15. # but as a special case, paths with no colons at all are also relative.
  16. # Anything else is absolute (the string up to the first colon is the
  17. # volume name).
  18.  
  19. def isabs(s):
  20.     return ':' in s and s[0] <> ':'
  21.  
  22.  
  23. # Join two pathnames.
  24. # The result is equivalent to what the second pathname would refer to
  25. # if the first pathname were the current directory.
  26.  
  27. def join(s, t):
  28.     if (not s) or isabs(t): return t
  29.     if t[:1] == ':': t = t[1:]
  30.     if ':' not in s:
  31.         s = ':' + s
  32.     if s[-1:] <> ':':
  33.         s = s + ':'
  34.     return s + t
  35.  
  36.  
  37. # Split a pathname in two parts: the directory leading up to the final bit,
  38. # and the basename (the filename, without colons, in that directory).
  39. # The result (s, t) is such that join(s, t) yields the original argument.
  40.  
  41. def split(s):
  42.     if ':' not in s: return '', s
  43.     colon = 0
  44.     for i in range(len(s)):
  45.         if s[i] == ':': colon = i+1
  46.     return s[:colon], s[colon:]
  47.  
  48.  
  49. # Short interfaces to split()
  50.  
  51. def dirname(s): return split(s)[0]
  52. def basename(s): return split(s)[1]
  53.  
  54.  
  55. # XXX This is undocumented and may go away!
  56. # Normalize a pathname: get rid of '::' sequences by backing up,
  57. # e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
  58. # Raise the exception norm_error below if backing up is impossible,
  59. # e.g., for '::foo'.
  60.  
  61. norm_error = 'macpath.norm_error: path cannot be normalized'
  62.  
  63. def norm(s):
  64.     import string
  65.     if ':' not in s:
  66.         return ':' + s
  67.     f = string.splitfields(s, ':')
  68.     pre = []
  69.     post = []
  70.     if not f[0]:
  71.         pre = f[:1]
  72.         f = f[1:]
  73.     if not f[len(f)-1]:
  74.         post = f[-1:]
  75.         f = f[:-1]
  76.     res = []
  77.     for seg in f:
  78.         if seg:
  79.             res.append(seg)
  80.         else:
  81.             if not res: raise norm_error, 'path starts with ::'
  82.             del res[len(res)-1]
  83.             if not (pre or res):
  84.                 raise norm_error, 'path starts with volume::'
  85.     if pre: res = pre + res
  86.     if post: res = res + post
  87.     s = res[0]
  88.     for seg in res[1:]:
  89.         s = s + ':' + seg
  90.     return s
  91.  
  92.  
  93. # Return true if the pathname refers to an existing directory.
  94.  
  95. def isdir(s):
  96.     try:
  97.         st = mac.stat(s)
  98.     except mac.error:
  99.         return 0
  100.     return S_ISDIR(st[ST_MODE])
  101.  
  102.  
  103. # Return true if the pathname refers to an existing regular file.
  104.  
  105. def isfile(s):
  106.     try:
  107.         st = mac.stat(s)
  108.     except mac.error:
  109.         return 0
  110.     return S_ISREG(st[ST_MODE])
  111.  
  112.  
  113. # Return true if the pathname refers to an existing file or directory.
  114.  
  115. def exists(s):
  116.     try:
  117.         st = mac.stat(s)
  118.     except mac.error:
  119.         return 0
  120.     return 1
  121.  
  122.  
  123. # Normalize path, removing things like ...:A:..:... (yet to be written)
  124.  
  125. def normpath(s):
  126.     return s
  127.