home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / path.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  19.8 KB  |  715 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import generators
  5. import sys
  6. import warnings
  7. import os
  8. import fnmatch
  9. import glob
  10. import shutil
  11. import codecs
  12. import hashlib
  13. from os.path import join as pathjoin
  14. __version__ = '2.2'
  15. __all__ = [
  16.     'path']
  17. if os.name == 'nt':
  18.     
  19.     try:
  20.         import win32security
  21.     except ImportError:
  22.         win32security = None
  23.     except:
  24.         None<EXCEPTION MATCH>ImportError
  25.     
  26.  
  27. None<EXCEPTION MATCH>ImportError
  28.  
  29. try:
  30.     import pwd
  31. except ImportError:
  32.     pwd = None
  33.  
  34. _base = str
  35. _getcwd = os.getcwd
  36.  
  37. try:
  38.     if os.path.supports_unicode_filenames:
  39.         _base = unicode
  40.         _getcwd = os.getcwdu
  41.         _filesystem_encoding = sys.getfilesystemencoding()
  42. except AttributeError:
  43.     pass
  44.  
  45.  
  46. try:
  47.     (True, False)
  48. except NameError:
  49.     (True, False) = (1, 0)
  50.  
  51.  
  52. try:
  53.     basestring
  54. except NameError:
  55.     basestring = (str, unicode)
  56.  
  57. _textmode = 'r'
  58. if hasattr(file, 'newlines'):
  59.     _textmode = 'U'
  60.  
  61.  
  62. class TreeWalkWarning(Warning):
  63.     pass
  64.  
  65.  
  66. class path(_base):
  67.     
  68.     def __repr__(self):
  69.         return 'path(%s)' % _base.__repr__(self)
  70.  
  71.     
  72.     def __add__(self, more):
  73.         
  74.         try:
  75.             resultStr = _base.__add__(self, more)
  76.         except TypeError:
  77.             resultStr = NotImplemented
  78.  
  79.         if resultStr is NotImplemented:
  80.             return resultStr
  81.         return self.__class__(resultStr)
  82.  
  83.     
  84.     def __radd__(self, other):
  85.         if isinstance(other, basestring):
  86.             return self.__class__(other.__add__(self))
  87.         return NotImplemented
  88.  
  89.     
  90.     def __div__(self, rel):
  91.         return self.__class__(pathjoin(self, rel))
  92.  
  93.     __truediv__ = __div__
  94.     
  95.     def getcwd(cls):
  96.         return cls(_getcwd())
  97.  
  98.     getcwd = classmethod(getcwd)
  99.     isabs = os.path.isabs
  100.     
  101.     def url(self):
  102.         return self.__class__('file:///' + self.abspath().replace('\\', '/'))
  103.  
  104.     
  105.     def abspath(self):
  106.         return self.__class__(os.path.abspath(self))
  107.  
  108.     
  109.     def normcase(self):
  110.         return self.__class__(os.path.normcase(self))
  111.  
  112.     
  113.     def normpath(self):
  114.         return self.__class__(os.path.normpath(self))
  115.  
  116.     
  117.     def realpath(self):
  118.         return self.__class__(os.path.realpath(self))
  119.  
  120.     
  121.     def expanduser(self):
  122.         return self.__class__(os.path.expanduser(self))
  123.  
  124.     
  125.     def expandvars(self):
  126.         return self.__class__(os.path.expandvars(self))
  127.  
  128.     
  129.     def dirname(self):
  130.         return self.__class__(os.path.dirname(self))
  131.  
  132.     basename = os.path.basename
  133.     
  134.     def expand(self):
  135.         return self.expandvars().expanduser().normpath().normcase()
  136.  
  137.     
  138.     def _get_namebase(self):
  139.         (base, ext) = os.path.splitext(self.name)
  140.         return base
  141.  
  142.     
  143.     def _get_ext(self):
  144.         (f, ext) = os.path.splitext(_base(self))
  145.         return ext
  146.  
  147.     
  148.     def _get_drive(self):
  149.         (drive, r) = os.path.splitdrive(self)
  150.         return self.__class__(drive)
  151.  
  152.     parent = property(dirname, None, None, " This path's parent directory, as a new path object.\n\n        For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')\n        ")
  153.     name = property(basename, None, None, " The name of this file or directory without the full path.\n\n        For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'\n        ")
  154.     namebase = property(_get_namebase, None, None, " The same as path.name, but with one file extension stripped off.\n\n        For example, path('/home/guido/python.tar.gz').name     == 'python.tar.gz',\n        but          path('/home/guido/python.tar.gz').namebase == 'python.tar'\n        ")
  155.     ext = property(_get_ext, None, None, " The file extension, for example '.py'. ")
  156.     drive = property(_get_drive, None, None, " The drive specifier, for example 'C:'.\n        This is always empty on systems that don't use drive specifiers.\n        ")
  157.     
  158.     def splitpath(self):
  159.         (parent, child) = os.path.split(self)
  160.         return (self.__class__(parent), child)
  161.  
  162.     
  163.     def splitdrive(self):
  164.         (drive, rel) = os.path.splitdrive(self)
  165.         return (self.__class__(drive), rel)
  166.  
  167.     
  168.     def splitext(self):
  169.         (filename, ext) = os.path.splitext(self)
  170.         return (self.__class__(filename), ext)
  171.  
  172.     
  173.     def stripext(self):
  174.         return self.splitext()[0]
  175.  
  176.     if hasattr(os.path, 'splitunc'):
  177.         
  178.         def splitunc(self):
  179.             (unc, rest) = os.path.splitunc(self)
  180.             return (self.__class__(unc), rest)
  181.  
  182.         
  183.         def _get_uncshare(self):
  184.             (unc, r) = os.path.splitunc(self)
  185.             return self.__class__(unc)
  186.  
  187.         uncshare = property(_get_uncshare, None, None, ' The UNC mount point for this path.\n            This is empty for paths on local drives. ')
  188.     
  189.     
  190.     def joinpath(self, *args):
  191.         return self.__class__(pathjoin(self, *args))
  192.  
  193.     
  194.     def splitall(self):
  195.         parts = []
  196.         loc = self
  197.         while loc != os.curdir and loc != os.pardir:
  198.             prev = loc
  199.             (loc, child) = prev.splitpath()
  200.             if loc == prev:
  201.                 break
  202.             
  203.             parts.append(child)
  204.         parts.append(loc)
  205.         parts.reverse()
  206.         return parts
  207.  
  208.     
  209.     def relpath(self):
  210.         cwd = self.__class__(os.getcwd())
  211.         return cwd.relpathto(self)
  212.  
  213.     
  214.     def relpathto(self, dest):
  215.         origin = self.abspath()
  216.         dest = self.__class__(dest).abspath()
  217.         orig_list = origin.normcase().splitall()
  218.         dest_list = dest.splitall()
  219.         if orig_list[0] != os.path.normcase(dest_list[0]):
  220.             return dest
  221.         i = 0
  222.         for start_seg, dest_seg in zip(orig_list, dest_list):
  223.             if start_seg != os.path.normcase(dest_seg):
  224.                 break
  225.             
  226.             i += 1
  227.         
  228.         segments = [
  229.             os.pardir] * (len(orig_list) - i)
  230.         segments += dest_list[i:]
  231.         if len(segments) == 0:
  232.             relpath = os.curdir
  233.         else:
  234.             relpath = pathjoin(*segments)
  235.         return self.__class__(relpath)
  236.  
  237.     
  238.     def listdir(self, pattern = None):
  239.         
  240.         try:
  241.             names = os.listdir(self)
  242.         except Exception:
  243.             return []
  244.  
  245.         if pattern is not None:
  246.             names = fnmatch.filter(names, pattern)
  247.         
  248.         return [ self / child for child in names ]
  249.  
  250.     
  251.     def dirs(self, pattern = None):
  252.         return _[1]
  253.  
  254.     
  255.     def files(self, pattern = None):
  256.         return _[1]
  257.  
  258.     
  259.     def walk(self, pattern = None, errors = 'strict'):
  260.         if errors not in ('strict', 'warn', 'ignore'):
  261.             raise ValueError('invalid errors parameter')
  262.         errors not in ('strict', 'warn', 'ignore')
  263.         
  264.         try:
  265.             childList = self.listdir()
  266.         except Exception:
  267.             if errors == 'ignore':
  268.                 return None
  269.             if errors == 'warn':
  270.                 warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning)
  271.                 return None
  272.             raise 
  273.         except:
  274.             errors == 'warn'
  275.  
  276.         for child in childList:
  277.             if pattern is None or child.fnmatch(pattern):
  278.                 yield child
  279.                 errors == 'warn'
  280.             
  281.             
  282.             try:
  283.                 isdir = child.isdir()
  284.             except Exception:
  285.                 if errors == 'ignore':
  286.                     isdir = False
  287.                 elif errors == 'warn':
  288.                     warnings.warn("Unable to access '%s': %s" % (child, sys.exc_info()[1]), TreeWalkWarning)
  289.                     isdir = False
  290.                 else:
  291.                     raise 
  292.                 errors == 'ignore'
  293.  
  294.             if isdir:
  295.                 for item in child.walk(pattern, errors):
  296.                     yield item
  297.                 
  298.         
  299.  
  300.     
  301.     def walkdirs(self, pattern = None, errors = 'strict', top_down = True):
  302.         if errors not in ('strict', 'warn', 'ignore'):
  303.             raise ValueError('invalid errors parameter')
  304.         errors not in ('strict', 'warn', 'ignore')
  305.         
  306.         try:
  307.             dirs = self.dirs()
  308.         except Exception:
  309.             if errors == 'ignore':
  310.                 return None
  311.             if errors == 'warn':
  312.                 warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning)
  313.                 return None
  314.             raise 
  315.         except:
  316.             errors == 'warn'
  317.  
  318.         for child in dirs:
  319.             for subsubdir in child.walkdirs(pattern, errors):
  320.                 yield subsubdir
  321.                 None if top_down else errors == 'ignore'
  322.             
  323.             if not top_down:
  324.                 if pattern is None or child.fnmatch(pattern):
  325.                     yield child
  326.                 
  327.             child.fnmatch(pattern)
  328.         
  329.  
  330.     
  331.     def walkfiles(self, pattern = None, errors = 'strict'):
  332.         if errors not in ('strict', 'warn', 'ignore'):
  333.             raise ValueError('invalid errors parameter')
  334.         errors not in ('strict', 'warn', 'ignore')
  335.         
  336.         try:
  337.             childList = self.listdir()
  338.         except Exception:
  339.             if errors == 'ignore':
  340.                 return None
  341.             if errors == 'warn':
  342.                 warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning)
  343.                 return None
  344.             raise 
  345.         except:
  346.             errors == 'warn'
  347.  
  348.         for child in childList:
  349.             
  350.             try:
  351.                 isfile = child.isfile()
  352.                 if not isfile:
  353.                     pass
  354.                 isdir = child.isdir()
  355.             except:
  356.                 errors == 'warn'
  357.                 errors == 'ignore'
  358.                 if errors == 'ignore':
  359.                     continue
  360.                 elif errors == 'warn':
  361.                     warnings.warn("Unable to access '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning)
  362.                     continue
  363.                 else:
  364.                     raise 
  365.  
  366.             if isfile:
  367.                 if pattern is None or child.fnmatch(pattern):
  368.                     yield child
  369.                     errors == 'warn'
  370.                 
  371.             child.fnmatch(pattern)
  372.             if isdir:
  373.                 for f in child.walkfiles(pattern, errors):
  374.                     yield f
  375.                     errors == 'warn'
  376.                 
  377.             errors == 'ignore'
  378.         
  379.  
  380.     
  381.     def fnmatch(self, pattern):
  382.         return fnmatch.fnmatch(self.name, pattern)
  383.  
  384.     
  385.     def glob(self, pattern):
  386.         cls = self.__class__
  387.         return [ cls(s) for s in glob.glob(_base(self / pattern)) ]
  388.  
  389.     
  390.     def open(self, mode = 'r'):
  391.         return file(self, mode)
  392.  
  393.     
  394.     def openfolder(self):
  395.         if os.name == 'nt':
  396.             if self.isfile() or self.isdir():
  397.                 Popen = Popen
  398.                 import subprocess
  399.                 args = [
  400.                     'explorer',
  401.                     '/select,',
  402.                     '%s' % self.abspath().encode(sys.getfilesystemencoding())]
  403.                 Popen(args, shell = True)
  404.             else:
  405.                 os.startfile(self.parent)
  406.  
  407.     
  408.     def bytes(self):
  409.         f = self.open('rb')
  410.         
  411.         try:
  412.             return f.read()
  413.         finally:
  414.             f.close()
  415.  
  416.  
  417.     
  418.     def write_bytes(self, bytes, append = False):
  419.         if append:
  420.             mode = 'ab'
  421.         else:
  422.             mode = 'wb'
  423.         f = self.open(mode)
  424.         
  425.         try:
  426.             f.write(bytes)
  427.         finally:
  428.             f.close()
  429.  
  430.  
  431.     
  432.     def text(self, encoding = None, errors = 'strict'):
  433.         if encoding is None:
  434.             f = self.open(_textmode)
  435.             
  436.             try:
  437.                 return f.read()
  438.             finally:
  439.                 f.close()
  440.  
  441.         else:
  442.             f = codecs.open(self, 'r', encoding, errors)
  443.             
  444.             try:
  445.                 t = f.read()
  446.             finally:
  447.                 f.close()
  448.  
  449.             return t.replace(u'\r\n', u'\n').replace(u'\r┬à', u'\n').replace(u'\r', u'\n').replace(u'┬à', u'\n').replace(u'ΓÇ¿', u'\n')
  450.         return encoding is None
  451.  
  452.     
  453.     def write_text(self, text, encoding = None, errors = 'strict', linesep = os.linesep, append = False):
  454.         if isinstance(text, unicode):
  455.             if linesep is not None:
  456.                 text = text.replace(u'\r\n', u'\n').replace(u'\r┬à', u'\n').replace(u'\r', u'\n').replace(u'┬à', u'\n').replace(u'ΓÇ¿', u'\n')
  457.                 text = text.replace(u'\n', linesep)
  458.             
  459.             if encoding is None:
  460.                 encoding = sys.getdefaultencoding()
  461.             
  462.             bytes = text.encode(encoding, errors)
  463.         elif linesep is not None:
  464.             text = text.replace('\r\n', '\n').replace('\r', '\n')
  465.             bytes = text.replace('\n', linesep)
  466.         
  467.         self.write_bytes(bytes, append)
  468.  
  469.     
  470.     def lines(self, encoding = None, errors = 'strict', retain = True):
  471.         if encoding is None and retain:
  472.             f = self.open(_textmode)
  473.             
  474.             try:
  475.                 return f.readlines()
  476.             finally:
  477.                 f.close()
  478.  
  479.         else:
  480.             return self.text(encoding, errors).splitlines(retain)
  481.         return retain
  482.  
  483.     
  484.     def write_lines(self, lines, encoding = None, errors = 'strict', linesep = os.linesep, append = False):
  485.         if append:
  486.             mode = 'ab'
  487.         else:
  488.             mode = 'wb'
  489.         f = self.open(mode)
  490.         
  491.         try:
  492.             for line in lines:
  493.                 isUnicode = isinstance(line, unicode)
  494.                 if linesep is not None:
  495.                     if isUnicode:
  496.                         if line[-2:] in (u'\r\n', u'\r┬à'):
  497.                             line = line[:-2]
  498.                         elif line[-1:] in (u'\r', u'\n', u'┬à', u'ΓÇ¿'):
  499.                             line = line[:-1]
  500.                         
  501.                     elif line[-2:] == '\r\n':
  502.                         line = line[:-2]
  503.                     elif line[-1:] in ('\r', '\n'):
  504.                         line = line[:-1]
  505.                     
  506.                     line += linesep
  507.                 
  508.                 if isUnicode:
  509.                     if encoding is None:
  510.                         encoding = sys.getdefaultencoding()
  511.                     
  512.                     line = line.encode(encoding, errors)
  513.                 
  514.                 f.write(line)
  515.         finally:
  516.             f.close()
  517.  
  518.  
  519.     
  520.     def read_md5(self):
  521.         f = self.open('rb')
  522.         
  523.         try:
  524.             m = hashlib.md5()
  525.             while True:
  526.                 d = f.read(8192)
  527.                 if not d:
  528.                     break
  529.                 
  530.                 m.update(d)
  531.         finally:
  532.             f.close()
  533.  
  534.         return m.digest()
  535.  
  536.     
  537.     def exists(self):
  538.         import warnings
  539.         warnings.warn('path.exists is deprecated. Use pth.isfile() or pth.isdir()')
  540.         return os.path.exists(self)
  541.  
  542.     isdir = os.path.isdir
  543.     isfile = os.path.isfile
  544.     islink = os.path.islink
  545.     ismount = os.path.ismount
  546.     if hasattr(os.path, 'samefile'):
  547.         samefile = os.path.samefile
  548.     
  549.     getatime = os.path.getatime
  550.     atime = property(getatime, None, None, ' Last access time of the file. ')
  551.     getmtime = os.path.getmtime
  552.     mtime = property(getmtime, None, None, ' Last-modified time of the file. ')
  553.     if hasattr(os.path, 'getctime'):
  554.         getctime = os.path.getctime
  555.         ctime = property(getctime, None, None, ' Creation time of the file. ')
  556.     
  557.     getsize = os.path.getsize
  558.     size = property(getsize, None, None, ' Size of the file, in bytes. ')
  559.     if hasattr(os, 'access'):
  560.         
  561.         def access(self, mode):
  562.             return os.access(self, mode)
  563.  
  564.     
  565.     
  566.     def stat(self):
  567.         return os.stat(self)
  568.  
  569.     
  570.     def lstat(self):
  571.         return os.lstat(self)
  572.  
  573.     
  574.     def get_owner(self):
  575.         if os.name == 'nt':
  576.             if win32security is None:
  577.                 raise Exception('path.owner requires win32all to be installed')
  578.             win32security is None
  579.             desc = win32security.GetFileSecurity(self, win32security.OWNER_SECURITY_INFORMATION)
  580.             sid = desc.GetSecurityDescriptorOwner()
  581.             (account, domain, typecode) = win32security.LookupAccountSid(None, sid)
  582.             return domain + u'\\' + account
  583.         if pwd is None:
  584.             raise NotImplementedError('path.owner is not implemented on this platform.')
  585.         pwd is None
  586.         st = self.stat()
  587.         return pwd.getpwuid(st.st_uid).pw_name
  588.  
  589.     owner = property(get_owner, None, None, ' Name of the owner of this file or directory. ')
  590.     if hasattr(os, 'statvfs'):
  591.         
  592.         def statvfs(self):
  593.             return os.statvfs(self)
  594.  
  595.     
  596.     if hasattr(os, 'pathconf'):
  597.         
  598.         def pathconf(self, name):
  599.             return os.pathconf(self, name)
  600.  
  601.     
  602.     
  603.     def utime(self, times):
  604.         os.utime(self, times)
  605.  
  606.     
  607.     def chmod(self, mode):
  608.         os.chmod(self, mode)
  609.  
  610.     if hasattr(os, 'chown'):
  611.         
  612.         def chown(self, uid, gid):
  613.             os.chown(self, uid, gid)
  614.  
  615.     
  616.     
  617.     def rename(self, new):
  618.         os.rename(self, new)
  619.  
  620.     
  621.     def renames(self, new):
  622.         os.renames(self, new)
  623.  
  624.     
  625.     def mkdir(self, mode = 511):
  626.         os.mkdir(self, mode)
  627.  
  628.     
  629.     def makedirs(self, mode = 511):
  630.         os.makedirs(self, mode)
  631.  
  632.     
  633.     def rmdir(self):
  634.         os.rmdir(self)
  635.  
  636.     
  637.     def removedirs(self):
  638.         os.removedirs(self)
  639.  
  640.     
  641.     def touch(self):
  642.         fd = os.open(self, os.O_WRONLY | os.O_CREAT, 438)
  643.         os.close(fd)
  644.         os.utime(self, None)
  645.  
  646.     
  647.     def remove(self):
  648.         os.remove(self)
  649.  
  650.     
  651.     def unlink(self):
  652.         os.unlink(self)
  653.  
  654.     if hasattr(os, 'link'):
  655.         
  656.         def link(self, newpath):
  657.             os.link(self, newpath)
  658.  
  659.     
  660.     if hasattr(os, 'symlink'):
  661.         
  662.         def symlink(self, newlink):
  663.             os.symlink(self, newlink)
  664.  
  665.     
  666.     if hasattr(os, 'readlink'):
  667.         
  668.         def readlink(self):
  669.             return self.__class__(os.readlink(self))
  670.  
  671.         
  672.         def readlinkabs(self):
  673.             p = self.readlink()
  674.             if p.isabs():
  675.                 return p
  676.             return (self.parent / p).abspath()
  677.  
  678.     
  679.     copyfile = shutil.copyfile
  680.     copymode = shutil.copymode
  681.     copystat = shutil.copystat
  682.     copy = shutil.copy
  683.     copy2 = shutil.copy2
  684.     copytree = shutil.copytree
  685.     if hasattr(shutil, 'move'):
  686.         move = shutil.move
  687.     
  688.     rmtree = shutil.rmtree
  689.     if hasattr(os, 'chroot'):
  690.         
  691.         def chroot(self):
  692.             os.chroot(self)
  693.  
  694.     
  695.     if hasattr(os, 'startfile'):
  696.         
  697.         def startfile(self):
  698.             os.startfile(self)
  699.  
  700.     
  701.     if _base is unicode:
  702.         
  703.         def __str__(self):
  704.             return self.encode(_filesystem_encoding)
  705.  
  706.         
  707.         def __new__(self, s):
  708.             if isinstance(s, str):
  709.                 s = s.decode(_filesystem_encoding)
  710.             
  711.             return unicode.__new__(self, s)
  712.  
  713.     
  714.  
  715.