~lottanzb/lottanzb/xdg-location

« back to all changes in this revision

Viewing changes to lottanzb/util.py

  • Committer: Severin Heiniger
  • Date: 2009-02-03 22:04:47 UTC
  • mfrom: (572.1.49 plugins)
  • Revision ID: severinheiniger@gmail.com-20090203220447-ennr7ecy5okyf4nd
Support for plugins added.

The Newzbin and categorization features have been turned into plugins. More plugins to follow.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# You should have received a copy of the GNU General Public License
13
13
# along with this program; if not, write to the Free Software
14
14
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
15
 
 
 
15
 
16
16
import gobject
17
17
import threading
18
18
import types
34
34
from os import unlink
35
35
from os.path import isfile
36
36
from distutils.spawn import find_executable
37
 
 
38
 
from gobject import \
39
 
    GObject as OriginalGObject, \
40
 
    property as gproperty, \
41
 
    list_properties, \
42
 
    GObjectMeta
43
 
 
44
37
from socket import gethostname
45
38
 
 
39
from gobject import GObject as OriginalGObject
 
40
from gobject import property as gproperty
 
41
from gobject import list_properties, GObjectMeta
 
42
 
46
43
_ = lambda message: gettext.dgettext("lottanzb", message)
47
44
 
48
45
# Initializes the use of Python threading in the gobject module
109
106
            else:
110
107
                value = str(value)
111
108
        
112
 
        OriginalGObject.set_property(self, key, value)
113
 
        
114
 
        # Notify registered proxies that the model has changed.
115
 
        if self._v_autonotify:
116
 
            self.notify_proxies(key)
 
109
        old_value = self.get_property(key)
 
110
        
 
111
        if old_value != value:
 
112
            OriginalGObject.set_property(self, key, value)
 
113
        
 
114
            # Notify registered proxies that the model has changed.
 
115
            if self._v_autonotify:
 
116
                self.notify_proxies(key)
117
117
    
118
118
    def keys(self):
119
119
        return [prop.name.replace("-", "_") for prop in list_properties(self)]
155
155
            except TypeError:
156
156
                # Python won't be able to copy the proxy objects in _v_proxies
157
157
                # and _v_blockedproxies.
158
 
                if not key.startswith("_v_"):
 
158
                if not key.startswith("_"):
159
159
                    raise
160
160
        
161
161
        for key in self.keys():
346
346
def setup_kiwi_conversion():
347
347
    libgladeloader.LibgladeWidgetTree = KiwifiedLibgladeWidgetTree
348
348
 
 
349
class _TimerBase(GObject):
 
350
    """
 
351
    Helper class for Timer.
 
352
    """
 
353
    
 
354
    stopped = gproperty(type=bool, default=True)
 
355
    
 
356
    gsignal("tick")
 
357
    
 
358
    def __init__(self, timeout):
 
359
        GObject.__init__(self)
 
360
        
 
361
        self.timeout = timeout
 
362
    
 
363
    def stop(self):
 
364
        """
 
365
        Stop the emission of the 'tick' signal.
 
366
        """
 
367
        
 
368
        if not self.stopped:
 
369
            self.stopped = True
 
370
    
 
371
    def start(self):
 
372
        """
 
373
        Start the regulary emission of the 'tick' signal.
 
374
        
 
375
        The Times class ensures thanks to the _TimerSequence helper class,
 
376
        that it will exactly take the specific amount of time after this
 
377
        method is invoked until the 'tick' signal is emitted.
 
378
        """
 
379
        
 
380
        if self.stopped:
 
381
            self.stopped = False
 
382
 
 
383
class _TimerSequence(_TimerBase):
 
384
    """
 
385
    Helper class for Timer.
 
386
    """
 
387
    
 
388
    def start(self):
 
389
        """
 
390
        This is where the gobject.timeout_add is actually called.
 
391
        """
 
392
        
 
393
        _TimerBase.start(self)
 
394
        
 
395
        gobject.timeout_add(self.timeout, self.on_gobject_timeout)
 
396
    
 
397
    def on_gobject_timeout(self):
 
398
        if self.stopped:
 
399
            return False
 
400
        
 
401
        self.emit("tick")
 
402
        
 
403
        return True
 
404
 
 
405
class Timer(_TimerBase):
 
406
    """
 
407
    Wrapper for gobject.timeout_add, which is more object-oriented and
 
408
    which provides the two convenient methods 'start' and 'stop'.
 
409
    
 
410
    The timeout is the number of milliseconds between two emissions of the
 
411
    'tick' signal. Optionally, it's possible to directly specify a handler
 
412
    like using gobject.timeout_add.
 
413
    """
 
414
    
 
415
    def __init__(self, timeout, handler=None, *args):
 
416
        self._sequences = []
 
417
        
 
418
        _TimerBase.__init__(self, timeout)
 
419
        
 
420
        if callable(handler):
 
421
            self.connect("tick", handler, *args)
 
422
        
 
423
        self.connect("notify::stopped", self.on_state_changed)
 
424
    
 
425
    def on_state_changed(self, *args):        
 
426
        for sequence in self._sequences:
 
427
            sequence.stop()
 
428
        
 
429
        if not self.stopped:
 
430
            sequence = _TimerSequence(self.timeout)
 
431
            sequence.connect("tick", self.on_sequence_tick)
 
432
            sequence.start()
 
433
            
 
434
            self._sequences.append(sequence)
 
435
    
 
436
    def on_sequence_tick(self, *args):
 
437
        self.emit("tick")
 
438
 
349
439
# From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498171
350
440
class FileLock:
351
441
    """