|
4 | 4 | import random |
5 | 5 | import re |
6 | 6 |
|
7 | | -from core import Extension, utils, Server, YAMLError, DEFAULT_TAG, MizFile, ServerImpl |
| 7 | +from core import Extension, utils, Server, YAMLError, DEFAULT_TAG, MizFile, ServerImpl, Status |
8 | 8 | from datetime import datetime |
9 | 9 | from extensions.realweather import RealWeather |
10 | 10 | from pathlib import Path |
|
24 | 24 |
|
25 | 25 | class MizEdit(Extension): |
26 | 26 |
|
27 | | - async def prepare(self) -> bool: |
| 27 | + def __init__(self, server: Server, config: dict): |
| 28 | + super().__init__(server, config) |
| 29 | + if server.status != Status.SHUTDOWN: |
| 30 | + self.presets = self._init_presets() |
| 31 | + else: |
| 32 | + self.presets = {} |
| 33 | + |
| 34 | + def _init_presets(self): |
28 | 35 | presets_file = self.config.get('presets', os.path.join(self.node.config_dir, 'presets.yaml')) |
29 | | - self.presets = {} |
| 36 | + presets = {} |
30 | 37 | if not isinstance(presets_file, list): |
31 | 38 | presets_file = [presets_file] |
32 | 39 | for file in presets_file: |
33 | 40 | try: |
34 | | - self.presets |= yaml.load(Path(file).read_text(encoding='utf-8')) |
35 | | - if not isinstance(self.presets, dict): |
| 41 | + presets |= yaml.load(Path(file).read_text(encoding='utf-8')) |
| 42 | + if not isinstance(presets, dict): |
36 | 43 | raise ValueError("File must contain a dictionary, not a list!") |
37 | 44 | except FileNotFoundError: |
38 | 45 | self.log.error(f"MizEdit: File {file} not found!") |
39 | 46 | continue |
40 | 47 | except (MarkedYAMLError, ValueError) as ex: |
41 | 48 | raise YAMLError(file, ex) |
| 49 | + return presets |
| 50 | + |
| 51 | + async def prepare(self) -> bool: |
| 52 | + self.presets = self._init_presets() |
42 | 53 | return await super().prepare() |
43 | 54 |
|
44 | 55 | async def get_presets(self, config: dict) -> list[dict]: |
|
0 commit comments