-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsHandler.py
119 lines (101 loc) · 3.37 KB
/
SettingsHandler.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Copyright (C) 2022 Fern Lane, SonicEval (aka Pulsely) project
Licensed under the GNU Affero General Public License, Version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.en.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import json
import os
import AudioHandler
# Default app settings
SETTINGS_DEFAULT = {
'audio_playback_interface': '',
'audio_recording_interface': '',
'audio_recording_channels': 2,
'audio_sample_rate': 44100,
'signal_type': AudioHandler.TEST_SIGNAL_TYPE_SWEEP,
'signal_start_freq': 10,
'signal_stop_freq': 22000,
'signal_test_duration': 20,
'audio_playback_volume': 70,
'fft_size_chunks': 4,
'fft_window_type': AudioHandler.WINDOW_TYPE_HANNING,
'noise_filter_order': 5,
'noise_random_seed': 0,
'csv_separator': ',',
'normalize_frequency_response': True,
'normalize_reference': True,
'normalize_to_save': True
}
class SettingsHandler:
def __init__(self, filename: str):
"""
Initializes SettingsHandler class
:param filename:
"""
self.filename = filename
self.settings = {}
def read_from_file(self):
"""
Parses and checks settings from file
:return:
"""
try:
# Create new if file not exists
if not os.path.exists(self.filename):
self.settings = SETTINGS_DEFAULT
self.write_to_file()
# Open file
settings_file = open(self.filename, 'r')
# Parse JSON
try:
self.settings = json.load(settings_file)
except:
self.settings = SETTINGS_DEFAULT
self.write_to_file()
# Check settings
if not self.check_settings():
self.settings = SETTINGS_DEFAULT
self.write_to_file()
except Exception as e:
print(e)
def check_settings(self):
"""
Checks settings
:return:
"""
try:
default_keys = SETTINGS_DEFAULT.keys()
for key in default_keys:
if key not in self.settings:
return False
return True
except Exception as e:
print(e)
return False
def write_to_file(self):
"""
Writes settings to JSON file
:return:
"""
try:
# Open file for writing
settings_file = open(self.filename, 'w')
# Check if file is writable
if settings_file.writable():
# Write json to file
json.dump(self.settings, settings_file, indent=4)
else:
settings_file.close()
except Exception as e:
print(e)