Skip to content

Commit 3870231

Browse files
committed
simplify validator dummy config and lazy data dir
1 parent 8ba41b6 commit 3870231

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

scripts/validate_plugins/run.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -654,27 +654,14 @@ def __bool__(self) -> bool:
654654

655655
class DummyConfig(dict):
656656
def __init__(self, initial=None) -> None:
657-
super().__init__()
658-
if initial:
659-
for key, value in initial.items():
660-
self[key] = value
661-
662-
@staticmethod
663-
def _wrap(value):
664-
if isinstance(value, dict) and not isinstance(value, DummyConfig):
665-
return DummyConfig(value)
666-
return value
667-
668-
def __setitem__(self, key, value) -> None:
669-
super().__setitem__(key, self._wrap(value))
670-
671-
def __getitem__(self, key):
672-
if key in self:
673-
return super().__getitem__(key)
657+
super().__init__(initial or {})
658+
659+
def __missing__(self, key):
660+
del key
674661
return NullStub()
675662

676663
def __getattr__(self, name: str):
677-
return self.get(name)
664+
return self[name]
678665

679666

680667
class DummyContext:
@@ -683,25 +670,30 @@ def __init__(self) -> None:
683670
self._astrbot_root = Path(os.environ.get("ASTRBOT_ROOT", Path.cwd())).resolve()
684671
self._data_root = self._astrbot_root / "data"
685672
self._plugin_data_dir = self._data_root / "plugin_data"
686-
self._plugin_data_dir.mkdir(parents=True, exist_ok=True)
687673
self._config = DummyConfig(
688674
{
689675
"wake_prefix": [],
690-
"dashboard": {},
676+
"dashboard": DummyConfig(),
691677
"admins_id": [],
692678
"admin_ids": [],
693-
"platform_settings": {
679+
"platform_settings": DummyConfig(
680+
{
694681
"aiocqhttp": {},
695682
"qqofficial": {},
696683
"telegram": {},
697684
"gewechat": {},
698685
"wechatpadpro": {},
699-
},
686+
}
687+
),
700688
"data_dir": str(self._data_root),
701689
}
702690
)
703691
self.config = self._config
704692

693+
def _ensure_plugin_data_dir(self) -> Path:
694+
self._plugin_data_dir.mkdir(parents=True, exist_ok=True)
695+
return self._plugin_data_dir
696+
705697
def get_all_stars(self):
706698
try:
707699
from astrbot.core.star.star import star_registry
@@ -735,11 +727,10 @@ def get_config(self, umo: str | None = None):
735727
return self._config
736728

737729
def get_context_config(self):
738-
return self._config
730+
return self.get_config()
739731

740732
def get_data_dir(self) -> str:
741-
self._plugin_data_dir.mkdir(parents=True, exist_ok=True)
742-
return str(self._plugin_data_dir)
733+
return str(self._ensure_plugin_data_dir())
743734

744735
def __getattr__(self, name: str) -> NullStub:
745736
del name

tests/test_validate_plugins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,23 @@ def test_load_plugins_index_rejects_non_dict_values(self):
386386

387387

388388
class DummyContextStubTests(unittest.IsolatedAsyncioTestCase):
389+
def test_dummy_context_defers_plugin_data_dir_creation_until_requested(self):
390+
module = load_validator_module()
391+
392+
with tempfile.TemporaryDirectory() as tmp_dir:
393+
astrbot_root = Path(tmp_dir) / "astrbot-root"
394+
plugin_data_dir = astrbot_root / "data" / "plugin_data"
395+
396+
with mock.patch.dict(os.environ, {"ASTRBOT_ROOT": str(astrbot_root)}, clear=True):
397+
context = module.DummyContext()
398+
dir_exists_before = plugin_data_dir.exists()
399+
created_dir = Path(context.get_data_dir())
400+
dir_exists_after = plugin_data_dir.is_dir()
401+
402+
self.assertFalse(dir_exists_before)
403+
self.assertEqual(created_dir.resolve(), plugin_data_dir.resolve())
404+
self.assertTrue(dir_exists_after)
405+
389406
def test_dummy_context_returns_worker_data_dir_for_plugin_storage(self):
390407
module = load_validator_module()
391408

0 commit comments

Comments
 (0)