From 474ff76861f05366b26ba26440afe953105b3575 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Sat, 2 Sep 2023 22:40:57 +0800 Subject: [PATCH 1/3] :sparkles: support custom dir --- nonebot_plugin_localstore/__init__.py | 24 ++++++++++++++++++++---- nonebot_plugin_localstore/config.py | 9 +++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 nonebot_plugin_localstore/config.py diff --git a/nonebot_plugin_localstore/__init__.py b/nonebot_plugin_localstore/__init__.py index 2bb253f..50135d3 100644 --- a/nonebot_plugin_localstore/__init__.py +++ b/nonebot_plugin_localstore/__init__.py @@ -2,8 +2,10 @@ from typing import Callable, Optional from typing_extensions import ParamSpec +from nonebot import get_driver from nonebot.plugin import PluginMetadata +from .config import Config from .data_source import user_data_dir, user_cache_dir, user_config_dir __plugin_meta__ = PluginMetadata( @@ -21,16 +23,30 @@ ), type="library", homepage="https://github.com/nonebot/plugin-localstore", - config=None, + config=Config, supported_adapters=None, ) +plugin_config = Config.parse_obj(get_driver().config) + P = ParamSpec("P") APP_NAME = "nonebot2" -BASE_CACHE_DIR = user_cache_dir(APP_NAME).resolve() -BASE_CONFIG_DIR = user_config_dir(APP_NAME).resolve() -BASE_DATA_DIR = user_data_dir(APP_NAME).resolve() +BASE_CACHE_DIR = ( + user_cache_dir(APP_NAME).resolve() + if plugin_config.localstore_cache_dir is None + else plugin_config.localstore_cache_dir +) +BASE_CONFIG_DIR = ( + user_config_dir(APP_NAME).resolve() + if plugin_config.localstore_config_dir is None + else plugin_config.localstore_config_dir +) +BASE_DATA_DIR = ( + user_data_dir(APP_NAME).resolve() + if plugin_config.localstore_data_dir is None + else plugin_config.localstore_data_dir +) def _ensure_dir(path: Path) -> None: diff --git a/nonebot_plugin_localstore/config.py b/nonebot_plugin_localstore/config.py new file mode 100644 index 0000000..70b9be6 --- /dev/null +++ b/nonebot_plugin_localstore/config.py @@ -0,0 +1,9 @@ +from pathlib import Path +from typing import Optional + +from pydantic import BaseModel, Extra + +class Config(BaseModel, extra=Extra.ignore): + localstore_cache_dir: Optional[Path] = None + localstore_config_dir: Optional[Path] = None + localstore_data_dir: Optional[Path] = None From b8c80b7c7bdf596b1578f11fa66942f8c5e1f15e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 14:42:09 +0000 Subject: [PATCH 2/3] :rotating_light: auto fix by pre-commit hooks --- nonebot_plugin_localstore/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_localstore/config.py b/nonebot_plugin_localstore/config.py index 70b9be6..1c408db 100644 --- a/nonebot_plugin_localstore/config.py +++ b/nonebot_plugin_localstore/config.py @@ -1,7 +1,8 @@ from pathlib import Path from typing import Optional -from pydantic import BaseModel, Extra +from pydantic import Extra, BaseModel + class Config(BaseModel, extra=Extra.ignore): localstore_cache_dir: Optional[Path] = None From 1a9f234f1956fa39d4a022d3f644caf49030ef04 Mon Sep 17 00:00:00 2001 From: uy_sun Date: Sun, 3 Sep 2023 10:37:03 +0800 Subject: [PATCH 3/3] :construction: resolve path --- nonebot_plugin_localstore/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nonebot_plugin_localstore/__init__.py b/nonebot_plugin_localstore/__init__.py index 50135d3..8e5abdd 100644 --- a/nonebot_plugin_localstore/__init__.py +++ b/nonebot_plugin_localstore/__init__.py @@ -35,17 +35,17 @@ BASE_CACHE_DIR = ( user_cache_dir(APP_NAME).resolve() if plugin_config.localstore_cache_dir is None - else plugin_config.localstore_cache_dir + else plugin_config.localstore_cache_dir.resolve() ) BASE_CONFIG_DIR = ( user_config_dir(APP_NAME).resolve() if plugin_config.localstore_config_dir is None - else plugin_config.localstore_config_dir + else plugin_config.localstore_config_dir.resolve() ) BASE_DATA_DIR = ( user_data_dir(APP_NAME).resolve() if plugin_config.localstore_data_dir is None - else plugin_config.localstore_data_dir + else plugin_config.localstore_data_dir.resolve() )