home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- _count = 0
- import time
- import cStringIO
- from Queue import Queue, Empty
- from calibre import prints
- from calibre.constants import DEBUG
-
- class BaseJob(object):
- WAITING = 0
- RUNNING = 1
- FINISHED = 2
-
- def __init__(self, description, done = (lambda x: x)):
- global _count
- _count += 1
- self.id = _count
- self.description = description
- self.done = done
- self.done2 = None
- self.killed = False
- self.failed = False
- self.kill_on_start = False
- self.start_time = None
- self.result = None
- self.duration = None
- self.log_path = None
- self.notifications = Queue()
- self._run_state = self.WAITING
- self.percent = 0
- self._message = None
- self._status_text = _('Waiting...')
- self._done_called = False
-
-
- def update(self, consume_notifications = True):
- if self.duration is not None:
- self._run_state = self.FINISHED
- self.percent = 100
- if self.killed:
- self._status_text = _('Stopped')
- elif self.failed:
- pass
-
- self._status_text = _('Finished')
- if DEBUG:
-
- try:
- prints('Job:', self.id, self.description, 'finished', safe_encode = True)
- prints('\t'.join(self.details.splitlines(True)), safe_encode = True)
-
-
- if not self._done_called:
- self._done_called = True
-
- try:
- self.done(self)
- except:
- pass
-
-
- try:
- if callable(self.done2):
- self.done2(self)
-
-
- elif self.start_time is not None:
- self._run_state = self.RUNNING
- self._status_text = _('Working...')
-
- while consume_notifications:
-
- try:
- (self.percent, self._message) = self.notifications.get_nowait()
- self.percent *= 100
- continue
- except Empty:
- break
- continue
-
-
- None<EXCEPTION MATCH>Empty
-
-
- def status_text(self):
- if self._run_state == self.FINISHED or not (self._message):
- return self._status_text
- return self._message
-
- status_text = property(status_text)
-
- def run_state(self):
- return self._run_state
-
- run_state = property(run_state)
-
- def running_time(self):
- if self.duration is not None:
- return self.duration
- if self.start_time is not None:
- return time.time() - self.start_time
-
- running_time = property(running_time)
-
- def is_finished(self):
- return self._run_state == self.FINISHED
-
- is_finished = property(is_finished)
-
- def is_started(self):
- return self._run_state != self.WAITING
-
- is_started = property(is_started)
-
- def is_running(self):
- if self.is_started:
- pass
- return not (self.is_finished)
-
- is_running = property(is_running)
-
- def __cmp__(self, other):
- if self.is_finished == other.is_finished:
- if self.start_time is None:
- if other.start_time is None:
- return cmp(other.id, self.id)
- return 1
- self.start_time is None
- if other.start_time is None:
- return -1
- return cmp(other.start_time, self.start_time)
- self.is_finished == other.is_finished
- if self.is_finished:
- return 1
- return -1
- return 0
-
-
- def log_file(self):
- if self.log_path:
- return open(self.log_path, 'rb')
- return cStringIO.StringIO(_('No details available.'))
-
- log_file = property(log_file)
-
- def details(self):
- return self.log_file.read().decode('utf-8', 'replace')
-
- details = property(details)
-
-
- class ParallelJob(BaseJob):
-
- def __init__(self, name, description, done, args = [], kwargs = { }):
- self.name = name
- self.args = args
- self.kwargs = kwargs
- BaseJob.__init__(self, description, done)
-
-
-