全局资源文件加载

一、使用 @classmethod 全局加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tomllib


class Config:
_profile = None

@classmethod
def __load(cls):
if cls._profile is None:
import os
# 根据当前配置加载文件获取 config.toml 文件的配置路径
root_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(root_dir, "config.toml"), "rb") as fp:
config_data = tomllib.load(fp)
profiles = config_data.get("profiles")
active = profiles.get("active")
cls._profile = profiles.get(active)
return cls._profile

@classmethod
def get(cls, key: str):
if cls._profile is None:
cls.__load()
return cls._profile.get(key)

全局资源文件加载
https://probiecoder.cn/python/global-env-load.html
作者
duwei
发布于
2025年5月22日
许可协议