home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / python / Lib / ntpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-03-27  |  11.3 KB  |  470 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  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 sys
  11. import stat
  12. import genericpath
  13. import warnings
  14. from genericpath import *
  15. __all__ = [
  16.     'normcase',
  17.     'isabs',
  18.     'join',
  19.     'splitdrive',
  20.     'split',
  21.     'splitext',
  22.     'basename',
  23.     'dirname',
  24.     'commonprefix',
  25.     'getsize',
  26.     'getmtime',
  27.     'getatime',
  28.     'getctime',
  29.     'islink',
  30.     'exists',
  31.     'lexists',
  32.     'isdir',
  33.     'isfile',
  34.     'ismount',
  35.     'walk',
  36.     'expanduser',
  37.     'expandvars',
  38.     'normpath',
  39.     'abspath',
  40.     'splitunc',
  41.     'curdir',
  42.     'pardir',
  43.     'sep',
  44.     'pathsep',
  45.     'defpath',
  46.     'altsep',
  47.     'extsep',
  48.     'devnull',
  49.     'realpath',
  50.     'supports_unicode_filenames',
  51.     'relpath']
  52. curdir = '.'
  53. pardir = '..'
  54. extsep = '.'
  55. sep = '\\'
  56. pathsep = ';'
  57. altsep = '/'
  58. defpath = '.;C:\\bin'
  59. if 'ce' in sys.builtin_module_names:
  60.     defpath = '\\Windows'
  61. elif 'os2' in sys.builtin_module_names:
  62.     altsep = '/'
  63.  
  64. devnull = 'nul'
  65.  
  66. def normcase(s):
  67.     '''Normalize case of pathname.
  68.  
  69.     Makes all characters lowercase and all slashes into backslashes.'''
  70.     return s.replace('/', '\\').lower()
  71.  
  72.  
  73. def isabs(s):
  74.     '''Test whether a path is absolute'''
  75.     s = splitdrive(s)[1]
  76.     if s != '':
  77.         pass
  78.     return s[:1] in '/\\'
  79.  
  80.  
  81. def join(a, *p):
  82.     '''Join two or more pathname components, inserting "\\" as needed.
  83.     If any component is an absolute path, all previous path components
  84.     will be discarded.'''
  85.     path = a
  86.     for b in p:
  87.         b_wins = 0
  88.         if path == '':
  89.             b_wins = 1
  90.         elif isabs(b):
  91.             if path[1:2] != ':' or b[1:2] == ':':
  92.                 b_wins = 1
  93.             elif (len(path) > 3 or len(path) == 3) and path[-1] not in '/\\':
  94.                 b_wins = 1
  95.             
  96.         
  97.         if b_wins:
  98.             path = b
  99.             continue
  100.         if not len(path) > 0:
  101.             raise AssertionError
  102.         if path[-1] in '/\\':
  103.             if b and b[0] in '/\\':
  104.                 path += b[1:]
  105.             else:
  106.                 path += b
  107.         b[0] in '/\\'
  108.         if path[-1] == ':':
  109.             path += b
  110.             continue
  111.         len(path) > 0
  112.         if b:
  113.             if b[0] in '/\\':
  114.                 path += b
  115.             else:
  116.                 path += '\\' + b
  117.         b[0] in '/\\'
  118.         path += '\\'
  119.     
  120.     return path
  121.  
  122.  
  123. def splitdrive(p):
  124.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  125. "(drive,path)";  either part may be empty'''
  126.     if p[1:2] == ':':
  127.         return (p[0:2], p[2:])
  128.     return ('', p)
  129.  
  130.  
  131. def splitunc(p):
  132.     """Split a pathname into UNC mount point and relative path specifiers.
  133.  
  134.     Return a 2-tuple (unc, rest); either part may be empty.
  135.     If unc is not empty, it has the form '//host/mount' (or similar
  136.     using backslashes).  unc+rest is always the input path.
  137.     Paths containing drive letters never have an UNC part.
  138.     """
  139.     if p[1:2] == ':':
  140.         return ('', p)
  141.     firstTwo = p[0:2]
  142.     if firstTwo == '//' or firstTwo == '\\\\':
  143.         normp = normcase(p)
  144.         index = normp.find('\\', 2)
  145.         if index == -1:
  146.             return ('', p)
  147.         index = normp.find('\\', index + 1)
  148.         return (p[:index], p[index:])
  149.     return ('', p)
  150.  
  151.  
  152. def split(p):
  153.     '''Split a pathname.
  154.  
  155.     Return tuple (head, tail) where tail is everything after the final slash.
  156.     Either part may be empty.'''
  157.     (d, p) = splitdrive(p)
  158.     i = len(p)
  159.     while i and p[i - 1] not in '/\\':
  160.         i = i - 1
  161.     head = p[:i]
  162.     tail = p[i:]
  163.     head2 = head
  164.     while head2 and head2[-1] in '/\\':
  165.         head2 = head2[:-1]
  166.     if not head2:
  167.         pass
  168.     head = head
  169.     return (d + head, tail)
  170.  
  171.  
  172. def splitext(p):
  173.     return genericpath._splitext(p, sep, altsep, extsep)
  174.  
  175. splitext.__doc__ = genericpath._splitext.__doc__
  176.  
  177. def basename(p):
  178.     '''Returns the final component of a pathname'''
  179.     return split(p)[1]
  180.  
  181.  
  182. def dirname(p):
  183.     '''Returns the directory component of a pathname'''
  184.     return split(p)[0]
  185.  
  186.  
  187. def islink(path):
  188.     '''Test for symbolic link.
  189.     On WindowsNT/95 and OS/2 always returns false
  190.     '''
  191.     return False
  192.  
  193. lexists = exists
  194.  
  195. def ismount(path):
  196.     '''Test whether a path is a mount point (defined as root of drive)'''
  197.     (unc, rest) = splitunc(path)
  198.     if unc:
  199.         return rest in ('', '/', '\\')
  200.     p = splitdrive(path)[1]
  201.     if len(p) == 1:
  202.         pass
  203.     return p[0] in '/\\'
  204.  
  205.  
  206. def walk(top, func, arg):
  207.     """Directory tree walk with callback function.
  208.  
  209.     For each directory in the directory tree rooted at top (including top
  210.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  211.     dirname is the name of the directory, and fnames a list of the names of
  212.     the files and subdirectories in dirname (excluding '.' and '..').  func
  213.     may modify the fnames list in-place (e.g. via del or slice assignment),
  214.     and walk will only recurse into the subdirectories whose names remain in
  215.     fnames; this can be used to implement a filter, or to impose a specific
  216.     order of visiting.  No semantics are defined for, or required of, arg,
  217.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  218.     a filename pattern, or a mutable object designed to accumulate
  219.     statistics.  Passing None for arg is common."""
  220.     warnings.warnpy3k('In 3.x, os.path.walk is removed in favor of os.walk.', stacklevel = 2)
  221.     
  222.     try:
  223.         names = os.listdir(top)
  224.     except os.error:
  225.         return None
  226.  
  227.     func(arg, top, names)
  228.     for name in names:
  229.         name = join(top, name)
  230.         if isdir(name):
  231.             walk(name, func, arg)
  232.             continue
  233.     
  234.  
  235.  
  236. def expanduser(path):
  237.     '''Expand ~ and ~user constructs.
  238.  
  239.     If user or $HOME is unknown, do nothing.'''
  240.     if path[:1] != '~':
  241.         return path
  242.     i = 1
  243.     n = len(path)
  244.     while i < n and path[i] not in '/\\':
  245.         i = i + 1
  246.         continue
  247.         path[:1] != '~'
  248.     if 'HOME' in os.environ:
  249.         userhome = os.environ['HOME']
  250.     elif 'USERPROFILE' in os.environ:
  251.         userhome = os.environ['USERPROFILE']
  252.     elif 'HOMEPATH' not in os.environ:
  253.         return path
  254.     
  255.     try:
  256.         drive = os.environ['HOMEDRIVE']
  257.     except KeyError:
  258.         drive = ''
  259.  
  260.     userhome = join(drive, os.environ['HOMEPATH'])
  261.     if i != 1:
  262.         userhome = join(dirname(userhome), path[1:i])
  263.     
  264.     return userhome + path[i:]
  265.  
  266.  
  267. def expandvars(path):
  268.     '''Expand shell variables of the forms $var, ${var} and %var%.
  269.  
  270.     Unknown variables are left unchanged.'''
  271.     if '$' not in path and '%' not in path:
  272.         return path
  273.     import string
  274.     varchars = string.ascii_letters + string.digits + '_-'
  275.     res = ''
  276.     index = 0
  277.     pathlen = len(path)
  278.     while index < pathlen:
  279.         c = path[index]
  280.         if c == "'":
  281.             path = path[index + 1:]
  282.             pathlen = len(path)
  283.             
  284.             try:
  285.                 index = path.index("'")
  286.                 res = res + "'" + path[:index + 1]
  287.             except ValueError:
  288.                 '%' not in path
  289.                 '%' not in path
  290.                 res = res + path
  291.                 index = pathlen - 1
  292.             except:
  293.                 '%' not in path<EXCEPTION MATCH>ValueError
  294.             
  295.  
  296.         '%' not in path
  297.         if c == '%':
  298.             if path[index + 1:index + 2] == '%':
  299.                 res = res + c
  300.                 index = index + 1
  301.             else:
  302.                 path = path[index + 1:]
  303.                 pathlen = len(path)
  304.                 
  305.                 try:
  306.                     index = path.index('%')
  307.                 except ValueError:
  308.                     '%' not in path
  309.                     '%' not in path
  310.                     res = res + '%' + path
  311.                     index = pathlen - 1
  312.                 except:
  313.                     '%' not in path
  314.  
  315.                 var = path[:index]
  316.                 if var in os.environ:
  317.                     res = res + os.environ[var]
  318.                 else:
  319.                     res = res + '%' + var + '%'
  320.         elif c == '$':
  321.             if path[index + 1:index + 2] == '$':
  322.                 res = res + c
  323.                 index = index + 1
  324.             elif path[index + 1:index + 2] == '{':
  325.                 path = path[index + 2:]
  326.                 pathlen = len(path)
  327.                 
  328.                 try:
  329.                     index = path.index('}')
  330.                     var = path[:index]
  331.                     if var in os.environ:
  332.                         res = res + os.environ[var]
  333.                     else:
  334.                         res = res + '${' + var + '}'
  335.                 except ValueError:
  336.                     '%' not in path
  337.                     '%' not in path
  338.                     res = res + '${' + path
  339.                     index = pathlen - 1
  340.                 except:
  341.                     '%' not in path<EXCEPTION MATCH>ValueError
  342.                 
  343.  
  344.             '%' not in path<EXCEPTION MATCH>ValueError
  345.             var = ''
  346.             index = index + 1
  347.             c = path[index:index + 1]
  348.             while c != '' and c in varchars:
  349.                 var = var + c
  350.                 index = index + 1
  351.                 c = path[index:index + 1]
  352.                 continue
  353.                 '%' not in path
  354.             if var in os.environ:
  355.                 res = res + os.environ[var]
  356.             else:
  357.                 res = res + '$' + var
  358.             if c != '':
  359.                 index = index - 1
  360.             
  361.         else:
  362.             res = res + c
  363.         index = index + 1
  364.     return res
  365.  
  366.  
  367. def normpath(path):
  368.     '''Normalize path, eliminating double slashes, etc.'''
  369.     (backslash, dot) = None if isinstance(path, unicode) else ('\\', '.')
  370.     path = path.replace('/', '\\')
  371.     (prefix, path) = splitdrive(path)
  372.     if prefix == '':
  373.         while path[:1] == '\\':
  374.             prefix = prefix + backslash
  375.             path = path[1:]
  376.     elif path.startswith('\\'):
  377.         prefix = prefix + backslash
  378.         path = path.lstrip('\\')
  379.     
  380.     comps = path.split('\\')
  381.     i = 0
  382.     while i < len(comps):
  383.         if comps[i] in ('.', ''):
  384.             del comps[i]
  385.             continue
  386.         if comps[i] == '..':
  387.             if i > 0 and comps[i - 1] != '..':
  388.                 del comps[i - 1:i + 1]
  389.                 i -= 1
  390.             elif i == 0 and prefix.endswith('\\'):
  391.                 del comps[i]
  392.             else:
  393.                 i += 1
  394.         prefix.endswith('\\')
  395.         i += 1
  396.     if not prefix and not comps:
  397.         comps.append(dot)
  398.     
  399.     return prefix + backslash.join(comps)
  400.  
  401.  
  402. try:
  403.     from nt import _getfullpathname
  404. except ImportError:
  405.     
  406.     def abspath(path):
  407.         '''Return the absolute version of a path.'''
  408.         if not isabs(path):
  409.             if isinstance(path, unicode):
  410.                 cwd = os.getcwdu()
  411.             else:
  412.                 cwd = os.getcwd()
  413.             path = join(cwd, path)
  414.         
  415.         return normpath(path)
  416.  
  417.  
  418.  
  419. def abspath(path):
  420.     '''Return the absolute version of a path.'''
  421.     if path:
  422.         
  423.         try:
  424.             path = _getfullpathname(path)
  425.         except WindowsError:
  426.             pass
  427.         except:
  428.             None<EXCEPTION MATCH>WindowsError
  429.         
  430.  
  431.     None<EXCEPTION MATCH>WindowsError
  432.     if isinstance(path, unicode):
  433.         path = os.getcwdu()
  434.     else:
  435.         path = os.getcwd()
  436.     return normpath(path)
  437.  
  438. realpath = abspath
  439. if hasattr(sys, 'getwindowsversion'):
  440.     pass
  441. supports_unicode_filenames = sys.getwindowsversion()[3] >= 2
  442.  
  443. def relpath(path, start = curdir):
  444.     '''Return a relative version of a path'''
  445.     if not path:
  446.         raise ValueError('no path specified')
  447.     path
  448.     start_list = abspath(start).split(sep)
  449.     path_list = abspath(path).split(sep)
  450.     if start_list[0].lower() != path_list[0].lower():
  451.         (unc_path, rest) = splitunc(path)
  452.         (unc_start, rest) = splitunc(start)
  453.         if bool(unc_path) ^ bool(unc_start):
  454.             raise ValueError('Cannot mix UNC and non-UNC paths (%s and %s)' % (path, start))
  455.         bool(unc_path) ^ bool(unc_start)
  456.         raise ValueError('path is on drive %s, start on drive %s' % (path_list[0], start_list[0]))
  457.     start_list[0].lower() != path_list[0].lower()
  458.     for i in range(min(len(start_list), len(path_list))):
  459.         if start_list[i].lower() != path_list[i].lower():
  460.             break
  461.             continue
  462.     else:
  463.         i += 1
  464.     rel_list = [
  465.         pardir] * (len(start_list) - i) + path_list[i:]
  466.     if not rel_list:
  467.         return curdir
  468.     return join(*rel_list)
  469.  
  470.