获取配置值
(cls, key, default=None)
| 92 | |
| 93 | @classmethod |
| 94 | def get(cls, key, default=None): |
| 95 | """获取配置值""" |
| 96 | instance = cls.get_instance() |
| 97 | # 读取时,加锁或不加锁都行。但为了统一,我们在修改配置前后都要加锁。 |
| 98 | # get 只要最终需要保存,则会加锁 -> _save_config() |
| 99 | if key in instance._config_data: |
| 100 | return instance._config_data[key] |
| 101 | |
| 102 | # 若环境变量中存在该 key,则使用环境变量并写回 config |
| 103 | if key in os.environ: |
| 104 | value = os.environ[key] |
| 105 | instance._config_data[key] = value |
| 106 | instance._save_config() |
| 107 | return value |
| 108 | |
| 109 | # 若 default 不为 None,则设置并保存 |
| 110 | if default is not None: |
| 111 | instance._config_data[key] = default |
| 112 | instance._save_config() |
| 113 | return default |
| 114 | |
| 115 | # 找不到则抛出异常 |
| 116 | # raise KeyError(f"{key} is not found in config file or environment variables.") |
| 117 | return default |
| 118 | |
| 119 | @classmethod |
| 120 | def set(cls, key, value): |
no test coverage detected