-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_settings.py
More file actions
56 lines (48 loc) · 1.76 KB
/
Copy pathmanage_settings.py
File metadata and controls
56 lines (48 loc) · 1.76 KB
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
import yaml
import os
import shutil
CONFIG_FILE = "D:/CODING/Programming Languages/YAML/settings.yaml"
BACKUP_FILE = CONFIG_FILE + ".bak"
def read_config():
with open(CONFIG_FILE, "r") as file:
return yaml.safe_load(file)
def write_config(config):
with open(CONFIG_FILE, "w") as file:
yaml.safe_dump(config, file)
def backup_config():
shutil.copy(CONFIG_FILE, BACKUP_FILE)
def update_config(environment, section, key, value):
config = read_config()
if environment in config and section in config[environment]:
config[environment][section][key] = value
backup_config()
write_config(config)
else:
print(f"Section {section} not found in {environment} environment.")
def display_config(config):
print(yaml.dump(config, default_flow_style=False))
if __name__ == "__main__":
while True:
print("\nSettings Management System")
print("1. Read Settings")
print("2. Update Settings")
print("3. Display Settings")
print("4. Exit")
choice = input("Choose an option: ")
if choice == "1":
config = read_config()
display_config(config)
elif choice == "2":
environment = input("Enter environment to update (development, testing, production): ")
section = input("Enter section to update (server, database, logging): ")
key = input("Enter key to update: ")
value = input("Enter new value: ")
update_config(environment, section, key, value)
elif choice == "3":
config = read_config()
display_config(config)
elif choice == "4":
print("Exiting...")
break
else:
print("Invalid option. Please try again.")