home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / ntpath.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  9.3 KB  |  383 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import os
  5. import stat
  6. __all__ = [
  7.     'normcase',
  8.     'isabs',
  9.     'join',
  10.     'splitdrive',
  11.     'split',
  12.     'splitext',
  13.     'basename',
  14.     'dirname',
  15.     'commonprefix',
  16.     'getsize',
  17.     'getmtime',
  18.     'getatime',
  19.     'islink',
  20.     'exists',
  21.     'isdir',
  22.     'isfile',
  23.     'ismount',
  24.     'walk',
  25.     'expanduser',
  26.     'expandvars',
  27.     'normpath',
  28.     'abspath',
  29.     'splitunc']
  30.  
  31. def normcase(s):
  32.     return s.replace('/', '\\').lower()
  33.  
  34.  
  35. def isabs(s):
  36.     s = splitdrive(s)[1]
  37.     if s != '':
  38.         pass
  39.     return s[:1] in '/\\'
  40.  
  41.  
  42. def join(a, *p):
  43.     path = a
  44.     for b in p:
  45.         b_wins = 0
  46.         if path == '':
  47.             b_wins = 1
  48.         elif isabs(b):
  49.             if path[1:2] != ':' or b[1:2] == ':':
  50.                 b_wins = 1
  51.             elif len(path) > 3 and len(path) == 3 and path[-1] not in '/\\':
  52.                 b_wins = 1
  53.             
  54.         
  55.         if b_wins:
  56.             path = b
  57.         elif path[-1] in '/\\':
  58.             if b and b[0] in '/\\':
  59.                 path += b[1:]
  60.             else:
  61.                 path += b
  62.         elif path[-1] == ':':
  63.             path += b
  64.         elif b:
  65.             if b[0] in '/\\':
  66.                 path += b
  67.             else:
  68.                 path += '\\' + b
  69.         else:
  70.             path += '\\'
  71.     
  72.     return path
  73.  
  74.  
  75. def splitdrive(p):
  76.     if p[1:2] == ':':
  77.         return (p[0:2], p[2:])
  78.     
  79.     return ('', p)
  80.  
  81.  
  82. def splitunc(p):
  83.     if p[1:2] == ':':
  84.         return ('', p)
  85.     
  86.     firstTwo = p[0:2]
  87.     if firstTwo == '//' or firstTwo == '\\\\':
  88.         normp = normcase(p)
  89.         index = normp.find('\\', 2)
  90.         if index == -1:
  91.             return ('', p)
  92.         
  93.         index = normp.find('\\', index + 1)
  94.         if index == -1:
  95.             index = len(p)
  96.         
  97.         return (p[:index], p[index:])
  98.     
  99.     return ('', p)
  100.  
  101.  
  102. def split(p):
  103.     (d, p) = splitdrive(p)
  104.     i = len(p)
  105.     while i and p[i - 1] not in '/\\':
  106.         i = i - 1
  107.     (head, tail) = (p[:i], p[i:])
  108.     head2 = head
  109.     while head2 and head2[-1] in '/\\':
  110.         head2 = head2[:-1]
  111.     if not head2:
  112.         pass
  113.     head = head
  114.     return (d + head, tail)
  115.  
  116.  
  117. def splitext(p):
  118.     (root, ext) = ('', '')
  119.     for c in p:
  120.         if c in [
  121.             '/',
  122.             '\\']:
  123.             (root, ext) = (root + ext + c, '')
  124.         elif c == '.':
  125.             if ext:
  126.                 (root, ext) = (root + ext, c)
  127.             else:
  128.                 ext = c
  129.         elif ext:
  130.             ext = ext + c
  131.         else:
  132.             root = root + c
  133.     
  134.     return (root, ext)
  135.  
  136.  
  137. def basename(p):
  138.     return split(p)[1]
  139.  
  140.  
  141. def dirname(p):
  142.     return split(p)[0]
  143.  
  144.  
  145. def commonprefix(m):
  146.     if not m:
  147.         return ''
  148.     
  149.     prefix = m[0]
  150.     for item in m:
  151.         for i in range(len(prefix)):
  152.             if prefix[:i + 1] != item[:i + 1]:
  153.                 prefix = prefix[:i]
  154.                 if i == 0:
  155.                     return ''
  156.                 
  157.                 break
  158.             
  159.         
  160.     
  161.     return prefix
  162.  
  163.  
  164. def getsize(filename):
  165.     st = os.stat(filename)
  166.     return st[stat.ST_SIZE]
  167.  
  168.  
  169. def getmtime(filename):
  170.     st = os.stat(filename)
  171.     return st[stat.ST_MTIME]
  172.  
  173.  
  174. def getatime(filename):
  175.     st = os.stat(filename)
  176.     return st[stat.ST_ATIME]
  177.  
  178.  
  179. def islink(path):
  180.     return 0
  181.  
  182.  
  183. def exists(path):
  184.     
  185.     try:
  186.         st = os.stat(path)
  187.     except os.error:
  188.         return 0
  189.  
  190.     return 1
  191.  
  192.  
  193. def isdir(path):
  194.     
  195.     try:
  196.         st = os.stat(path)
  197.     except os.error:
  198.         return 0
  199.  
  200.     return stat.S_ISDIR(st[stat.ST_MODE])
  201.  
  202.  
  203. def isfile(path):
  204.     
  205.     try:
  206.         st = os.stat(path)
  207.     except os.error:
  208.         return 0
  209.  
  210.     return stat.S_ISREG(st[stat.ST_MODE])
  211.  
  212.  
  213. def ismount(path):
  214.     (unc, rest) = splitunc(path)
  215.     if unc:
  216.         return rest in ('', '/', '\\')
  217.     
  218.     p = splitdrive(path)[1]
  219.     if len(p) == 1:
  220.         pass
  221.     return p[0] in '/\\'
  222.  
  223.  
  224. def walk(top, func, arg):
  225.     
  226.     try:
  227.         names = os.listdir(top)
  228.     except os.error:
  229.         return None
  230.  
  231.     func(arg, top, names)
  232.     exceptions = ('.', '..')
  233.     for name in names:
  234.         if name not in exceptions:
  235.             name = join(top, name)
  236.             if isdir(name):
  237.                 walk(name, func, arg)
  238.             
  239.         
  240.     
  241.  
  242.  
  243. def expanduser(path):
  244.     if path[:1] != '~':
  245.         return path
  246.     
  247.     (i, n) = (1, len(path))
  248.     while i < n and path[i] not in '/\\':
  249.         i = i + 1
  250.     if i == 1:
  251.         if os.environ.has_key('HOME'):
  252.             userhome = os.environ['HOME']
  253.         elif not os.environ.has_key('HOMEPATH'):
  254.             return path
  255.         else:
  256.             
  257.             try:
  258.                 drive = os.environ['HOMEDRIVE']
  259.             except KeyError:
  260.                 drive = ''
  261.  
  262.             userhome = join(drive, os.environ['HOMEPATH'])
  263.     else:
  264.         return path
  265.     return userhome + path[i:]
  266.  
  267.  
  268. def expandvars(path):
  269.     if '$' not in path:
  270.         return path
  271.     
  272.     import string
  273.     varchars = string.ascii_letters + string.digits + '_-'
  274.     res = ''
  275.     index = 0
  276.     pathlen = len(path)
  277.     while index < pathlen:
  278.         c = path[index]
  279.         if c == "'":
  280.             path = path[index + 1:]
  281.             pathlen = len(path)
  282.             
  283.             try:
  284.                 index = path.index("'")
  285.                 res = res + "'" + path[:index + 1]
  286.             except ValueError:
  287.                 res = res + path
  288.                 index = pathlen - 1
  289.  
  290.         elif c == '$':
  291.             if path[index + 1:index + 2] == '$':
  292.                 res = res + c
  293.                 index = index + 1
  294.             elif path[index + 1:index + 2] == '{':
  295.                 path = path[index + 2:]
  296.                 pathlen = len(path)
  297.                 
  298.                 try:
  299.                     index = path.index('}')
  300.                     var = path[:index]
  301.                     if os.environ.has_key(var):
  302.                         res = res + os.environ[var]
  303.                 except ValueError:
  304.                     res = res + path
  305.                     index = pathlen - 1
  306.  
  307.             else:
  308.                 var = ''
  309.                 index = index + 1
  310.                 c = path[index:index + 1]
  311.                 while c != '' and c in varchars:
  312.                     var = var + c
  313.                     index = index + 1
  314.                     c = path[index:index + 1]
  315.                 if os.environ.has_key(var):
  316.                     res = res + os.environ[var]
  317.                 
  318.                 if c != '':
  319.                     res = res + c
  320.                 
  321.         else:
  322.             res = res + c
  323.         index = index + 1
  324.     return res
  325.  
  326.  
  327. def normpath(path):
  328.     path = path.replace('/', '\\')
  329.     (prefix, path) = splitdrive(path)
  330.     while path[:1] == '\\':
  331.         prefix = prefix + '\\'
  332.         path = path[1:]
  333.     comps = path.split('\\')
  334.     i = 0
  335.     while i < len(comps):
  336.         if comps[i] in ('.', ''):
  337.             del comps[i]
  338.         elif comps[i] == '..':
  339.             if i > 0 and comps[i - 1] != '..':
  340.                 del comps[i - 1:i + 1]
  341.                 i -= 1
  342.             elif i == 0 and prefix.endswith('\\'):
  343.                 del comps[i]
  344.             else:
  345.                 i += 1
  346.         else:
  347.             i += 1
  348.     if not prefix and not comps:
  349.         comps.append('.')
  350.     
  351.     return prefix + '\\'.join(comps)
  352.  
  353.  
  354. def abspath(path):
  355.     global abspath
  356.     
  357.     try:
  358.         _getfullpathname = _getfullpathname
  359.         import nt
  360.     except ImportError:
  361.         
  362.         def _abspath(path):
  363.             if not isabs(path):
  364.                 path = join(os.getcwd(), path)
  365.             
  366.             return normpath(path)
  367.  
  368.         abspath = _abspath
  369.         return _abspath(path)
  370.  
  371.     if path:
  372.         
  373.         try:
  374.             path = _getfullpathname(path)
  375.         except WindowsError:
  376.             pass
  377.  
  378.     else:
  379.         path = os.getcwd()
  380.     return normpath(path)
  381.  
  382. realpath = abspath
  383.