This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
settings.py
82 lines (56 loc) · 2.36 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Module to manage settings.
This module provides a centralized way to manage application settings,
stored in a JSON file. It contains a class, SettingsManager, which encapsulates
the methods to load, save, get, and set these settings.
Example:
from settings import settings
log_level = settings.get('log_level', 'INFO')
settings.set('log_level', 'DEBUG')
"""
import json
class SettingsManager:
"""Class to manage application settings.
The SettingsManager class reads settings from a JSON file and provides methods
to get and set these settings. Any changes to the settings are immediately
saved to the file.
Attributes:
settings_file (str): The name of the file where settings are stored.
settings (dict): Dictionary containing the settings.
"""
def __init__(self, settings_file='config/settings.json'):
"""Initialize the SettingsManager with a given settings file.
Args:
settings_file (str): The name of the file to read settings from. Defaults to 'settings.json'.
"""
self.settings_file = settings_file
self.load()
def load(self):
"""Load settings from the JSON file.
Reads the JSON file specified in `settings_file` and loads it into the `settings` dictionary.
"""
with open(self.settings_file, 'r') as f:
self.settings = json.load(f)
def save(self):
"""Save the current settings to the JSON file.
Writes the contents of the `settings` dictionary back to the JSON file.
"""
with open(self.settings_file, 'w') as f:
json.dump(self.settings, f, indent=4)
def get(self, key, default=None):
"""Retrieve a setting value by its key.
Args:
key (str): The key for the setting.
Returns:
The value for the given key, or the default value if the key does not exist.
"""
return self.settings.get(key, default)
def set(self, key, value):
"""Set a setting value by its key.
Args:
key (str): The key for the setting.
value: The value to set.
"""
self.settings[key] = value
self.save()
# Initialize a global instance to be used throughout the application
settings = SettingsManager()