-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
90 lines (66 loc) · 2.33 KB
/
config_manager.py
File metadata and controls
90 lines (66 loc) · 2.33 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
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
83
84
85
86
87
88
89
90
import configparser
# Global config object
_config = None
def load_config():
"""
Load configuration from settings.ini file.
If file doesn't exist or is missing sections/options, creates defaults.
Returns:
configparser.ConfigParser: Loaded configuration object
"""
global _config
_config = configparser.ConfigParser()
_config.read('settings.ini')
if not _config.has_section('application'):
_config.add_section('application')
if not _config.has_option('application', 'theme'):
_config.set('application', 'theme', 'arc')
if not _config.has_option('application', 'video_source'):
_config.set('application', 'video_source', '0')
if not _config.has_option('application', 'hand_preference'):
_config.set('application', 'hand_preference', 'Right')
if not _config.has_option('application', 'camera_orientation'):
_config.set('application', 'camera_orientation', 'Front Facing')
save_config()
return _config
def save_config():
"""
Save current configuration to settings.ini file.
Creates the file if it doesn't exist.
"""
global _config
if _config is None:
load_config()
with open('settings.ini', 'w') as configfile:
_config.write(configfile)
def get_config_value(section, option, default_value=None):
"""
Get a value from the configuration.
Args:
section (str): Configuration section
option (str): Option name within section
default_value: Value to return if option doesn't exist
Returns:
The configuration value or default_value if not found
"""
global _config
if _config is None:
load_config()
try:
return _config.get(section, option)
except (configparser.NoSectionError, configparser.NoOptionError):
return default_value
def set_config_value(section, option, value):
"""
Set a value in the configuration.
Args:
section (str): Configuration section
option (str): Option name within section
value: Value to set
"""
global _config
if _config is None:
load_config()
if not _config.has_section(section):
_config.add_section(section)
_config.set(section, option, value)