-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathabc.py
More file actions
294 lines (242 loc) · 7.97 KB
/
abc.py
File metadata and controls
294 lines (242 loc) · 7.97 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import abc
import anyio.abc
import io
import tempfile
from typing import BinaryIO
from core import utils
from core.abc import BMCLAPIFile, ResponseFile, ResponseFileNotFound, ResponseFileMemory, ResponseFileLocal, ResponseFileRemote
from ..logger import logger
from tianxiu2b2t import units
from tianxiu2b2t.anyio.concurrency import gather
MEASURE_SIZES = (10, 20, 30, 40, 50, 100, 200)
class FileInfo:
def __init__(
self,
name: str,
size: int,
path: str
):
self.name = name
self.size = size
self.path = CPath(path)
def __str__(self) -> str:
return f"{self.name} ({self.size} bytes)"
def __repr__(self) -> str:
return f"FileInfo(name={self.name}, size={self.size}, path={self.path})"
def __hash__(self) -> int:
return hash(self.name)
def __eq__(self, __o: object) -> bool:
if isinstance(__o, FileInfo):
return self.name == __o.name and self.size == __o.size and self.path == __o.path
elif isinstance(__o, BMCLAPIFile):
return self.name == __o.hash and self.size == __o.size
return False
class Storage(metaclass=abc.ABCMeta):
type: str = "_inerface"
def __init__(
self,
name: str,
path: str,
weight: int,
**kwargs
):
self._name = name
self._path = CPath(path)
self._task_group = None
self.readonly = False
self.online = False
self.weight = weight
self.current_weight = 0
self._kwargs = kwargs
@property
def cache_size(self):
return units.parse_number_units(self._kwargs.get("cache_size", "inf"))
@property
def cache_ttl(self):
return units.parse_time_units(self._kwargs.get("cache_ttl", "10m"))
@property
def download_dir(self):
return bool(self._kwargs.get("add_download_dir", True))
@abc.abstractmethod
async def setup(
self,
task_group: anyio.abc.TaskGroup
):
self._task_group = task_group
task_group.start_soon(self.check_measures)
async def check_measures(self):
while 1:
try:
missings = []
for size, res in zip(
MEASURE_SIZES,
await gather(
*(self._check_measure(size) for size in MEASURE_SIZES)
)
):
if res:
continue
missings.append(size)
if missings:
logger.info(f"Storage {self.name} missing measures: {missings}")
await gather(
*(self.write_measure(size) for size in missings)
)
except:
logger.tdebug_traceback("storage.check_measures")
await anyio.sleep(300)
async def _check_measure(self, size: int) -> bool:
try:
return await self.check_measure(size)
except:
logger.ttraceback("storage.check_measure", size=size, name=self.name, type=self.type)
return False
@abc.abstractmethod
async def check_measure(
self,
size: int
) -> bool:
raise NotImplementedError
@abc.abstractmethod
async def list_files(
self,
path: str
) -> list[FileInfo]:
raise NotImplementedError
async def list_download_files(
self,
muitlpbar: utils.MultiTQDM
):
res: list[FileInfo] = []
with muitlpbar.sub(
256,
description=f"Listing files in {self.name}({self.type})"
) as pbar:
async def works(root_ids: list[int]):
for root_id in root_ids:
path = f"{root_id:02x}"
if self.download_dir:
path = f"download/{path}"
res.extend(await self.list_files(path))
pbar.update(1)
async with anyio.create_task_group() as task_group:
work = utils.split_workload(list(RANGE), 10)
for w in work:
task_group.start_soon(works, w)
return res
@abc.abstractmethod
async def upload(
self,
path: str,
data: BinaryIO,
size: int
):
raise NotImplementedError
async def upload_download_file(self, path: str, data: BinaryIO, size: int):
if self.download_dir:
path = f"download/{path}"
await self.upload(path, data, size)
async def get_response_file(
self,
hash: str
) -> ResponseFile:
path = f"{hash[:2]}/{hash}"
if self.download_dir:
path = f"download/{path}"
return await self.get_file(path)
@abc.abstractmethod
async def get_file(
self,
path: str,
) -> ResponseFile:
raise NotImplementedError
@property
def task_group(self) -> anyio.abc.TaskGroup:
if self._task_group is None:
raise RuntimeError("Storage is not setup")
return self._task_group
@property
def name(self) -> str:
return self._name
@property
def path(self) -> 'CPath':
return self._path
async def write_measure(self, size: int):
path = f"measure/{size}"
size = size * 1024 * 1024
chunk = b"\x00" * (1024 * 1024)
with tempfile.TemporaryFile() as tmp:
remain = size
while remain > 0:
w = min(remain, len(chunk))
tmp.write(chunk[:w])
remain -= w
tmp.seek(0)
await self.upload(
path,
tmp,
size
)
logger.tsuccess("storage.write_measure", size=int(size / (1024 * 1024)), name=self.name, type=self.type)
def get_py_check_path(self) -> 'CPath':
return self.path / ".py_check"
def emit_status(self):
logger.debug(f"Storage {self.name} online status: {self.online}")
utils.event.emit("storage.status", (self, self.online))
class CPath:
def __init__(
self,
path: str
) -> None:
self._path = CPath.convert_path(path).rstrip("/")
# convert '\\' to '/'
@staticmethod
def convert_path(path: str) -> str:
return path.replace("\\", "/")
def __str__(self) -> str:
return self._path
def __repr__(self) -> str:
return self._path
def __eq__(self, __o: object) -> bool:
if not isinstance(__o, CPath):
return False
return self._path == __o._path
def __hash__(self) -> int:
return hash(self._path)
def __truediv__(self, __o: object) -> "CPath":
if isinstance(__o, (int, float)):
__o = str(__o)
if not isinstance(__o, str):
raise TypeError("unsupported operand type(s) for /: 'CPath' and '{}'".format(type(__o).__name__))
return CPath(self._path + "/" + __o)
def __rtruediv__(self, __o: object) -> "CPath":
if isinstance(__o, (int, float)):
__o = str(__o)
if not isinstance(__o, str):
raise TypeError("unsupported operand type(s) for /: '{}' and 'CPath'".format(type(__o).__name__))
return CPath(__o + self._path)
def __len__(self) -> int:
return len(self._path)
@property
def parents(self) -> list['CPath']:
paths = self._path.split("/")
res = []
d = ""
for p in paths:
if p == "":
continue
d += "/" + p
res.append(CPath(d))
res.pop()
if not res:
res.append(CPath("/"))
return res
@property
def parent(self) -> 'CPath':
return self.parents[-1]
@property
def name(self) -> str:
if "/" in self._path:
return self._path.split("/")[-1]
return self._path
RANGE = range(0x00, 0xFF + 1)