-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f471d1c
commit b47e723
Showing
8 changed files
with
131 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import json | ||
from pathlib import Path | ||
from dataclasses import dataclass, field | ||
from typing import Callable, ClassVar | ||
|
||
|
||
@dataclass | ||
class Config: | ||
path: Path | ||
basic: dict = field(default_factory=dict, init=False) | ||
plugin: dict[str, dict] = field(default_factory=dict, init=False) | ||
updater: Callable[["Config"], None] | ||
|
||
instance: ClassVar[Config] | ||
|
||
def __post_init__(self): | ||
self.updater(self) | ||
|
||
def reload(self): | ||
self.updater(self) | ||
|
||
@classmethod | ||
def load(cls, path: str | os.PathLike[str] | None = None) -> Config: | ||
if path is None: | ||
_path = Path.cwd() / ".entari.json" | ||
else: | ||
_path = Path(path) | ||
if not _path.exists(): | ||
return cls(_path, lambda _: None) | ||
if not _path.is_file(): | ||
raise ValueError(f"{_path} is not a file") | ||
if _path.suffix.startswith(".json"): | ||
|
||
def _updater(self: Config): | ||
with self.path.open("r", encoding="utf-8") as f: | ||
data = json.load(f) | ||
self.basic = data.get("basic", {}) | ||
self.plugin = data.get("plugin", {}) | ||
obj = cls(_path, _updater) | ||
cls.instance = obj | ||
return obj | ||
if _path.suffix in (".yaml", ".yml"): | ||
try: | ||
import yaml | ||
except ImportError: | ||
raise RuntimeError("yaml is not installed") | ||
|
||
def _updater(self: Config): | ||
with self.path.open("r", encoding="utf-8") as f: | ||
data = yaml.safe_load(f) | ||
self.basic = data.get("basic", {}) | ||
self.plugin = data.get("plugin", {}) | ||
obj = cls(_path, _updater) | ||
cls.instance = obj | ||
return obj | ||
raise NotImplementedError(f"unsupported config file format: {_path.suffix}") | ||
|
||
|
||
load_config = Config.load |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
basic: | ||
ignore_self_message: true | ||
plugins: | ||
::auto_reload: | ||
watch_dirs: ["."] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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