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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import shutil
  9. import time
  10. from calibre.devices.errors import PathError
  11.  
  12. class File(object):
  13.     
  14.     def __init__(self, path):
  15.         stats = os.stat(path)
  16.         self.is_dir = os.path.isdir(path)
  17.         self.is_readonly = not os.access(path, os.W_OK)
  18.         self.ctime = stats.st_ctime
  19.         self.wtime = stats.st_mtime
  20.         self.size = stats.st_size
  21.         if path.endswith(os.sep):
  22.             path = path[:-1]
  23.         
  24.         self.path = path
  25.         self.name = os.path.basename(path)
  26.  
  27.  
  28.  
  29. def check_transfer(infile, dest):
  30.     infile.seek(0)
  31.     dest.seek(0)
  32.     return infile.read() == dest.read()
  33.  
  34.  
  35. class CLI(object):
  36.     
  37.     def get_file(self, path, outfile, end_session = True):
  38.         path = self.munge_path(path)
  39.         
  40.         try:
  41.             src = _[1]
  42.             shutil.copyfileobj(src, outfile)
  43.         finally:
  44.             pass
  45.  
  46.  
  47.     
  48.     def put_file(self, infile, path, replace_file = False, end_session = True):
  49.         path = self.munge_path(path)
  50.         close = False
  51.         if not hasattr(infile, 'read'):
  52.             infile = open(infile, 'rb')
  53.             close = True
  54.         
  55.         infile.seek(0)
  56.         if os.path.isdir(path):
  57.             path = os.path.join(path, infile.name)
  58.         
  59.         if not replace_file and os.path.exists(path):
  60.             raise PathError('File already exists: ' + path)
  61.         os.path.exists(path)
  62.         d = os.path.dirname(path)
  63.         if not os.path.exists(d):
  64.             os.makedirs(d)
  65.         
  66.         
  67.         try:
  68.             dest = _[1]
  69.             
  70.             try:
  71.                 shutil.copyfileobj(infile, dest)
  72.             except IOError:
  73.                 open(path, 'w+b').__exit__
  74.                 open(path, 'w+b').__exit__
  75.                 open(path, 'w+b')
  76.                 print 'WARNING: First attempt to send file to device failed'
  77.                 time.sleep(0.2)
  78.                 infile.seek(0)
  79.                 dest.seek(0)
  80.                 dest.truncate()
  81.                 shutil.copyfileobj(infile, dest)
  82.             except:
  83.                 open(path, 'w+b').__exit__
  84.  
  85.         finally:
  86.             pass
  87.  
  88.  
  89.     
  90.     def munge_path(self, path):
  91.         if path.startswith('/'):
  92.             if not path.startswith(self._main_prefix):
  93.                 if (self._card_a_prefix or path.startswith(self._card_a_prefix)) and self._card_b_prefix:
  94.                     pass
  95.             if not path.startswith(self._card_b_prefix):
  96.                 path = self._main_prefix + path[1:]
  97.             elif path.startswith('carda:'):
  98.                 path = path.replace('carda:', self._card_a_prefix[:-1])
  99.             elif path.startswith('cardb:'):
  100.                 path = path.replace('cardb:', self._card_b_prefix[:-1])
  101.             
  102.         return path
  103.  
  104.     
  105.     def list(self, path, recurse = False, end_session = True, munge = True):
  106.         if munge:
  107.             path = self.munge_path(path)
  108.         
  109.         if os.path.isfile(path):
  110.             return [
  111.                 (os.path.dirname(path), [
  112.                     File(path)])]
  113.         entries = [ File(os.path.join(path, f)) for f in os.listdir(path) ]
  114.         dirs = [
  115.             (path, entries)]
  116.         for _file in entries:
  117.             if recurse and _file.is_dir:
  118.                 dirs[len(dirs):] = self.list(_file.path, recurse = True, munge = False)
  119.                 continue
  120.             []
  121.         
  122.         return dirs
  123.  
  124.     
  125.     def mkdir(self, path, end_session = True):
  126.         if self.SUPPORTS_SUB_DIRS:
  127.             path = self.munge_path(path)
  128.             os.mkdir(path)
  129.         
  130.  
  131.     
  132.     def rm(self, path, end_session = True):
  133.         path = self.munge_path(path)
  134.         self.delete_books([
  135.             path])
  136.  
  137.     
  138.     def touch(self, path, end_session = True):
  139.         path = self.munge_path(path)
  140.         if not os.path.exists(path):
  141.             open(path, 'w').close()
  142.         
  143.         if not os.path.isdir(path):
  144.             os.utime(path, None)
  145.         
  146.  
  147.  
  148.