forked from jairam0/shriram
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsettings.py
165 lines (144 loc) · 4.6 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import configparser
import errno
import json
import os
from collections import defaultdict
from pathlib import Path
DEFAULT_CONFIGDIR = Path(os.path.expanduser('~/.config/nuxhash'))
SETTINGS_FILENAME = 'settings.conf'
BENCHMARKS_FILENAME = 'benchmarks.json'
DEFAULT_SETTINGS = {
'nicehash': {
'wallet': '3CJFb2B66m7UoJAVjB6otBQYQ2UFnAXhuf',
'workername': 'nuxhash',
'region': 'eu',
'api_organization': '',
'api_key': '',
'api_secret': ''
},
'switching': {
'interval': 60,
'threshold': 0.1
},
'gui': {
'units': 'mBTC'
},
'donate': {
'optout': False
},
'excavator_miner': {
'listen': '',
'args': ''
}
}
EMPTY_BENCHMARKS = defaultdict(lambda: {})
def read_settings_from_file(fd):
parser = configparser.ConfigParser()
parser.read_file(fd)
methods = {
'nicehash': {
'wallet': parser.get,
'workername': parser.get,
'region': parser.get,
'api_organization': parser.get,
'api_key': parser.get,
'api_secret': parser.get
},
'switching': {
'interval': parser.getint,
'threshold': parser.getfloat
},
'gui': {
'units': parser.get
},
'donate': {
'optout': parser.getboolean
},
'excavator_miner': {
'listen': parser.get,
'args': parser.get
}
}
def read_options(data, *sections):
if isinstance(data, dict):
return {key: read_options(item, *(sections + (key,)))
for key, item in data.items()}
elif callable(data):
try:
return data(*sections)
except (configparser.NoSectionError, configparser.NoOptionError):
value = DEFAULT_SETTINGS
for key in sections:
value = value[key]
return value
else:
raise ValueError
return read_options(methods)
def write_settings_to_file(fd, settings):
parser = configparser.ConfigParser()
for section in settings:
parser.add_section(section)
for option in settings[section]:
value = settings[section][option]
parser.set(section, option, str(value))
parser.write(fd)
def read_benchmarks_from_file(fd, devices):
benchmarks = defaultdict(lambda: {})
js = json.load(fd)
for js_device in js:
device = next((device for device in devices
if str(device) == js_device), None)
if device is None:
continue
js_speeds = js[js_device]
for algorithm_name in js_speeds:
if isinstance(js_speeds[algorithm_name], list):
benchmarks[device][algorithm_name] = js_speeds[algorithm_name]
else:
benchmarks[device][algorithm_name] = [js_speeds[algorithm_name]]
return benchmarks
def write_benchmarks_to_file(fd, benchmarks):
to_file = {}
for device in benchmarks:
to_file[str(device)] = {}
speeds = benchmarks[device]
for algorithm_name in speeds:
if len(speeds[algorithm_name]) == 1:
to_file[str(device)][algorithm_name] = speeds[algorithm_name][0]
else:
to_file[str(device)][algorithm_name] = speeds[algorithm_name]
json.dump(to_file, fd, indent=4)
def load_settings(config_dir):
try:
with open(config_dir/SETTINGS_FILENAME, 'r') as settings_fd:
settings = read_settings_from_file(settings_fd)
except IOError as err:
if err.errno != errno.ENOENT:
raise
return DEFAULT_SETTINGS
else:
return settings
def load_benchmarks(config_dir, devices):
try:
with open(config_dir/BENCHMARKS_FILENAME, 'r') as benchmarks_fd:
benchmarks = read_benchmarks_from_file(benchmarks_fd, devices)
except IOError as err:
if err.errno != errno.ENOENT:
raise
return EMPTY_BENCHMARKS
else:
return benchmarks
def save_settings(config_dir, settings):
_mkdir(config_dir)
with open(config_dir/SETTINGS_FILENAME, 'w') as settings_fd:
write_settings_to_file(settings_fd, settings)
def save_benchmarks(config_dir, benchmarks):
_mkdir(config_dir)
with open(config_dir/BENCHMARKS_FILENAME, 'w') as benchmarks_fd:
write_benchmarks_to_file(benchmarks_fd, benchmarks)
def _mkdir(d):
try:
os.makedirs(d)
except OSError:
if not os.path.isdir(d):
raise