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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. from calibre.constants import iswindows, __appname__, win32api, win32event, winerror, fcntl
  8. import time
  9. import atexit
  10. import os
  11.  
  12. class LockError(Exception):
  13.     pass
  14.  
  15.  
  16. class WindowsExclFile(object):
  17.     
  18.     def __init__(self, path, timeout = 20):
  19.         self.name = path
  20.         import win32file as w
  21.         import pywintypes
  22.         while timeout > 0:
  23.             timeout -= 1
  24.             
  25.             try:
  26.                 self._handle = w.CreateFile(path, w.GENERIC_READ | w.GENERIC_WRITE, 0, None, w.OPEN_ALWAYS, w.FILE_ATTRIBUTE_NORMAL, None)
  27.             continue
  28.             except pywintypes.error:
  29.                 err = None
  30.                 if getattr(err, 'args', [
  31.                     -1])[0] in (32, 33):
  32.                     time.sleep(1)
  33.                     continue
  34.                 else:
  35.                     raise 
  36.                 getattr(err, 'args', [
  37.                     -1])[0] in (32, 33)
  38.             
  39.  
  40.             None<EXCEPTION MATCH>pywintypes.error
  41.  
  42.     
  43.     def seek(self, amt, frm = 0):
  44.         import win32file as w
  45.         if frm not in (0, 1, 2):
  46.             raise ValueError('Invalid from for seek: %s' % frm)
  47.         frm not in (0, 1, 2)
  48.         frm = {
  49.             0: w.FILE_BEGIN,
  50.             1: w.FILE_CURRENT,
  51.             2: w.FILE_END }[frm]
  52.         if frm is w.FILE_END:
  53.             amt = 0 - amt
  54.         
  55.         w.SetFilePointer(self._handle, amt, frm)
  56.  
  57.     
  58.     def tell(self):
  59.         import win32file as w
  60.         return w.SetFilePointer(self._handle, 0, w.FILE_CURRENT)
  61.  
  62.     
  63.     def flush(self):
  64.         import win32file as w
  65.         w.FlushFileBuffers(self._handle)
  66.  
  67.     
  68.     def close(self):
  69.         if self._handle is not None:
  70.             import win32file as w
  71.             self.flush()
  72.             w.CloseHandle(self._handle)
  73.             self._handle = None
  74.         
  75.  
  76.     
  77.     def read(self, bytes = -1):
  78.         import win32file as w
  79.         sz = w.GetFileSize(self._handle)
  80.         max = sz - self.tell()
  81.         if bytes < 0:
  82.             bytes = max
  83.         
  84.         bytes = min(max, bytes)
  85.         if bytes < 1:
  86.             return ''
  87.         (hr, ans) = w.ReadFile(self._handle, bytes, None)
  88.         if hr != 0:
  89.             raise IOError('Error reading file: %s' % hr)
  90.         hr != 0
  91.         return ans
  92.  
  93.     
  94.     def readlines(self, sizehint = -1):
  95.         return self.read().splitlines()
  96.  
  97.     
  98.     def write(self, bytes):
  99.         if isinstance(bytes, unicode):
  100.             bytes = bytes.encode('utf-8')
  101.         
  102.         import win32file as w
  103.         w.WriteFile(self._handle, bytes, None)
  104.  
  105.     
  106.     def truncate(self, size = None):
  107.         import win32file as w
  108.         pos = self.tell()
  109.         if size is None:
  110.             size = pos
  111.         
  112.         t = min(size, pos)
  113.         self.seek(t)
  114.         w.SetEndOfFile(self._handle)
  115.         self.seek(pos)
  116.  
  117.     
  118.     def isatty(self):
  119.         return False
  120.  
  121.     
  122.     def closed(self):
  123.         return self._handle is None
  124.  
  125.     closed = property(closed)
  126.  
  127.  
  128. class ExclusiveFile(object):
  129.     
  130.     def __init__(self, path, timeout = 15):
  131.         self.path = path
  132.         self.timeout = timeout
  133.  
  134.     
  135.     def __enter__(self):
  136.         self.file = None if iswindows else open(self.path, 'a+b')
  137.         self.file.seek(0)
  138.         timeout = self.timeout
  139.         if not iswindows:
  140.             while self.timeout < 0 or timeout >= 0:
  141.                 
  142.                 try:
  143.                     fcntl.lockf(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  144.                 continue
  145.                 except IOError:
  146.                     time.sleep(1)
  147.                     timeout -= 1
  148.                     continue
  149.                 
  150.  
  151.                 None<EXCEPTION MATCH>IOError
  152.             if timeout < 0 and self.timeout >= 0:
  153.                 self.file.close()
  154.                 raise LockError('Failed to lock')
  155.             self.timeout >= 0
  156.         
  157.         return self.file
  158.  
  159.     
  160.     def __exit__(self, type, value, traceback):
  161.         self.file.close()
  162.  
  163.  
  164.  
  165. def _clean_lock_file(file):
  166.     
  167.     try:
  168.         file.close()
  169.     except:
  170.         pass
  171.  
  172.     
  173.     try:
  174.         os.remove(file.name)
  175.     except:
  176.         pass
  177.  
  178.  
  179.  
  180. def singleinstance(name):
  181.     if iswindows:
  182.         mutexname = 'mutexforsingleinstanceof' + __appname__ + name
  183.         mutex = win32event.CreateMutex(None, False, mutexname)
  184.         if mutex:
  185.             atexit.register(win32api.CloseHandle, mutex)
  186.         
  187.         return not (win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS)
  188.     path = os.path.expanduser('~/.' + __appname__ + '_' + name + '.lock')
  189.     
  190.     try:
  191.         f = open(path, 'w')
  192.         fcntl.lockf(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  193.         atexit.register(_clean_lock_file, f)
  194.         return True
  195.     except IOError:
  196.         iswindows
  197.         iswindows
  198.         return False
  199.  
  200.     return False
  201.  
  202.