~ubuntu-branches/ubuntu/karmic/quodlibet/karmic

« back to all changes in this revision

Viewing changes to quodlibet/util/copool.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-01-30 23:55:34 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20090130235534-l4e72ulw0vqfo17w
Tags: 2.0-1ubuntu1
* Merge from Debian experimental (LP: #276856), remaining Ubuntu changes:
  + debian/patches/40-use-music-profile.patch:
    - Use the "Music and Movies" pipeline per default.
* Refresh the above patch for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2006 Joe Wreschnig, Alexandre Passos
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License version 2 as
 
5
# published by the Free Software Foundation
 
6
#
 
7
# $Id: copool.py 4330 2008-09-14 03:19:26Z piman $
 
8
 
 
9
"""Manage a pool of routines using Python iterators."""
 
10
 
 
11
import gobject
 
12
 
 
13
__routines = {}
 
14
__paused = {}
 
15
 
 
16
def __wrap(func, funcid, args, kwargs):
 
17
    for value in func(*args, **kwargs):
 
18
        yield True
 
19
    remove(funcid)
 
20
    yield False
 
21
 
 
22
def add(func, *args, **kwargs):
 
23
    """Register a routine to run in GObject main loop.
 
24
 
 
25
    func should be a function that returns a Python iterator (e.g.
 
26
    generator) that provides values until it should stop being called.
 
27
 
 
28
    Optional Keyword Arguments:
 
29
    priority -- priority to run at (default PRIORITY_LOW)
 
30
    funcid -- mutex/removal identifier for this function
 
31
 
 
32
    Only one function with the same funcid can be running at once.
 
33
    Starting a new function with the same ID will stop the old one. If
 
34
    no funcid is given, the function itself is used. The funcid must
 
35
    be usable as a hash key.
 
36
    """
 
37
    funcid = kwargs.pop("funcid", func)
 
38
    if funcid in __routines or funcid in __paused:
 
39
        remove(funcid)
 
40
    priority = kwargs.pop("priority", gobject.PRIORITY_LOW)
 
41
    next = __wrap(func, funcid, args, kwargs).next
 
42
    idle_id = gobject.idle_add(next, priority=priority)
 
43
    __routines[funcid] = (idle_id, next, priority)
 
44
    print_d("Added copool function %r with id %r" % (func, funcid))
 
45
 
 
46
def remove(funcid):
 
47
    """Stop a registered routine."""
 
48
    if funcid in __routines:
 
49
        gobject.source_remove(__routines[funcid][0])
 
50
        del(__routines[funcid])
 
51
    if funcid in __paused:
 
52
        del(__paused[funcid])
 
53
    print_d("Removed copool function id %r" % funcid)
 
54
 
 
55
def remove_all():
 
56
    """Stop all running routines."""
 
57
    for funcid in __routines.keys():
 
58
        remove(funcid)
 
59
 
 
60
def pause(funcid):
 
61
    """Temporarily pause a registered routine."""
 
62
    func = __routines[funcid]
 
63
    remove(funcid)
 
64
    __paused[funcid] = func
 
65
 
 
66
def resume(funcid):
 
67
    """Resume a paused routine."""
 
68
    if funcid in __paused:
 
69
        old_idle_id, func, priority = __paused[funcid]
 
70
        del(__paused[funcid])
 
71
        idle_id = gobject.idle_add(func, priority=priority)
 
72
        __routines[funcid] = (idle_id, func, priority)
 
73
 
 
74
def step(funcid):
 
75
    """Force this function to iterate once."""
 
76
    if funcid in __routines:
 
77
        __routines[funcid][1]()
 
78
    elif funcid in __paused:
 
79
        __paused[funcid]()
 
80
    else:
 
81
        raise ValueError("no pooled routine %r" % funcid)
 
82