2024-08-28 22:20:36 +02:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
from core.vars import logger
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
file = os.path.join(os.path.expanduser("~"), ".config", "ai-suite-rocm", "config.json")
|
|
|
|
|
|
|
|
|
|
|
|
def create():
|
|
|
|
if not os.path.exists(file):
|
|
|
|
os.makedirs(os.path.dirname(file), exist_ok=True)
|
|
|
|
with open(file, "w") as f:
|
|
|
|
f.write("{}")
|
|
|
|
|
|
|
|
logger.info(f"Created config file at {file}")
|
|
|
|
|
|
|
|
|
|
|
|
def read():
|
|
|
|
global data
|
|
|
|
with open(file, "r") as f:
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
def write():
|
|
|
|
global data
|
|
|
|
with open(file, "w") as f:
|
|
|
|
json.dump(data, f)
|
|
|
|
|
|
|
|
|
2024-08-29 12:03:37 +02:00
|
|
|
def get(key: str, default=None):
|
2024-08-28 22:20:36 +02:00
|
|
|
global data
|
2024-08-29 12:03:37 +02:00
|
|
|
if key not in data:
|
|
|
|
return default
|
|
|
|
|
2024-08-28 22:20:36 +02:00
|
|
|
return data.get(key)
|
|
|
|
|
|
|
|
|
2024-08-29 12:03:37 +02:00
|
|
|
def put(key: str, value):
|
2024-08-28 22:20:36 +02:00
|
|
|
global data
|
|
|
|
data[key] = value
|
|
|
|
write()
|
|
|
|
|
|
|
|
|
|
|
|
def has(key: str):
|
|
|
|
global data
|
|
|
|
return key in data
|
|
|
|
|
|
|
|
|
|
|
|
def remove(key: str):
|
|
|
|
global data
|
2024-08-29 12:03:37 +02:00
|
|
|
if key in data:
|
|
|
|
data.pop(key)
|
2024-08-28 22:20:36 +02:00
|
|
|
write()
|
|
|
|
|
|
|
|
|
|
|
|
def clear():
|
|
|
|
global data
|
|
|
|
data = {}
|
|
|
|
write()
|
2024-08-29 12:03:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_file(filename: str, content: str):
|
|
|
|
with open(os.path.join(os.path.dirname(file), filename), "w") as f:
|
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_file(filename: str):
|
|
|
|
os.remove(os.path.join(os.path.dirname(file), filename))
|
|
|
|
|
|
|
|
|
|
|
|
def file_exists(filename: str):
|
|
|
|
return os.path.exists(os.path.join(os.path.dirname(file), filename))
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_path(filename: str):
|
|
|
|
return os.path.join(os.path.dirname(file), filename)
|
|
|
|
|
|
|
|
|
|
|
|
def open_file(filename: str, mode: str = 'w'):
|
|
|
|
return open(os.path.join(os.path.dirname(file), filename), mode)
|