-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: 重构林汐配置管理 (#5 )
- Loading branch information
Showing
22 changed files
with
264 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,6 @@ data/ | |
.idea | ||
.ruff_cache | ||
accounts/ | ||
config.yaml | ||
|
||
### Node ### | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
from .config import Config as Config | ||
from sora.utils.files import load_yaml | ||
from sora.config.path import SORA_CONFIG | ||
|
||
from .utils import ConfigManager as ConfigManager | ||
|
||
load_yaml(SORA_CONFIG) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Award: | ||
login: # ---> [20, 10, 0] | ||
- 20 # 硬币奖励 | ||
- 10 # 好感度奖励 | ||
- 0 # 经验值奖励 | ||
sign: # ---> [[30, 60], 5, 100] | ||
- [30, 60] | ||
- 5 | ||
- 100 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from ruamel import yaml | ||
from ruamel.yaml import YAML | ||
|
||
from sora.log import logger | ||
from sora.config.path import SORA_CONFIG | ||
|
||
|
||
class ConfigManager: | ||
config_path = SORA_CONFIG | ||
|
||
@classmethod | ||
def init(cls): | ||
logger.success("配置管理器", "<g>初始化完成</g>") | ||
|
||
@classmethod | ||
def load_config(cls): | ||
with open(cls.config_path, encoding="utf-8") as f: | ||
config = yaml.safe_load(f) | ||
return config | ||
|
||
@classmethod | ||
def save_config(cls, config): | ||
yaml = YAML() | ||
yaml.dump(config, open(cls.config_path, "w", encoding="utf-8")) | ||
|
||
@classmethod | ||
def set_config(cls, key, value): | ||
config = cls.load_config() | ||
keys = key.split(".") | ||
nested_config = config | ||
for k in keys[:-1]: | ||
nested_config = nested_config.get(k, {}) | ||
if keys[-1] not in nested_config: | ||
raise ValueError(f'配置项 "{key}" 不存在') | ||
nested_config[keys[-1]] = value | ||
cls.save_config(config) | ||
|
||
@classmethod | ||
def add_config(cls, key, value): | ||
config = cls.load_config() | ||
keys = key.split(".") | ||
nested_config = config | ||
for k in keys[:-1]: | ||
nested_config = nested_config.setdefault(k, {}) | ||
if keys[-1] in nested_config: | ||
raise ValueError(f'配置项 "{key}" 已存在') | ||
nested_config[keys[-1]] = value | ||
cls.save_config(config) | ||
|
||
@classmethod | ||
def get_config(cls, key): | ||
config = cls.load_config() | ||
keys = key.split(".") | ||
nested_config = config | ||
for k in keys: | ||
nested_config = nested_config.get(k, {}) | ||
return nested_config |
Oops, something went wrong.