-
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.
Using variconf has several advantages: - Easy merging of values from config file and command line arguments - Automatic checks for missing values and wrong types - Using a dataclass for the config instead of the dictionary-like ConfigProvider allows autocompletion of config values. Remove the ConfigProvider class and instead add a module `server.config`, which holds a `Config` dataclass and a global instance which can be accessed via `get_config()`. `load_config()` is used to load config from the given config file and merges it with potential overwrites from the command line. BREAKING: The command line overwrites are now done like this: ``` ... --config-overwrites port=1234 data_dir=/foo/bar ... ``` So there is no separate argument for each value anymore. While this reduces the ease of use a bit (no help text for possible arguments), it greatly improves maintainability as config values only need to be specified in one place.
- Loading branch information
Showing
12 changed files
with
132 additions
and
161 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
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,69 @@ | ||
"""Configuration settings for the server.""" | ||
|
||
import dataclasses | ||
import pathlib | ||
|
||
try: | ||
import tomllib # type: ignore[import-not-found] | ||
except ImportError: | ||
# tomllib was added in Python 3.11. Older versions can use tomli | ||
import tomli as tomllib # type: ignore[import-not-found, no-redef] | ||
|
||
import omegaconf | ||
import variconf | ||
|
||
|
||
@dataclasses.dataclass | ||
class Config: | ||
"""Configuration settings.""" | ||
|
||
#: Port to listen on | ||
port: int = 8080 | ||
#: Seconds to wait for a player to answer | ||
timeout: int = 10 | ||
#: Log level used by the server | ||
log_level: str = "INFO" | ||
#: File containing the game class to run | ||
game_path: pathlib.Path = omegaconf.MISSING | ||
#: Class name of the game | ||
game_class: str = omegaconf.MISSING | ||
#: Path to the database file | ||
database_path: pathlib.Path = omegaconf.MISSING | ||
#: Path to the data directory (used to save data like game actions) | ||
data_dir: pathlib.Path = omegaconf.MISSING | ||
#: Threshold for matching players | ||
match_quality_threshold: float = 0.8 | ||
#: Percentage of players always waiting in queue | ||
percentage_min_players_waiting: float = 0.1 | ||
#: (Minutes waiting * percentage) added as a time bonus for waiting players | ||
percental_time_bonus: float = 0.1 | ||
|
||
|
||
_config: Config | None = None | ||
|
||
|
||
def get_config() -> Config: | ||
"""Get global config instance.""" | ||
global _config | ||
if _config is None: | ||
_config = Config() | ||
return _config | ||
|
||
|
||
def set_config(config: Config): | ||
"""Set global config instance.""" | ||
global _config | ||
_config = config | ||
|
||
|
||
def load_config(config_file: pathlib.Path, dotlist_overwrites: list[str]) -> Config: | ||
"""Load config from config file and optional dotlist overwrites.""" | ||
wconf = variconf.WConf(Config) | ||
|
||
with open(config_file, "rb") as f: | ||
config_from_file = tomllib.load(f)["CompetitionServer"] | ||
|
||
config = wconf.load_dict(config_from_file).load_dotlist(dotlist_overwrites) | ||
set_config(Config(**config.get())) # type: ignore[arg-type] | ||
|
||
return get_config() |
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
Oops, something went wrong.