Move core matching factors to the config file

Bonus changes here were making the config a singleton, fixing some more tests and then re-writing the stress test because it was pissing me off.
This commit is contained in:
Marc Di Luzio 2024-08-11 22:07:43 +01:00
parent a5d7dae851
commit 9043615498
6 changed files with 254 additions and 86 deletions

View file

@ -1,5 +1,5 @@
"""Very simple config loading library"""
from schema import Schema, And, Use
from schema import Schema, And, Use, Optional
import files
import os
import logging
@ -13,10 +13,18 @@ _FILE = "config.json"
_VERSION = 1
class _Keys():
class _Key():
TOKEN = "token"
VERSION = "version"
MATCH = "match"
SCORE_FACTORS = "score_factors"
REPEAT_ROLE = "repeat_role"
REPEAT_MATCH = "repeat_match"
EXTRA_MEMBER = "extra_member"
UPPER_THRESHOLD = "upper_threshold"
# Removed
OWNERS = "owners"
@ -24,10 +32,21 @@ class _Keys():
_SCHEMA = Schema(
{
# The current version
_Keys.VERSION: And(Use(int)),
_Key.VERSION: And(Use(int)),
# Discord bot token
_Keys.TOKEN: And(Use(str)),
_Key.TOKEN: And(Use(str)),
# Settings for the match algorithmn, see matching.py for explanations on usage
Optional(_Key.MATCH): {
Optional(_Key.SCORE_FACTORS): {
Optional(_Key.REPEAT_ROLE): And(Use(int)),
Optional(_Key.REPEAT_MATCH): And(Use(int)),
Optional(_Key.EXTRA_MEMBER): And(Use(int)),
Optional(_Key.UPPER_THRESHOLD): And(Use(int)),
}
}
}
)
@ -35,7 +54,7 @@ _SCHEMA = Schema(
def _migrate_to_v1(d: dict):
# Owners moved to History in v1
# Note: owners will be required to be re-added to the state.json
owners = d.pop(_Keys.OWNERS)
owners = d.pop(_Key.OWNERS)
logger.warn(
"Migration removed owners from config, these must be re-added to the state.json")
logger.warn("Owners: %s", owners)
@ -47,7 +66,29 @@ _MIGRATIONS = [
]
class Config():
class _ScoreFactors():
def __init__(self, data: dict):
"""Initialise and validate the config"""
self._dict = data
@property
def repeat_role(self) -> int:
return self._dict.get(_Key.REPEAT_ROLE, None)
@property
def repeat_match(self) -> int:
return self._dict.get(_Key.REPEAT_MATCH, None)
@property
def extra_member(self) -> int:
return self._dict.get(_Key.EXTRA_MEMBER, None)
@property
def upper_threshold(self) -> int:
return self._dict.get(_Key.UPPER_THRESHOLD, None)
class _Config():
def __init__(self, data: dict):
"""Initialise and validate the config"""
_SCHEMA.validate(data)
@ -57,6 +98,10 @@ class Config():
def token(self) -> str:
return self._dict["token"]
@property
def score_factors(self) -> _ScoreFactors:
return _ScoreFactors(self._dict.get(_Key.SCORE_FACTORS, {}))
def _migrate(dict: dict):
"""Migrate a dict through versions"""
@ -66,7 +111,7 @@ def _migrate(dict: dict):
dict["version"] = _VERSION
def load_from_file(file: str = _FILE) -> Config:
def _load_from_file(file: str = _FILE) -> _Config:
"""
Load the state from a file
Apply any required migrations
@ -74,4 +119,9 @@ def load_from_file(file: str = _FILE) -> Config:
assert os.path.isfile(file)
loaded = files.load(file)
_migrate(loaded)
return Config(loaded)
return _Config(loaded)
# Core config for users to use
# Singleton as there should only be one, and it's global
Config = _load_from_file()