-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdata_storage.py
More file actions
49 lines (41 loc) · 1.49 KB
/
data_storage.py
File metadata and controls
49 lines (41 loc) · 1.49 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
import json
import os
class DataStorage:
def __init__(self, file_name="default.json"):
# 使用AppData目录存储数据
appdata_dir = os.environ.get("APPDATA")
my_app_dir = os.path.join(appdata_dir, "NetStealer")
os.makedirs(my_app_dir, exist_ok=True)
self.file_path = os.path.join(my_app_dir, file_name)
self.data = self._load_data()
def _load_data(self):
if os.path.exists(self.file_path):
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except:
return []
return []
def _save_data(self):
with open(self.file_path, 'w', encoding='utf-8') as f:
json.dump(self.data, f, ensure_ascii=False, indent=2)
def add_entry(self, mac_address, note=""):
entry = {
"mac_address": mac_address,
"note": note
}
self.data.append(entry)
self._save_data()
def remove_entry(self, index):
if 0 <= index < len(self.data):
self.data.pop(index)
self._save_data()
def update_entry(self, index, mac_address=None, note=None):
if 0 <= index < len(self.data):
if mac_address is not None:
self.data[index]["mac_address"] = mac_address
if note is not None:
self.data[index]["note"] = note
self._save_data()
def get_all_entries(self):
return self.data