ui: Added support for fetching movie images
[maevies] / ui / maeviesui / util / asyncworker.py
1 # -*- coding: utf-8 -*-
2
3 ###########################################################################
4 #    SeriesFinale
5 #    Copyright (C) 2009 Joaquim Rocha <jrocha@igalia.com>
6
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 ###########################################################################
20
21 from threading import Thread
22 import Queue
23 import gobject
24 import logging
25 logging.basicConfig(level=logging.DEBUG)
26
27 class AsyncItem(object):
28
29     def __init__(self, target_method, target_method_args, finish_callback=None, finish_callback_args=()):
30         self.target_method = target_method
31         self.target_method_args = target_method_args
32         self.finish_callback = finish_callback
33         self.finish_callback_args = finish_callback_args
34         self.canceled = False
35
36     def run(self):
37         if self.canceled:
38             return
39         results = error = None
40         try:
41             results = self.target_method(*self.target_method_args)
42         except Exception, exception:
43             logging.debug(str(exception))
44             error = exception
45         if self.canceled or not self.finish_callback:
46             return
47         self.finish_callback_args += (results,)
48         self.finish_callback_args += (error,)
49         gobject.idle_add(self.finish_callback, *self.finish_callback_args)
50
51     def cancel(self):
52         self.canceled = True
53
54 class AsyncWorker(Thread):
55
56     def __init__(self):
57         Thread.__init__(self)
58         self.queue = Queue.Queue(0)
59         self.stopped = False
60         self.async_item = None
61         self.item_number = -1
62
63     def run(self):
64         while not self.stopped:
65             if self.queue.empty():
66                 self.stop()
67                 break
68             try:
69                 self.async_item = self.queue.get()
70                 self.item_number += 1
71                 self.async_item.run()
72                 self.queue.task_done()
73                 self.async_item = None
74             except Exception, exception:
75                 logging.debug(str(exception))
76                 self.stop()
77
78     def stop(self):
79         self.stopped = True
80         if self.async_item:
81             self.async_item.cancel()
82