-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.py
More file actions
322 lines (302 loc) · 14.1 KB
/
inventory.py
File metadata and controls
322 lines (302 loc) · 14.1 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# src/services/inventory.py
from __future__ import annotations
from pathlib import Path
import logging
from collections import deque
from typing import Dict, Deque, List, Tuple
import asyncio
import aiofiles
import io
import zipfile
import shutil
class Inventory:
def __init__(self, dirs: Dict[str, Path], flush_sec: int = 10):
self.dirs = dirs
self.flush_sec = flush_sec
self.cache: Dict[str, Deque[str]] = {k: deque() for k in dirs}
self.locks = {k: asyncio.Lock() for k in dirs}
self._dirty: Dict[str, bool] = {k: False for k in dirs}
self._flusher_task: asyncio.Task | None = None
self._log = logging.getLogger('shopbot')
# Snapshot path helper: prefer '_current_stock' (no extension),
# but also support legacy '_current_stock.txt' for backward compatibility.
def _snapshot_read_path(self, folder: Path) -> Path | None:
p1 = folder / '_current_stock'
p2 = folder / '_current_stock.txt'
if p1.exists():
return p1
if p2.exists():
return p2
return None
def _snapshot_write_path(self, folder: Path) -> Path:
return folder / '_current_stock'
def _e_source_file(self, folder: Path) -> Path:
"""Return the canonical source file for E proxies."""
return folder / 'HANG_E.txt'
def _proxy_source_file(self, folder: Path) -> Path:
"""Return the canonical proxy source file (PROXY.txt) located at inventory root."""
# Use settings.PRODUCT_F_FILE if available to allow customization
try:
from settings import PRODUCT_F_FILE
return folder / PRODUCT_F_FILE
except Exception:
return folder / 'PROXY.txt'
async def start(self):
# Use unified reload path to ensure snapshot validation and correct counting
await self.reload()
self._flusher_task = asyncio.create_task(self._flusher())
async def stop(self):
if self._flusher_task:
self._flusher_task.cancel()
await self.flush_all()
def count(self, key: str) -> int:
return len(self.cache[key])
async def allocate(self, key: str, qty: int) -> Tuple[str, bytes]:
if qty <= 0:
raise ValueError('Số lượng không hợp lệ')
async with self.locks[key]:
if len(self.cache[key]) < qty:
raise ValueError('Kho không đủ số lượng')
taken = [self.cache[key].popleft() for _ in range(qty)]
self._dirty[key] = True
# Build content by product type
if key in ('A', 'B', 'C', 'D', 'J'):
# For A/B/C/D: Combine selected items (files or directories) into a single zip archive
mem = io.BytesIO()
with zipfile.ZipFile(mem, mode='w', compression=zipfile.ZIP_STORED) as zf:
for p in taken:
p_path = Path(p)
try:
if p_path.is_dir():
# Recursively add directory contents
for sub in p_path.rglob('*'):
if sub.is_file():
arc = p_path.name + '/' + sub.relative_to(p_path).as_posix()
zf.write(sub, arcname=arc)
else:
# Add the original file as an entry inside combined zip
zf.write(p_path, arcname=p_path.name)
finally:
# Remove the original item from disk as it's delivered
try:
if p_path.is_dir():
shutil.rmtree(p_path, ignore_errors=True)
else:
p_path.unlink(missing_ok=True)
except Exception:
pass
mem.seek(0)
# Persist snapshot immediately
await self.flush_key(key)
return f'{key}_{qty}.zip', mem.read()
elif key in ('E',):
# E (proxies): 'taken' are the proxy lines themselves; return as text and persist removal
content = ('\n'.join(taken) + '\n').encode('utf-8')
await self.flush_key(key) # will also rewrite HANG_E.txt
return f'{key}_{qty}.txt', content
elif key == 'F':
# F: proxies stored as plain lines (each taken entry is a proxy line)
content = ('\n'.join(taken) + '\n').encode('utf-8')
await self.flush_key(key)
return f'{key}_{qty}.txt', content
else:
# Fallback to plain text for any unknown key
content = ('\n'.join(taken) + '\n').encode('utf-8')
filename = f'{key}_{qty}.txt'
await self.flush_key(key)
return filename, content
async def _flusher(self):
while True:
await asyncio.sleep(self.flush_sec)
await self.flush_all()
async def flush_all(self):
for key, folder in self.dirs.items():
if not self._dirty[key]:
continue
all_lines = list(self.cache[key])
tmp = self._snapshot_write_path(folder)
try:
async with aiofiles.open(tmp, 'w', encoding='utf-8') as fh:
await fh.write('\n'.join(all_lines) + ('\n' if all_lines else ''))
except PermissionError as e:
# Cannot write snapshot into a protected directory (e.g., another user's Desktop). Log and continue.
self._log.warning(f"[INV] Cannot write snapshot to '{tmp}': {e}. Stock will persist in memory only.")
self._dirty[key] = False
async def flush_key(self, key: str):
"""Flush snapshot only for given key."""
folder = self.dirs[key]
all_lines = list(self.cache[key])
tmp = self._snapshot_write_path(folder)
try:
async with aiofiles.open(tmp, 'w', encoding='utf-8') as fh:
await fh.write('\n'.join(all_lines) + ('\n' if all_lines else ''))
except PermissionError as e:
self._log.warning(f"[INV] Cannot write snapshot to '{tmp}': {e}. Stock will persist in memory only.")
self._dirty[key] = False
# For E, also persist back to HANG_E.txt as source of truth
if key == 'E':
efile = self._e_source_file(folder)
try:
async with aiofiles.open(efile, 'w', encoding='utf-8') as fh:
await fh.write('\n'.join(all_lines) + ('\n' if all_lines else ''))
except Exception as e:
self._log.warning(f"[INV] Cannot write E source file '{efile}': {e}")
# For F, persist back to PROXY.txt at inventory root
if key == 'F':
pfile = self._proxy_source_file(folder)
try:
async with aiofiles.open(pfile, 'w', encoding='utf-8') as fh:
await fh.write('\n'.join(all_lines) + ('\n' if all_lines else ''))
except Exception as e:
self._log.warning(f"[INV] Cannot write proxy source file '{pfile}': {e}")
async def _load_dir(self, key: str, folder: Path, *, ignore_snapshot: bool = False):
items: List[str] = []
# Prefer snapshot if exists unless ignoring it explicitly
snap = None if ignore_snapshot else self._snapshot_read_path(folder)
if snap is not None:
snap_items: List[str] = []
async with aiofiles.open(snap, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
line = line.strip()
if line:
snap_items.append(line)
# Validate snapshot format per key
if key in ('A', 'B', 'C', 'D'):
# A/B/C: top-level .session files only; D: top-level directories or .session/.zip files
def _ok(x: str) -> bool:
px = Path(x)
if not px.exists():
return False
if key in ('A', 'B', 'C'):
# Only .session files at top-level
return (px.is_file() and px.suffix.lower() == '.session' and px.parent == folder)
# D can be a directory (TData) or file (.zip/.session) at top-level
return (px.parent == folder) and (px.is_dir() or (px.is_file() and px.suffix.lower() in ('.zip', '.session')))
is_valid = bool(snap_items) and all(_ok(x) for x in snap_items)
if is_valid:
async with self.locks[key]:
self.cache[key].clear()
self.cache[key].extend(snap_items)
return
# else: fall back to scanning raw files
elif key == 'E':
# For E, snapshot contains proxy lines; but we prefer to load from source file always.
pass
# Scan raw files by product type
if key in ('A', 'B', 'C'):
# Top-level scan per specification (no recursion)
# Support both top-level .session files and directories (legacy or grouped sessions)
for f in sorted(folder.iterdir()):
if f.name.startswith('_'):
continue
if f.is_dir():
items.append(str(f))
elif f.is_file() and f.suffix.lower() == '.session':
items.append(str(f))
elif key in ('D', 'J'):
# D and J can contain directories or .zip/.session files
for f in sorted(folder.iterdir()):
if f.name.startswith('_'):
continue
if f.is_dir():
items.append(str(f))
elif f.is_file() and f.suffix.lower() in ('.zip', '.session'):
items.append(str(f))
elif key == 'E':
# Load from HANG_E.txt: one proxy per non-empty line
efile = self._e_source_file(folder)
if efile.exists():
async with aiofiles.open(efile, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
s = line.strip()
if s:
items.append(s)
elif key == 'F':
# Load proxies from PROXY.txt at inventory root
pfile = self._proxy_source_file(folder)
if pfile.exists():
async with aiofiles.open(pfile, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
s = line.strip()
if s:
items.append(s)
# Replace under lock to avoid racing with allocate
async with self.locks[key]:
self.cache[key].clear()
self.cache[key].extend(items)
async def reload(self):
for key, folder in self.dirs.items():
try:
if not folder.exists():
folder.mkdir(parents=True, exist_ok=True)
except (PermissionError, FileNotFoundError) as e:
if folder.exists():
self._log.warning(f"[INV] Cannot create folder '{folder}' during reload: {e}. Proceeding since it exists.")
else:
raise
await self._load_dir(key, folder)
# mark dirty so flusher writes snapshot file
for k in self._dirty:
self._dirty[k] = True
async def rebuild_from_files(self):
"""Discard snapshot and rebuild cache from raw files, then write a fresh snapshot."""
for key, folder in self.dirs.items():
try:
if not folder.exists():
folder.mkdir(parents=True, exist_ok=True)
except (PermissionError, FileNotFoundError) as e:
if folder.exists():
self._log.warning(f"[INV] Cannot create folder '{folder}' during rebuild: {e}. Proceeding since it exists.")
else:
raise
await self._load_dir(key, folder, ignore_snapshot=True)
await self.flush_all()
# ---------- Diagnostics helpers ----------
async def debug_count_snapshot(self, key: str) -> int | None:
"""Return number of non-empty lines in snapshot for a key, or None if no snapshot."""
folder = self.dirs[key]
snap = self._snapshot_read_path(folder)
if snap is None or not snap.exists():
return None
count = 0
async with aiofiles.open(snap, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
if line.strip():
count += 1
return count
async def debug_count_from_files(self, key: str) -> int:
"""Scan source and count items using the same rules as loader."""
folder = self.dirs[key]
if key in ('A', 'B', 'C'):
return len([f for f in folder.iterdir() if not f.name.startswith('_') and (f.is_dir() or (f.is_file() and f.suffix.lower() == '.session'))])
if key in ('D', 'J'):
cnt = 0
for f in folder.iterdir():
if f.name.startswith('_'):
continue
if f.is_dir():
cnt += 1
elif f.is_file() and f.suffix.lower() in ('.zip', '.session'):
cnt += 1
return cnt
if key == 'E':
efile = self._e_source_file(folder)
if not efile.exists():
return 0
c = 0
async with aiofiles.open(efile, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
if line.strip():
c += 1
return c
if key == 'F':
pfile = self._proxy_source_file(folder)
if not pfile.exists():
return 0
c = 0
async with aiofiles.open(pfile, 'r', encoding='utf-8', errors='ignore') as fh:
async for line in fh:
if line.strip():
c += 1
return c
return 0