home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1785 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  20.5 KB  |  671 lines

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