1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
19 from threading
import Thread
, currentThread
20 from Queue
import Queue
23 from pysize
.core
.observable
import observable
24 from pysize
.core
.pysize_fs_tree
import pysize_tree
25 from pysize
.core
.compute_size
import size_observable
27 # We don't want the building of a tree to block the GUI. A possible
28 # solution is to periodically call gtk.main_iteration_do() during the
29 # building. Unfortunately, this introduces reentrance issues when an
30 # event trigger the building of another tree.
32 # So we build all trees in a single separate thread. This additional
33 # thread never touches the GUI. So when the GUI wants an updated tree
34 # it calls threaded_pysize_tree.schedule(), and when the thread finishes
35 # the tree it wakes the observer that will update the GUI.
37 # As the thread must not touch the GUI, every gtk manipulation must be made
38 # through a gobject.idle_add(lambda: ...).
40 # Yeah, we are using threads
41 gobject
.threads_init()
43 # The only aim of this exception is to unwind the stack in order
44 # to abort the current building, and quickly procede to the next
46 class AbortThisTree(Exception):
49 class threaded_pysize_tree(object):
51 self
.completion
= observable()
53 size_observable
.add_observer(self
.size_observer
)
54 self
.thread
= Thread(target
=self
.run_thread
)
55 self
.thread
.setDaemon(True)
58 def schedule(self
, *args
, **kwargs
):
59 self
.queue
.put((args
, kwargs
))
61 # Called for every computed size(), that is, periodically during
62 # the building of a tree.
63 def size_observer(self
, *args
, **kwargs
):
64 if not self
.queue
.empty() and currentThread() == self
.thread
:
65 # The GUI wants another tree, lets stop this one
70 params
= self
.queue
.get()
71 while not self
.queue
.empty():
73 params
= self
.queue
.get_nowait()
75 gobject
.idle_add(lambda: self
.completion
.fire_observers(None))
77 tree
= pysize_tree(*args
, **kwargs
)
80 gobject
.idle_add(lambda: self
.completion
.fire_observers(tree
))