-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_data.py
More file actions
37 lines (28 loc) · 1.13 KB
/
handle_data.py
File metadata and controls
37 lines (28 loc) · 1.13 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
from dataclasses import dataclass
import json
import os
DATA_DIR = "data/"
@dataclass
class InterpretersData:
FILE_PATH: str
interpreters: list[str] | None = None
global_interpreter: str | None = None
def is_empty(self) -> bool:
return self.interpreters is None and self.global_interpreter is None
def save(self) -> None:
SAVE_PATH = os.path.join(DATA_DIR, self.FILE_PATH)
os.makedirs(DATA_DIR, exist_ok=True)
with open(SAVE_PATH, "w", encoding="utf-8") as f:
json.dump(self.__dict__, f, indent=4)
def __init__(self, file_path) -> None:
self.FILE_PATH = file_path
LOAD_PATH = os.path.join(DATA_DIR, self.FILE_PATH)
if os.path.exists(LOAD_PATH):
with open(LOAD_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
self.interpreters = data.get("interpreters")
self.global_interpreter = data.get("global_interpreter")
else:
self.interpreters = None
self.global_interpreter = None
interpreters_data = InterpretersData("interpreters.json")