plugin: handle resource files in configs

This commit is contained in:
dece 2020-10-09 16:03:09 +02:00
parent 8b5721375f
commit 61703b484a
6 changed files with 14 additions and 1 deletions

2
.gitignore vendored
View file

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

View file

@ -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

View file

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

View file

@ -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()

View file

@ -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):

0
resources/.gitkeep Normal file
View file