plugin: handle resource files in configs

master
dece 4 years ago
parent 8b5721375f
commit 61703b484a

2
.gitignore vendored

@ -1 +1,3 @@
config.json
resources/*
!resources/.gitkeep

@ -24,3 +24,5 @@ Missing features
- [x] Wikipedia: find definition, get random page
- [ ] Wolframalpha
- [ ] Youtube: parsing for title, requests for channel or video
- [ ] Command aliases
- [ ] Question aliases

@ -5,6 +5,7 @@
"alternative_nicks": ["edmon", "edmond"],
"channels": ["#idi0crates"],
"speak_delay": 0.5,
"resources_dir": "resources",
"plugins": {
"common": {
"command_suffix": "please"

@ -5,5 +5,6 @@ def load_config(config_path, logger=None):
try:
with open(config_path, "rt") as config_file:
return json.load(config_file)
except OSError as exc:
except (OSError, json.decoder.JSONDecodeError) as exc:
logger.critical(f"Could not load config file: {exc}")
exit()

@ -1,4 +1,5 @@
from dataclasses import dataclass
from pathlib import Path
class Plugin:
@ -25,6 +26,12 @@ class Plugin:
plugins_configs = self.bot.config["plugins"]
config = plugins_configs["common"].copy()
config.update(plugins_configs.get(self.name, {}))
# Load resources as config values.
resources_dir = self.bot.config["resources_dir"]
for key, resource_path in config.get("resources", {}).items():
resource_path = Path(resources_dir) / resource_path
with open(resource_path, "rt") as resource_file:
config[key] = [l.strip() for l in resource_file.readlines()]
return config
def check_config(self):

Loading…
Cancel
Save