# siwei_config.py
import os
import json

from cryptography.fernet import Fernet

# 加载密钥
def load_key(key_path='secret.key'):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    key_path = os.path.join(current_dir, key_path)
    with open(key_path, 'rb') as key_file:
        return key_file.read()

# 解密并返回 JSON 数据（不写入磁盘）
def decrypt_json_to_dict(encrypted_file_path, key_path='secret.key'):
    key = load_key(key_path)
    fernet = Fernet(key)

    with open(encrypted_file_path, 'rb') as f:
        encrypted_data = f.read()

    decrypted_data = fernet.decrypt(encrypted_data)
    return json.loads(decrypted_data.decode('utf-8'))

CONFIG_PATH = r'C:\siwei_model\siwei_config.json.enc'

# 解密并赋值给 CONFIG
CONFIG = decrypt_json_to_dict(CONFIG_PATH)

# 可导出为全局访问变量
globals()['CONFIG'] = CONFIG
