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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. _count = 0
  9. import time
  10. import cStringIO
  11. from Queue import Queue, Empty
  12. from calibre import prints
  13. from calibre.constants import DEBUG
  14.  
  15. class BaseJob(object):
  16.     WAITING = 0
  17.     RUNNING = 1
  18.     FINISHED = 2
  19.     
  20.     def __init__(self, description, done = (lambda x: x)):
  21.         global _count
  22.         _count += 1
  23.         self.id = _count
  24.         self.description = description
  25.         self.done = done
  26.         self.done2 = None
  27.         self.killed = False
  28.         self.failed = False
  29.         self.kill_on_start = False
  30.         self.start_time = None
  31.         self.result = None
  32.         self.duration = None
  33.         self.log_path = None
  34.         self.notifications = Queue()
  35.         self._run_state = self.WAITING
  36.         self.percent = 0
  37.         self._message = None
  38.         self._status_text = _('Waiting...')
  39.         self._done_called = False
  40.  
  41.     
  42.     def update(self, consume_notifications = True):
  43.         if self.duration is not None:
  44.             self._run_state = self.FINISHED
  45.             self.percent = 100
  46.             if self.killed:
  47.                 self._status_text = _('Stopped')
  48.             elif self.failed:
  49.                 pass
  50.             
  51.             self._status_text = _('Finished')
  52.             if DEBUG:
  53.                 
  54.                 try:
  55.                     prints('Job:', self.id, self.description, 'finished', safe_encode = True)
  56.                     prints('\t'.join(self.details.splitlines(True)), safe_encode = True)
  57.  
  58.             
  59.             if not self._done_called:
  60.                 self._done_called = True
  61.                 
  62.                 try:
  63.                     self.done(self)
  64.                 except:
  65.                     pass
  66.  
  67.                 
  68.                 try:
  69.                     if callable(self.done2):
  70.                         self.done2(self)
  71.  
  72.             
  73.         elif self.start_time is not None:
  74.             self._run_state = self.RUNNING
  75.             self._status_text = _('Working...')
  76.         
  77.         while consume_notifications:
  78.             
  79.             try:
  80.                 (self.percent, self._message) = self.notifications.get_nowait()
  81.                 self.percent *= 100
  82.             continue
  83.             except Empty:
  84.                 break
  85.                 continue
  86.             
  87.  
  88.             None<EXCEPTION MATCH>Empty
  89.  
  90.     
  91.     def status_text(self):
  92.         if self._run_state == self.FINISHED or not (self._message):
  93.             return self._status_text
  94.         return self._message
  95.  
  96.     status_text = property(status_text)
  97.     
  98.     def run_state(self):
  99.         return self._run_state
  100.  
  101.     run_state = property(run_state)
  102.     
  103.     def running_time(self):
  104.         if self.duration is not None:
  105.             return self.duration
  106.         if self.start_time is not None:
  107.             return time.time() - self.start_time
  108.  
  109.     running_time = property(running_time)
  110.     
  111.     def is_finished(self):
  112.         return self._run_state == self.FINISHED
  113.  
  114.     is_finished = property(is_finished)
  115.     
  116.     def is_started(self):
  117.         return self._run_state != self.WAITING
  118.  
  119.     is_started = property(is_started)
  120.     
  121.     def is_running(self):
  122.         if self.is_started:
  123.             pass
  124.         return not (self.is_finished)
  125.  
  126.     is_running = property(is_running)
  127.     
  128.     def __cmp__(self, other):
  129.         if self.is_finished == other.is_finished:
  130.             if self.start_time is None:
  131.                 if other.start_time is None:
  132.                     return cmp(other.id, self.id)
  133.                 return 1
  134.             self.start_time is None
  135.             if other.start_time is None:
  136.                 return -1
  137.             return cmp(other.start_time, self.start_time)
  138.         self.is_finished == other.is_finished
  139.         if self.is_finished:
  140.             return 1
  141.         return -1
  142.         return 0
  143.  
  144.     
  145.     def log_file(self):
  146.         if self.log_path:
  147.             return open(self.log_path, 'rb')
  148.         return cStringIO.StringIO(_('No details available.'))
  149.  
  150.     log_file = property(log_file)
  151.     
  152.     def details(self):
  153.         return self.log_file.read().decode('utf-8', 'replace')
  154.  
  155.     details = property(details)
  156.  
  157.  
  158. class ParallelJob(BaseJob):
  159.     
  160.     def __init__(self, name, description, done, args = [], kwargs = { }):
  161.         self.name = name
  162.         self.args = args
  163.         self.kwargs = kwargs
  164.         BaseJob.__init__(self, description, done)
  165.  
  166.  
  167.