home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_macpath.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  6.1 KB  |  228 lines

  1. """Pathname and path-related operations for the Macintosh."""
  2.  
  3. import os
  4. from stat import *
  5.  
  6. __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
  7.            "basename","dirname","commonprefix","getsize","getmtime",
  8.            "getatime","islink","exists","isdir","isfile",
  9.            "walk","expanduser","expandvars","normpath","abspath"]
  10.  
  11. # Normalize the case of a pathname.  Dummy in Posix, but <s>.lower() here.
  12.  
  13. def normcase(path):
  14.     return path.lower()
  15.  
  16.  
  17. def isabs(s):
  18.     """Return true if a path is absolute.
  19.     On the Mac, relative paths begin with a colon,
  20.     but as a special case, paths with no colons at all are also relative.
  21.     Anything else is absolute (the string up to the first colon is the
  22.     volume name)."""
  23.  
  24.     return ':' in s and s[0] != ':'
  25.  
  26.  
  27. def join(s, *p):
  28.     path = s
  29.     for t in p:
  30.         if (not s) or isabs(t):
  31.             path = t
  32.             continue
  33.         if t[:1] == ':':
  34.             t = t[1:]
  35.         if ':' not in path:
  36.             path = ':' + path
  37.         if path[-1:] != ':':
  38.             path = path + ':'
  39.         path = path + t
  40.     return path
  41.  
  42.  
  43. def split(s):
  44.     """Split a pathname into two parts: the directory leading up to the final
  45.     bit, and the basename (the filename, without colons, in that directory).
  46.     The result (s, t) is such that join(s, t) yields the original argument."""
  47.  
  48.     if ':' not in s: return '', s
  49.     colon = 0
  50.     for i in range(len(s)):
  51.         if s[i] == ':': colon = i + 1
  52.     path, file = s[:colon-1], s[colon:]
  53.     if path and not ':' in path:
  54.         path = path + ':'
  55.     return path, file
  56.  
  57.  
  58. def splitext(p):
  59.     """Split a path into root and extension.
  60.     The extension is everything starting at the last dot in the last
  61.     pathname component; the root is everything before that.
  62.     It is always true that root + ext == p."""
  63.  
  64.     root, ext = '', ''
  65.     for c in p:
  66.         if c == ':':
  67.             root, ext = root + ext + c, ''
  68.         elif c == '.':
  69.             if ext:
  70.                 root, ext = root + ext, c
  71.             else:
  72.                 ext = c
  73.         elif ext:
  74.             ext = ext + c
  75.         else:
  76.             root = root + c
  77.     return root, ext
  78.  
  79.  
  80. def splitdrive(p):
  81.     """Split a pathname into a drive specification and the rest of the
  82.     path.  Useful on DOS/Windows/NT; on the Mac, the drive is always
  83.     empty (don't use the volume name -- it doesn't have the same
  84.     syntactic and semantic oddities as DOS drive letters, such as there
  85.     being a separate current directory per drive)."""
  86.  
  87.     return '', p
  88.  
  89.  
  90. # Short interfaces to split()
  91.  
  92. def dirname(s): return split(s)[0]
  93. def basename(s): return split(s)[1]
  94.  
  95.  
  96. def isdir(s):
  97.     """Return true if the pathname refers to an existing directory."""
  98.  
  99.     try:
  100.         st = os.stat(s)
  101.     except os.error:
  102.         return 0
  103.     return S_ISDIR(st[ST_MODE])
  104.  
  105.  
  106. # Get size, mtime, atime of files.
  107.  
  108. def getsize(filename):
  109.     """Return the size of a file, reported by os.stat()."""
  110.     st = os.stat(filename)
  111.     return st[ST_SIZE]
  112.  
  113. def getmtime(filename):
  114.     """Return the last modification time of a file, reported by os.stat()."""
  115.     st = os.stat(filename)
  116.     return st[ST_MTIME]
  117.  
  118. def getatime(filename):
  119.     """Return the last access time of a file, reported by os.stat()."""
  120.     st = os.stat(filename)
  121.     return st[ST_ATIME]
  122.  
  123.  
  124. def islink(s):
  125.     """Return true if the pathname refers to a symbolic link.
  126.     Always false on the Mac, until we understand Aliases.)"""
  127.  
  128.     return 0
  129.  
  130.  
  131. def isfile(s):
  132.     """Return true if the pathname refers to an existing regular file."""
  133.  
  134.     try:
  135.         st = os.stat(s)
  136.     except os.error:
  137.         return 0
  138.     return S_ISREG(st[ST_MODE])
  139.  
  140.  
  141. def exists(s):
  142.     """Return true if the pathname refers to an existing file or directory."""
  143.  
  144.     try:
  145.         st = os.stat(s)
  146.     except os.error:
  147.         return 0
  148.     return 1
  149.  
  150. # Return the longest prefix of all list elements.
  151.  
  152. def commonprefix(m):
  153.     "Given a list of pathnames, returns the longest common leading component"
  154.     if not m: return ''
  155.     prefix = m[0]
  156.     for item in m:
  157.         for i in range(len(prefix)):
  158.             if prefix[:i+1] != item[:i+1]:
  159.                 prefix = prefix[:i]
  160.                 if i == 0: return ''
  161.                 break
  162.     return prefix
  163.  
  164. def expandvars(path):
  165.     """Dummy to retain interface-compatibility with other operating systems."""
  166.     return path
  167.  
  168.  
  169. def expanduser(path):
  170.     """Dummy to retain interface-compatibility with other operating systems."""
  171.     return path
  172.  
  173. norm_error = 'macpath.norm_error: path cannot be normalized'
  174.  
  175. def normpath(s):
  176.     """Normalize a pathname.  Will return the same result for
  177.     equivalent paths."""
  178.  
  179.     if ":" not in s:
  180.         return ":"+s
  181.  
  182.     comps = s.split(":")
  183.     i = 1
  184.     while i < len(comps)-1:
  185.         if comps[i] == "" and comps[i-1] != "":
  186.             if i > 1:
  187.                 del comps[i-1:i+1]
  188.                 i = i - 1
  189.             else:
  190.                 # best way to handle this is to raise an exception
  191.                 raise norm_error, 'Cannot use :: immedeately after volume name'
  192.         else:
  193.             i = i + 1
  194.  
  195.     s = ":".join(comps)
  196.  
  197.     # remove trailing ":" except for ":" and "Volume:"
  198.     if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
  199.         s = s[:-1]
  200.     return s
  201.  
  202.  
  203. def walk(top, func, arg):
  204.     """Directory tree walk.
  205.     For each directory under top (including top itself),
  206.     func(arg, dirname, filenames) is called, where
  207.     dirname is the name of the directory and filenames is the list
  208.     of files (and subdirectories etc.) in the directory.
  209.     The func may modify the filenames list, to implement a filter,
  210.     or to impose a different order of visiting."""
  211.  
  212.     try:
  213.         names = os.listdir(top)
  214.     except os.error:
  215.         return
  216.     func(arg, top, names)
  217.     for name in names:
  218.         name = join(top, name)
  219.         if isdir(name):
  220.             walk(name, func, arg)
  221.  
  222.  
  223. def abspath(path):
  224.     """Return an absolute path."""
  225.     if not isabs(path):
  226.         path = join(os.getcwd(), path)
  227.     return normpath(path)
  228.