home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / NTPATH.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-30  |  11.2 KB  |  369 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Common pathname manipulations, WindowsNT/95 version. 
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. import string
  12.  
  13. def normcase(s):
  14.     '''Normalize case of pathname.
  15.  
  16.     Makes all characters lowercase and all slashes into backslashes.'''
  17.     return string.lower(string.replace(s, '/', '\\'))
  18.  
  19.  
  20. def isabs(s):
  21.     '''Test whether a path is absolute'''
  22.     s = splitdrive(s)[1]
  23.     if s != '':
  24.         pass
  25.     return s[:1] in '/\\'
  26.  
  27.  
  28. def join(a, *p):
  29.     '''Join two or more pathname components, inserting "\\" as needed'''
  30.     path = a
  31.     for b in p:
  32.         if isabs(b):
  33.             path = b
  34.         elif path == '' or path[-1:] in '/\\':
  35.             path = path + b
  36.         else:
  37.             path = path + os.sep + b
  38.     
  39.     return path
  40.  
  41.  
  42. def splitdrive(p):
  43.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  44. "(drive,path)";  either part may be empty'''
  45.     if p[1:2] == ':':
  46.         return (p[0:2], p[2:])
  47.     
  48.     return ('', p)
  49.  
  50.  
  51. def splitunc(p):
  52.     """Split a pathname into UNC mount point and relative path specifiers.
  53.  
  54.     Return a 2-tuple (unc, rest); either part may be empty.
  55.     If unc is not empty, it has the form '//host/mount' (or similar
  56.     using backslashes).  unc+rest is always the input path.
  57.     Paths containing drive letters never have an UNC part.
  58.     """
  59.     if p[1:2] == ':':
  60.         return ('', p)
  61.     
  62.     firstTwo = p[0:2]
  63.     if firstTwo == '//' or firstTwo == '\\\\':
  64.         normp = normcase(p)
  65.         index = string.find(normp, '\\', 2)
  66.         if index == -1:
  67.             return ('', p)
  68.         
  69.         index = string.find(normp, '\\', index + 1)
  70.         if index == -1:
  71.             index = len(p)
  72.         
  73.         return (p[:index], p[index:])
  74.     
  75.     return ('', p)
  76.  
  77.  
  78. def split(p):
  79.     '''Split a pathname.
  80.  
  81.     Return tuple (head, tail) where tail is everything after the final slash.
  82.     Either part may be empty.'''
  83.     (d, p) = splitdrive(p)
  84.     i = len(p)
  85.     while i and p[i - 1] not in '/\\':
  86.         i = i - 1
  87.     (head, tail) = (p[:i], p[i:])
  88.     head2 = head
  89.     while head2 and head2[-1] in '/\\':
  90.         head2 = head2[:-1]
  91.     if not head2:
  92.         pass
  93.     head = head
  94.     return (d + head, tail)
  95.  
  96.  
  97. def splitext(p):
  98.     '''Split the extension from a pathname.
  99.  
  100.     Extension is everything from the last dot to the end.
  101.     Return (root, ext), either part may be empty.'''
  102.     (root, ext) = ('', '')
  103.     for c in p:
  104.         if c in [
  105.             '/',
  106.             '\\']:
  107.             (root, ext) = (root + ext + c, '')
  108.         elif c == '.':
  109.             if ext:
  110.                 (root, ext) = (root + ext, c)
  111.             else:
  112.                 ext = c
  113.         elif ext:
  114.             ext = ext + c
  115.         else:
  116.             root = root + c
  117.     
  118.     return (root, ext)
  119.  
  120.  
  121. def basename(p):
  122.     '''Returns the final component of a pathname'''
  123.     return split(p)[1]
  124.  
  125.  
  126. def dirname(p):
  127.     '''Returns the directory component of a pathname'''
  128.     return split(p)[0]
  129.  
  130.  
  131. def commonprefix(m):
  132.     '''Given a list of pathnames, returns the longest common leading component'''
  133.     if not m:
  134.         return ''
  135.     
  136.     prefix = m[0]
  137.     for item in m:
  138.         for i in range(len(prefix)):
  139.             pass
  140.         
  141.     
  142.     return prefix
  143.  
  144.  
  145. def getsize(filename):
  146.     '''Return the size of a file, reported by os.stat()'''
  147.     st = os.stat(filename)
  148.     return st[stat.ST_SIZE]
  149.  
  150.  
  151. def getmtime(filename):
  152.     '''Return the last modification time of a file, reported by os.stat()'''
  153.     st = os.stat(filename)
  154.     return st[stat.ST_MTIME]
  155.  
  156.  
  157. def getatime(filename):
  158.     '''Return the last access time of a file, reported by os.stat()'''
  159.     st = os.stat(filename)
  160.     return st[stat.ST_MTIME]
  161.  
  162.  
  163. def islink(path):
  164.     '''Test for symbolic link.  On WindowsNT/95 always returns false'''
  165.     return 0
  166.  
  167.  
  168. def exists(path):
  169.     '''Test whether a path exists'''
  170.     
  171.     try:
  172.         st = os.stat(path)
  173.     except os.error:
  174.         return 0
  175.  
  176.     return 1
  177.  
  178.  
  179. def isdir(path):
  180.     '''Test whether a path is a directory'''
  181.     
  182.     try:
  183.         st = os.stat(path)
  184.     except os.error:
  185.         return 0
  186.  
  187.     return stat.S_ISDIR(st[stat.ST_MODE])
  188.  
  189.  
  190. def isfile(path):
  191.     '''Test whether a path is a regular file'''
  192.     
  193.     try:
  194.         st = os.stat(path)
  195.     except os.error:
  196.         return 0
  197.  
  198.     return stat.S_ISREG(st[stat.ST_MODE])
  199.  
  200.  
  201. def ismount(path):
  202.     '''Test whether a path is a mount point (defined as root of drive)'''
  203.     (unc, rest) = splitunc(path)
  204.     if unc:
  205.         return rest in ('', '/', '\\')
  206.     
  207.     p = splitdrive(path)[1]
  208.     if len(p) == 1:
  209.         pass
  210.     return p[0] in '/\\'
  211.  
  212.  
  213. def walk(top, func, arg):
  214.     '''Directory tree walk whth callback function.
  215.  
  216.     walk(top, func, args) calls func(arg, d, files) for each directory d 
  217.     in the tree rooted at top (including top itself); files is a list
  218.     of all the files and subdirs in directory d.'''
  219.     
  220.     try:
  221.         names = os.listdir(top)
  222.     except os.error:
  223.         return None
  224.  
  225.     func(arg, top, names)
  226.     exceptions = ('.', '..')
  227.     for name in names:
  228.         if name not in exceptions:
  229.             name = join(top, name)
  230.             if isdir(name):
  231.                 walk(name, func, arg)
  232.             
  233.         
  234.     
  235.  
  236.  
  237. def expanduser(path):
  238.     '''Expand ~ and ~user constructs.
  239.  
  240.     If user or $HOME is unknown, do nothing.'''
  241.     if path[:1] != '~':
  242.         return path
  243.     
  244.     (i, n) = (1, len(path))
  245.     while i < n and path[i] not in '/\\':
  246.         i = i + 1
  247.     if i == 1:
  248.         if os.environ.has_key('HOME'):
  249.             userhome = os.environ['HOME']
  250.         elif not os.environ.has_key('HOMEPATH'):
  251.             return path
  252.         else:
  253.             
  254.             try:
  255.                 drive = os.environ['HOMEDRIVE']
  256.             except KeyError:
  257.                 drive = ''
  258.  
  259.             userhome = join(drive, os.environ['HOMEPATH'])
  260.     else:
  261.         return path
  262.     return userhome + path[i:]
  263.  
  264. varchars = string.letters + string.digits + '_-'
  265.  
  266. def expandvars(path):
  267.     '''Expand shell variables of form $var and ${var}.
  268.  
  269.     Unknown variables are left unchanged.'''
  270.     if '$' not in path:
  271.         return path
  272.     
  273.     res = ''
  274.     index = 0
  275.     pathlen = len(path)
  276.     while index < pathlen:
  277.         c = path[index]
  278.         if c == "'":
  279.             path = path[index + 1:]
  280.             pathlen = len(path)
  281.             
  282.             try:
  283.                 index = string.index(path, "'")
  284.                 res = res + "'" + path[:index + 1]
  285.             except string.index_error:
  286.                 res = res + path
  287.                 index = pathlen - 1
  288.  
  289.         elif c == '$':
  290.             if path[index + 1:index + 2] == '$':
  291.                 res = res + c
  292.                 index = index + 1
  293.             elif path[index + 1:index + 2] == '{':
  294.                 path = path[index + 2:]
  295.                 pathlen = len(path)
  296.                 
  297.                 try:
  298.                     index = string.index(path, '}')
  299.                     var = path[:index]
  300.                     if os.environ.has_key(var):
  301.                         res = res + os.environ[var]
  302.                 except string.index_error:
  303.                     res = res + path
  304.                     index = pathlen - 1
  305.  
  306.             else:
  307.                 var = ''
  308.                 index = index + 1
  309.                 c = path[index:index + 1]
  310.                 while c != '' and c in varchars:
  311.                     var = var + c
  312.                     index = index + 1
  313.                     c = path[index:index + 1]
  314.                 if os.environ.has_key(var):
  315.                     res = res + os.environ[var]
  316.                 
  317.                 if c != '':
  318.                     res = res + c
  319.                 
  320.         else:
  321.             res = res + c
  322.         index = index + 1
  323.     return res
  324.  
  325.  
  326. def normpath(path):
  327.     '''Normalize path, eliminating double slashes, etc.'''
  328.     path = string.replace(path, '/', '\\')
  329.     (prefix, path) = splitdrive(path)
  330.     while path[:1] == os.sep:
  331.         prefix = prefix + os.sep
  332.         path = path[1:]
  333.     comps = string.splitfields(path, os.sep)
  334.     i = 0
  335.     while i < len(comps):
  336.         if comps[i] == '.':
  337.             del comps[i]
  338.         elif comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  339.             del comps[i - 1:i + 1]
  340.             i = i - 1
  341.         elif comps[i] == '' and i > 0 and comps[i - 1] != '':
  342.             del comps[i]
  343.         else:
  344.             i = i + 1
  345.     if not prefix and not comps:
  346.         comps.append('.')
  347.     
  348.     return prefix + string.joinfields(comps, os.sep)
  349.  
  350.  
  351. def abspath(path):
  352.     '''Return the absolute version of a path'''
  353.     
  354.     try:
  355.         import win32api
  356.         
  357.         try:
  358.             return win32api.GetFullPathName(path)
  359.         except win32api.error:
  360.             return path
  361.  
  362.     except ImportError:
  363.         if not isabs(path):
  364.             path = join(os.getcwd(), path)
  365.         
  366.         return normpath(path)
  367.  
  368.  
  369.