Docstring

This commit is contained in:
CPD 2025-02-25 12:11:13 +01:00
parent a66f014ce2
commit 0f460b7d02

View File

@ -2,6 +2,13 @@ from os import environ, makedirs, path
import yaml import yaml
class ConfigFile: class ConfigFile:
"""
Class managing a yaml config file.
The file is loaded on creation and can be saved with .save().
You may initialize an instance with empty string as filepath,
in this case no file will not be loaded or saved.
"""
def __init__(self, filepath: str, init_values = None): def __init__(self, filepath: str, init_values = None):
self.values = {} self.values = {}
if init_values: if init_values:
@ -19,10 +26,14 @@ class ConfigFile:
with open(self.filepath, "w") as file: with open(self.filepath, "w") as file:
yaml.dump(self.values, file) yaml.dump(self.values, file)
def get(self, name: str, default=None): def get_or(self, name: str, default):
if name in self.values: return self.values[name] if name in self.values: return self.values[name]
return default return default
def get(self, name: str):
if name in self.values: return self.values[name]
raise KeyError(f"Key '{name}' not found in config file '{self.filepath}'")
def set(self, name: str, value): def set(self, name: str, value):
self.values[name] = value self.values[name] = value