Skip to content

Commit

Permalink
Fix: 重构林汐配置管理
Browse files Browse the repository at this point in the history
Fix: 重构林汐配置管理 (#5 )
  • Loading branch information
KomoriDev authored Jul 22, 2023
2 parents 7c5d7cf + 1fe43ae commit f941835
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 441 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ QQGUILD_BOTS='
'

telegram_bots = [{"token": "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI"}]
telegram_proxy = ""
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ data/
.idea
.ruff_cache
accounts/
config.yaml

### Node ###

Expand Down
7 changes: 1 addition & 6 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
driver.register_adapter(ONEBOT_V11Adapter)
driver.register_adapter(ONEBOT_V12Adapter)
driver.register_adapter(QQGUILD_Adapter)
driver.register_adapter(TG_Adapter)
driver.register_adapter(TG_Adapter, telegram_proxy=driver.config.proxy)

nonebot.load_builtin_plugins("echo")

nonebot.load_from_toml("pyproject.toml")
nonebot.load_plugin("sora")


from sora import config

if config.WithGoCQHTTP.enabled:
nonebot.load_plugin("nonebot_plugin_gocqhttp")

if __name__ == "__main__":
nonebot.run()
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"docs:dev": "vitepress dev website",
"docs:build": "vitepress build website",
"docs:preview": "vitepress preview website"
},
"dependencies": {
"pyright": "^1.1.318"
}
}
}
7 changes: 2 additions & 5 deletions sora/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

from sora import database
from sora.utils import DRIVER
from sora.config import Config
from sora.config.path import SORA_CONFIG
from sora.config import ConfigManager

DBConfigType = dict[str, Any]

__conf = Config(SORA_CONFIG)
config = __conf.parse()


async def _init(self, db_config: "DBConfigType", create_db: bool):
if self._db_config is None:
Expand All @@ -38,6 +34,7 @@ async def _init(self, db_config: "DBConfigType", create_db: bool):
async def startup():
logger.opt(colors=True).info(logo)
await database.connect()
ConfigManager.init()
# await PluginManager.init()
# asyncio.ensure_future(check_resource())

Expand Down
7 changes: 6 additions & 1 deletion sora/config/__init__.py
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)
45 changes: 0 additions & 45 deletions sora/config/config.py

This file was deleted.

9 changes: 9 additions & 0 deletions sora/config/config.yaml
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
47 changes: 0 additions & 47 deletions sora/config/console.py

This file was deleted.

65 changes: 0 additions & 65 deletions sora/config/create.py

This file was deleted.

26 changes: 0 additions & 26 deletions sora/config/default_config.yaml

This file was deleted.

51 changes: 0 additions & 51 deletions sora/config/models.py

This file was deleted.

3 changes: 2 additions & 1 deletion sora/config/path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path

# 配置路径
SORA_CONFIG = Path() / "config.yaml"
SORA_CONFIG = Path() / "sora" / "config" / "config.yaml"

# 图片路径
IMAGE_PATH = Path() / "resources" / "image"
Expand Down Expand Up @@ -35,6 +35,7 @@ def load_path():
FONT_PATH.mkdir(parents=True, exist_ok=True)
TEMPLATE_PATH.mkdir(parents=True, exist_ok=True)
DATABASE_PATH.mkdir(parents=True, exist_ok=True)
PLUGIN_PATH.mkdir(parents=True, exist_ok=True)


load_path()
57 changes: 57 additions & 0 deletions sora/config/utils/__init__.py
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
Loading

0 comments on commit f941835

Please sign in to comment.