feat(utils): add settings.toml

This commit is contained in:
Kristofers Solo 2024-01-07 15:55:07 +02:00
parent 06f962f5c7
commit f1e854a38c
4 changed files with 58 additions and 1 deletions

21
settings.toml Normal file
View File

@ -0,0 +1,21 @@
[keybinds]
rotate_colockwise = "up arrow"
hard_drop = "space"
hold = "shift+c"
rotate_counter_colockwise = "ctrl+z"
pause = "esc+F1"
move_left = "left arrow"
move_right = "right arrow"
move_down = "down arrow"
[numpad]
hold = "numpad 0"
hard_drop = "numpad 5"
move_left = "numpad 4"
move_right = "numpad 6"
soft_drop = "numpad 2"
rotate_colockwise_1 = "numpad 1"
rotate_colockwise_2 = "numpad 5"
rotate_colockwise_3 = "numpad 9"
rotate_counter_colockwise_1 = "numpad 3"
rotate_counter_colockwise_2 = "numpad 7"

View File

@ -3,6 +3,7 @@ from .enum import Direction, GameMode, Rotation
from .figure import Figure, FigureConfig
from .log import log
from .path import BASE_PATH
from .settings import read_settings, save_settings
from .tuples import BestMove, Size
from .weights import Weights
@ -18,4 +19,6 @@ __all__ = [
"GameMode",
"Weights",
"BestMove",
"read_settings",
"save_settings",
]

View File

@ -1,6 +1,6 @@
from pathlib import Path
from attr import define, field
from attr import define
from pygame import Vector2 as Vec2
from .colors import TokyoNightNight

33
src/utils/settings.py Normal file
View File

@ -0,0 +1,33 @@
from pathlib import Path
from typing import Optional
import toml
from .config import CONFIG, Config
from .log import log
def save_settings(settings: Config, file_path: Path) -> None:
with open(file_path, "w") as file:
toml.dump(settings, file)
def read_settings(file_path: Path) -> Optional[dict[str, str]]:
"""
Read and parse a TOML file and return the content as a dictionary.
Args:
file_path: The path to the TOML file.
Returns:
dict: The parsed content of the TOML file.
"""
try:
with open(file_path, "r") as file:
return toml.load(file)
except FileNotFoundError:
log.error(f"Error: The file '{file_path}' does not exist.")
return None
except toml.TomlDecodeError as e:
log.error(f"rror decoding TOML file: {e}")
return None