home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1463 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.9 KB  |  168 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.         self.core_usage = 1
  41.  
  42.     
  43.     def update(self, consume_notifications = True):
  44.         if self.duration is not None:
  45.             self._run_state = self.FINISHED
  46.             self.percent = 100
  47.             if self.killed:
  48.                 self._status_text = _('Stopped')
  49.             elif self.failed:
  50.                 pass
  51.             
  52.             self._status_text = _('Finished')
  53.             if DEBUG:
  54.                 
  55.                 try:
  56.                     prints('Job:', self.id, self.description, 'finished', safe_encode = True)
  57.                     prints('\t'.join(self.details.splitlines(True)), safe_encode = True)
  58.  
  59.             
  60.             if not self._done_called:
  61.                 self._done_called = True
  62.                 
  63.                 try:
  64.                     self.done(self)
  65.                 except:
  66.                     pass
  67.  
  68.                 
  69.                 try:
  70.                     if callable(self.done2):
  71.                         self.done2(self)
  72.  
  73.             
  74.         elif self.start_time is not None:
  75.             self._run_state = self.RUNNING
  76.             self._status_text = _('Working...')
  77.         
  78.         while consume_notifications:
  79.             
  80.             try:
  81.                 (self.percent, self._message) = self.notifications.get_nowait()
  82.                 self.percent *= 100
  83.             continue
  84.             except Empty:
  85.                 break
  86.                 continue
  87.             
  88.  
  89.             None<EXCEPTION MATCH>Empty
  90.  
  91.     
  92.     def status_text(self):
  93.         if self._run_state == self.FINISHED or not (self._message):
  94.             return self._status_text
  95.         return self._message
  96.  
  97.     status_text = property(status_text)
  98.     
  99.     def run_state(self):
  100.         return self._run_state
  101.  
  102.     run_state = property(run_state)
  103.     
  104.     def running_time(self):
  105.         if self.duration is not None:
  106.             return self.duration
  107.         if self.start_time is not None:
  108.             return time.time() - self.start_time
  109.  
  110.     running_time = property(running_time)
  111.     
  112.     def is_finished(self):
  113.         return self._run_state == self.FINISHED
  114.  
  115.     is_finished = property(is_finished)
  116.     
  117.     def is_started(self):
  118.         return self._run_state != self.WAITING
  119.  
  120.     is_started = property(is_started)
  121.     
  122.     def is_running(self):
  123.         if self.is_started:
  124.             pass
  125.         return not (self.is_finished)
  126.  
  127.     is_running = property(is_running)
  128.     
  129.     def __cmp__(self, other):
  130.         if self.is_finished == other.is_finished:
  131.             if self.start_time is None:
  132.                 if other.start_time is None:
  133.                     return cmp(other.id, self.id)
  134.                 return 1
  135.             self.start_time is None
  136.             if other.start_time is None:
  137.                 return -1
  138.             return cmp(other.start_time, self.start_time)
  139.         self.is_finished == other.is_finished
  140.         if self.is_finished:
  141.             return 1
  142.         return -1
  143.         return 0
  144.  
  145.     
  146.     def log_file(self):
  147.         if self.log_path:
  148.             return open(self.log_path, 'rb')
  149.         return cStringIO.StringIO(_('No details available.'))
  150.  
  151.     log_file = property(log_file)
  152.     
  153.     def details(self):
  154.         return self.log_file.read().decode('utf-8', 'replace')
  155.  
  156.     details = property(details)
  157.  
  158.  
  159. class ParallelJob(BaseJob):
  160.     
  161.     def __init__(self, name, description, done, args = [], kwargs = { }):
  162.         self.name = name
  163.         self.args = args
  164.         self.kwargs = kwargs
  165.         BaseJob.__init__(self, description, done)
  166.  
  167.  
  168.