"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/ui/gtk/threaded_pysize_tree.py" (11 Mar 2007, 3110 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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.
    5 #
    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.
   10 #
   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.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   19 from threading import Thread
   20 from Queue import Queue
   21 import gobject
   22 
   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
   26 
   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.
   31 #
   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.
   36 #
   37 # As the thread must not touch the GUI, every gtk manipulation must be made
   38 # through a gobject.idle_add(lambda: ...).
   39 
   40 # Yeah, we are using threads
   41 gobject.threads_init()
   42 
   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
   45 # one.
   46 class AbortThisTree(Exception):
   47     pass
   48 
   49 class threaded_pysize_tree(object):
   50     def __init__(self):
   51         self.completion = observable()
   52         self.queue = Queue(0)
   53         size_observable.add_observer(self.size_observer)
   54         self.thread = Thread(target=self.run_thread)
   55         self.thread.setDaemon(True)
   56         self.thread.start()
   57 
   58     def schedule(self, *args, **kwargs):
   59         self.queue.put((args, kwargs))
   60 
   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():
   65             # The GUI wants another tree, lets stop this one
   66             raise AbortThisTree
   67 
   68     def run_thread(self):
   69         while True:
   70             params = self.queue.get()
   71             while not self.queue.empty():
   72                 # Get the last item
   73                 params = self.queue.get_nowait()
   74             args, kwargs = params
   75             gobject.idle_add(lambda: self.completion.fire_observers(None))
   76             try:
   77                 tree = pysize_tree(*args, **kwargs)
   78             except AbortThisTree:
   79                 continue
   80             gobject.idle_add(lambda: self.completion.fire_observers(tree))