-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.py
61 lines (50 loc) · 1.92 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
import json
import os
# Define the path to the settings JSON file
SETTINGS_FILE = 'settings.json'
# This dictionary will hold the settings in memory
settings = {}
def load_settings():
"""Load settings from the JSON file."""
global settings
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, 'r') as f:
settings = json.load(f)
# Convert 'known_nodes' back to a set if it exists
if 'known_nodes' in settings:
settings['known_nodes'] = set(settings['known_nodes'])
else:
# If the file doesn't exist, initialize with default settings
settings = get_default_settings()
_save_settings()
def get_default_settings():
"""Return default settings."""
return {
'NODE_ID': os.getenv('NODE_ID', '127.0.0.1:5000'),
'LAST_EXECUTION_FILE': 'last_execution.txt',
'INDEX_FILES_TIME': 1,
'PEER_DISCOVER_INTERVAL': 1,
'DIRECTORY': os.getenv("SHARED_DIRECTORY"),
'URL': 'https://raw.githubusercontent.com/username/repository/branch/path/to/file.json',
'HEARTBEAT_INTERVAL': 10,
'known_nodes': set(os.getenv('KNOWN_NODES', '').split(', ')),
}
def _save_settings():
"""Save the current settings to the JSON file."""
# Convert 'known_nodes' from set to list before saving
settings_to_save = settings.copy()
if 'known_nodes' in settings_to_save:
settings_to_save['known_nodes'] = list(settings_to_save['known_nodes'])
with open(SETTINGS_FILE, 'w') as f:
json.dump(settings_to_save, f, indent=4)
def get_setting(key, default=None):
"""Retrieve a setting value by key."""
return settings.get(key, default)
def set_setting(key, value):
"""Set a setting value and save the updated settings to the file."""
settings[key] = value
_save_settings()
def return_all():
return settings
# Automatically load settings when the module is imported
load_settings()