-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.py
57 lines (36 loc) · 1.24 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sublime
import functools
import itertools
import threading
def maybe(value):
if value is not None:
yield value
def group_by(xs, kf):
grouped = {}
for k, i in itertools.groupby(xs, kf):
grouped.setdefault(k, []).extend(i)
return grouped
class delayed(object):
def __init__(self, timeout):
self.timeout = timeout
def __call__(self, f):
def call_with_timeout(*args, **kwargs):
sublime.set_timeout(functools.partial(f, *args, **kwargs),
self.timeout)
return call_with_timeout
class SynchronizedCache(object):
def __init__(self):
self.__items = {}
self.__lock = threading.RLock()
def __call__(self, key, f):
with self.__lock:
if key not in self.__items:
self.__items[key] = f()
return self.__items[key]
class MetaOnePerWindow(type):
def __init__(cls, name, bases, dct):
super(MetaOnePerWindow, cls).__init__(name, bases, dct)
cls.instance_cache = SynchronizedCache()
def __call__(cls, window):
return cls.instance_cache(window.id(), lambda: type.__call__(cls, window))
OnePerWindow = MetaOnePerWindow('OnePerWindow', (object,), {})