forked from a1ex4/ownfoil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
114 lines (101 loc) · 3.33 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
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
from constants import *
import yaml
import os, sys
sys.path.append(APP_DIR + '/NSTools/py')
from nstools.nut import Keys
import logging
# Retrieve main logger
logger = logging.getLogger('main')
def load_keys(key_file=KEYS_FILE):
valid = False
try:
if os.path.isfile(key_file):
valid = Keys.load(key_file)
return valid
else:
logger.debug(f'Keys file {key_file} does not exist.')
except:
logger.error(f'Provided keys file {key_file} is invalid.')
return valid
def load_settings():
if os.path.exists(CONFIG_FILE):
logger.debug('Reading configuration file.')
with open(CONFIG_FILE, 'r') as yaml_file:
settings = yaml.safe_load(yaml_file)
valid_keys = load_keys()
settings['titles']['valid_keys'] = valid_keys
else:
settings = DEFAULT_SETTINGS
with open(CONFIG_FILE, 'w') as yaml_file:
yaml.dump(settings, yaml_file)
return settings
def verify_settings(section, data):
success = True
errors = []
if section == 'library':
# Check that paths exist
for dir in data['paths']:
if not os.path.exists(dir):
success = False
errors.append({
'path': 'library/path',
'error': f"Path {dir} does not exists."
})
break
return success, errors
def add_library_path_to_settings(path):
success = True
errors = []
if not os.path.exists(path):
success = False
errors.append({
'path': 'library/paths',
'error': f"Path {path} does not exists."
})
else:
settings = load_settings()
library_paths = settings['library']['paths']
if library_paths:
if path in library_paths:
success = False
errors.append({
'path': 'library/paths',
'error': f"Path {path} already configured."
})
return success, errors
library_paths.append(path)
else:
library_paths = [path]
settings['library']['paths'] = library_paths
with open(CONFIG_FILE, 'w') as yaml_file:
yaml.dump(settings, yaml_file)
return success, errors
def delete_library_path_from_settings(path):
success = True
errors = []
settings = load_settings()
library_paths = settings['library']['paths']
if library_paths:
if path in library_paths:
library_paths.remove(path)
settings['library']['paths'] = library_paths
with open(CONFIG_FILE, 'w') as yaml_file:
yaml.dump(settings, yaml_file)
else:
success = False
errors.append({
'path': 'library/paths',
'error': f"Path {path} not configured."
})
return success, errors
def set_titles_settings(region, language):
settings = load_settings()
settings['titles']['region'] = region
settings['titles']['language'] = language
with open(CONFIG_FILE, 'w') as yaml_file:
yaml.dump(settings, yaml_file)
def set_shop_settings(data):
settings = load_settings()
settings['shop'] = data
with open(CONFIG_FILE, 'w') as yaml_file:
yaml.dump(settings, yaml_file)