-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcaches.py
151 lines (113 loc) · 3.32 KB
/
caches.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sublime
import json
import codecs
import os
is_sublime_text_3 = int(sublime.version()) >= 3000
if is_sublime_text_3:
from .settings import Settings
else:
from settings import Settings
class ProcessCache():
_procs = []
last_task_name = None
@classmethod
def get_from_storage(cls):
return cls.storage().read() or []
@classmethod
def get(cls):
return cls._procs[:]
@classmethod
def refresh(cls):
def remove_dead(process):
if not process.is_alive():
cls.remove(process)
cls.each(remove_dead)
@classmethod
def add(cls, process):
cls.last_task_name = process.get_task_name()
if process not in cls._procs:
cls._procs.append(process)
process = process.to_json()
cls.storage().update(lambda procs: procs + [process] if process not in procs else procs)
@classmethod
def remove(cls, process):
if process in cls._procs:
cls._procs.remove(process)
cls.storage().update(lambda procs: [proc for proc in procs if proc['pid'] != process.pid])
@classmethod
def kill_all(cls):
cls.each(lambda process: process.kill())
cls.clear()
@classmethod
def each(cls, fn):
for process in cls.get():
fn(process)
@classmethod
def empty(cls):
return len(cls._procs) == 0
@classmethod
def clear(cls):
del cls._procs[:]
cls.storage().write([])
@classmethod
def storage(cls):
if Settings.get_from_shared_data("track_processes", True):
return CacheFile(Settings.package_path())
else:
return Cache()
class Cache():
def exists(self):
pass
def remove(self):
pass
def open(self, mode="r"):
pass
def read(self):
pass
def write(self, data):
pass
def update(self, fn):
pass
class CacheFile(Cache):
def __init__(self, working_dir):
self.working_dir = working_dir
self.cache_path = os.path.join(self.working_dir, Settings.CACHE_FILE_NAME)
def exists(self):
return os.path.exists(self.cache_path)
def remove(self):
return os.remove(self.cache_path)
def open(self, mode="r"):
return codecs.open(self.cache_path, mode, "utf-8", errors='replace')
def read(self):
data = None
cache_file = self.open()
try:
data = json.load(cache_file)
except ValueError:
data = []
finally:
cache_file.close()
return data
def write(self, data):
cache_file = self.open("w")
try:
json_data = json.dumps(data, ensure_ascii=False)
if not json_data:
json_data = '[]'
cache_file.write(json_data)
finally:
cache_file.close()
def update(self, fn):
cache_file = codecs.open(self.cache_path, "r+", "utf-8", errors='replace')
current_data = None
try:
current_data = json.load(cache_file)
except ValueError:
current_data = []
try:
cache_file.seek(0)
new_data = fn(current_data)
cache_file.write(json.dumps(new_data))
cache_file.truncate()
finally:
cache_file.close()