34 lines
844 B
Python
34 lines
844 B
Python
# Config file handling (yaml)
|
|
|
|
import yaml
|
|
|
|
class Config:
|
|
def __init__(self, config_file):
|
|
self.config_file = config_file
|
|
self.load()
|
|
|
|
def load(self):
|
|
try:
|
|
with open(self.config_file, 'r') as file:
|
|
self.config = yaml.safe_load(file)
|
|
except FileNotFoundError:
|
|
self.config = {}
|
|
|
|
def save(self):
|
|
with open(self.config_file, 'w') as file:
|
|
yaml.dump(self.config, file, default_flow_style=False)
|
|
|
|
def get(self, key, default=None):
|
|
return self.config.get(key, default)
|
|
|
|
def set(self, key, value):
|
|
self.config[key] = value
|
|
|
|
if __name__ == '__main__':
|
|
my_config = Config('config.yml')
|
|
|
|
# Load the configuration from the file
|
|
my_config.load()
|
|
|
|
# Print current configuration
|
|
print(my_config.config.items())
|