| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
from PyQt5 import QtCore |
| 21 |
import time |
| 22 |
from collections import defaultdict |
| 23 |
from functools import wraps |
| 24 |
|
| 25 |
"""Decorators for debugging and maybe main work mode.""" |
| 26 |
|
| 27 |
def print_in_out(unbound): |
| 28 |
"""Decorator to print input arguments and return value of the function.""" |
| 29 |
def _run(*args, **kwargs): |
| 30 |
from breezy.trace import mutter |
| 31 |
a = ','.join(repr(i) for i in args) |
| 32 |
b = ','.join('%s=%r' % (i,j) for (i,j) in kwargs.items()) |
| 33 |
if a and b: |
| 34 |
a = a + ',' + b |
| 35 |
else: |
| 36 |
a = a or b |
| 37 |
func_name = unbound.__name__ |
| 38 |
mutter('Called %s(%s)' % (func_name, a)) |
| 39 |
result = unbound(*args, **kwargs) |
| 40 |
mutter('%s returned %r' % (func_name, result)) |
| 41 |
return result |
| 42 |
return _run |
| 43 |
|
| 44 |
class LazyCall(object): |
| 45 |
|
| 46 |
def __init__(self, millisec, func, callback=None): |
| 47 |
self.func = func |
| 48 |
self.millisec = millisec |
| 49 |
self.callback = callback |
| 50 |
self._last_scheduled_at = 0 |
| 51 |
|
| 52 |
def call(self, *args, **kargs): |
| 53 |
self._func = lambda:self.func(*args, **kargs) |
| 54 |
if self._last_scheduled_at == 0: |
| 55 |
QtCore.QTimer.singleShot(self.millisec, self._exec) |
| 56 |
self._last_scheduled_at = time.time() |
| 57 |
|
| 58 |
def _exec(self): |
| 59 |
elapsed = (time.time() - self._last_scheduled_at) * 1000 |
| 60 |
if elapsed < self.millisec: |
| 61 |
|
| 62 |
QtCore.QTimer.singleShot(int(self.millisec - elapsed), self._exec) |
| 63 |
return |
| 64 |
|
| 65 |
last_executed_at = self._last_scheduled_at |
| 66 |
ret = self._func() |
| 67 |
has_next = last_executed_at != self._last_scheduled_at |
| 68 |
if self.callback: |
| 69 |
self.callback(ret, has_next) |
| 70 |
|
| 71 |
if has_next: |
| 72 |
|
| 73 |
QtCore.QTimer.singleShot(self.millisec, self._exec) |
| 74 |
return |
| 75 |
|
| 76 |
self._last_scheduled_at = 0 |
| 77 |
|
| 78 |
def lazy_call(millisec, per_instance=False): |
| 79 |
""" |
| 80 |
Decorator to delay function call. |
| 81 |
Specified function will called after waiting `millisec`, |
| 82 |
if there are multiple call while waiting, only last one will be done. |
| 83 |
""" |
| 84 |
def _lazy_call(function): |
| 85 |
if per_instance: |
| 86 |
_instances = defaultdict(lambda:LazyCall(millisec, function)) |
| 87 |
def __lazy_call(*args, **kwargs): |
| 88 |
key = hash(args[0]) |
| 89 |
caller = _instances[key] |
| 90 |
if not caller.callback: |
| 91 |
def cleanup(ret, has_next): |
| 92 |
if not has_next: |
| 93 |
del _instances[key] |
| 94 |
caller.callback = cleanup |
| 95 |
caller.call(*args, **kwargs) |
| 96 |
else: |
| 97 |
caller = LazyCall(millisec, function) |
| 98 |
def __lazy_call(*args, **kwargs): |
| 99 |
caller.call(*args, **kwargs) |
| 100 |
return __lazy_call |
| 101 |
return _lazy_call |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|