-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.py
387 lines (341 loc) · 13.7 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
###############################################################################
###############################################################################
## ##
## THATCHORD BY TOM CONTI-LESLIE settings.py ##
## ##
## This file defines a function which takes in a settings file location ##
## and potential manual settings assignments, then combines these settings ##
## and overwrites manual assignments with any presets that are defined. ##
## Note that manual assignments in the function's variables overwrite ##
## assignments in the settings file. ##
## ##
## ##
## License: CC BY-SA 4.0 ##
## ##
## Contact: tom (dot) contileslie (at) gmail (dot) com ##
## ##
###############################################################################
###############################################################################
def get_settings(settingsfile = "settings.yml",
# NB.: settingsfile is relative to ThatChord directory.
instrument_preset = None,
ranking_preset = None,
tuning = None,
nfrets = None,
nmute = None,
important = None,
order = None,
left = None,
stringstarts = None,
ranks = None,
input_type = None,
output_format = None,
output_method = None,
save_method = None,
save_loc = None,
height = None,
margin = None,
head = None,
string = None,
press = None,
muted = None,
top = None,
):
# firstly, put the manual assignments aside.
xinstrument_preset = instrument_preset
xranking_preset = ranking_preset
xtuning = tuning
xnfrets = nfrets
xnmute = nmute
ximportant = important
xorder = order
xleft = left
xstringstarts = stringstarts
xranks = ranks
xinput_type = input_type
xoutput_format = output_format
xoutput_method = output_method
xsave_method = save_method
xsave_loc = save_loc
xheight = height
xmargin = margin
xhead = head
xstring = string
xpress = press
xmuted = muted
xtop = top
# PRIORITY LEVEL 1: default values for all variables.
instrument_preset = "UKULELE"
tuning = [7, 0, 4, 9]
nfrets = 12
nmute = 0
important = 4
order = [2, 0, 1, 3]
left = False
stringstarts = [0, 0, 0, 0]
ranking_preset = "UKULELE"
ranks = [1, 2, 3, 1, 0, 0, 0, 0, 0]
input_type = "TERMINAL"
output_format = "PNG"
output_method = "SPLASH"
save_method = "SINGLE"
save_loc = "~/Documents/ThatChord/diagrams"
height = 5
margin = 3
head = "="
string = "|"
press = "O"
muted = "x"
top = True
# PRIORITY LEVEL 2: overwrite default values with settings loaded from
# settings.yml.
import yaml
import os
from errors import err
import custom
script_directory = os.path.dirname(os.path.realpath(__file__))
settings_path = os.path.join(script_directory, settingsfile)
try:
with open(settings_path, "r") as file:
content = yaml.load(file, Loader=yaml.FullLoader)
keys = content.keys()
if "presets" in keys:
for d in content["presets"]:
if "instrument" in d.keys():
instrument_preset = list(d.values())[0].upper()
if "ranking" in d.keys():
ranking_preset = list(d.values())[0].upper()
else:
err(20)
if "input" in keys:
for d in content["input"]:
if "how" in d.keys():
input_type = list(d.values())[0].upper()
else:
err(20)
if "output" in keys:
for d in content["output"]:
if "how" in d.keys():
output_method = list(d.values())[0].upper()
if "format" in d.keys():
output_format = list(d.values())[0].upper()
else:
err(20)
if "saving" in keys:
for d in content["saving"]:
if "method" in d.keys():
save_method = list(d.values())[0].upper()
if "location" in d.keys():
save_loc = list(d.values())[0]
else:
err(20)
if "custom_instrument" in keys:
for d in content["custom_instrument"]:
if "tuning" in d.keys():
tuning = custom.interpret(list(d.values())[0],
remove_duplicates = False)
if "nfrets" in d.keys():
nfrets = list(d.values())[0]
if "nmute" in d.keys():
nmute = list(d.values())[0]
if "important" in d.keys():
important = list(d.values())[0]
if "order" in d.keys():
order = list(d.values())[0].copy()
if "handedness" in d.keys():
handedness = list(d.values())[0].upper()
if handedness in ["LEFT", "RIGHT"]:
left = {"LEFT" : True, "RIGHT" : False}[handedness]
if "stringstarts" in d.keys():
stringstarts = list(d.values())[0].copy()
else:
err(20)
if "custom_ranking" in keys:
for d in content["custom_ranking"]:
if "ranks" in d.keys():
ranks = list(d.values())[0].copy()
else:
err(20)
if "graphical_parameters" in keys:
for d in content["graphical_parameters"]:
if "height" in d.keys():
height = list(d.values())[0]
if "margin" in d.keys():
margin = list(d.values())[0]
if "head" in d.keys():
head = list(d.values())[0]
if "string" in d.keys():
string = list(d.values())[0]
if "press" in d.keys():
press = list(d.values())[0]
if "muted" in d.keys():
muted = list(d.values())[0]
if "title_at_top" in d.keys():
top = list(d.values())[0]
else:
err(20)
except FileNotFoundError:
err(19)
# PRIORITY LEVEL 3: MANUAL ASSIGNMENTS.
if xinstrument_preset:
instrument_preset = xinstrument_preset.upper()
if xranking_preset:
ranking_preset = xranking_preset.upper()
if xtuning:
tuning = xtuning
if xnfrets:
nfrets = xnfrets
if xnmute:
nmute = xnmute
if ximportant:
important = ximportant
if xorder:
order = xorder
if xleft:
left = xleft
if xstringstarts:
stringstarts = xstringstarts
if xranks:
ranks = xranks
if xinput_type:
input_type = xinput_type.upper()
if xoutput_format:
output_format = xoutput_format.upper()
if xoutput_method:
output_method = xoutput_method.upper()
if xsave_method:
save_method = xsave_method.upper()
if xsave_loc:
save_loc = xsave_loc
if xheight:
height = xheight
if xmargin:
margin = xmargin
if xhead:
head = xhead
if xstring:
string = xstring
if xpress:
press = xpress
if xmuted:
muted = xmuted
if xtop:
top = xtop
# APPLY PRESETS: firstly remove -L tag
if instrument_preset[-2:] == "-L":
left = True
instrument_preset = instrument_preset[0:-2]
# APPLY PRESETS: check for a file in presets/instruments.
script_directory = os.path.dirname(os.path.realpath(__file__))
preset_path = os.path.join(script_directory,
"presets/instruments/" + instrument_preset + ".yml")
ymldict = {}
try:
with open(preset_path, "r") as file:
ymldict = yaml.load(file, Loader=yaml.FullLoader)
except FileNotFoundError:
# if there is no file with correct name, see if index can redirect
index_path = os.path.join(script_directory,
"presets/instruments/_index.yml")
with open(index_path, "r") as file:
index = yaml.load(file, Loader=yaml.FullLoader)
if instrument_preset in index.keys():
preset_path = os.path.join(script_directory,
("presets/instruments/" +
index[instrument_preset] +
".yml"))
try:
with open(preset_path, "r") as file:
ymldict = yaml.load(file, Loader=yaml.FullLoader)
except FileNotFoundError:
pass
# if a settings file was found, its contents are now in ymldict.
keys = ymldict.keys()
if "tuning" in keys:
tuning = custom.interpret(ymldict["tuning"],
remove_duplicates = False)
if "nfrets" in keys:
nfrets = ymldict["nfrets"]
if "nmute" in keys:
nmute = ymldict["nmute"]
if "important" in keys:
important = ymldict["important"]
if "order" in keys:
order = ymldict["order"]
if "stringstarts" in keys:
stringstarts = ymldict["stringstarts"]
if "height" in keys:
height = ymldict["height"]
if "margin" in keys:
margin = ymldict["margin"]
if "head" in keys:
head = ymldict["head"]
if "string" in keys:
string = ymldict["string"]
if "press" in keys:
press = ymldict["press"]
if "muted" in keys:
muted = ymldict["muted"]
if "top" in keys:
top = ymldict["top"]
# DEFINE RANKING PRESETS HERE
if ranking_preset in ["UKULELE"]:
ranks = [1, 2, 3, 1, 0, 0, 0, 0, 0]
if ranking_preset in ["GUITAR"]:
ranks = [3, 0, 3, 1, 0, 5, 2, 5, 8]
if ranking_preset in ["BANJO"]:
ranks = [2, 1, 3, 0, 1, 0, 3, 2, 1]
if ranking_preset in ["MANDOLIN"]:
ranks = [1, 2, 3, 1, 0, 0, 0, 0, 0]
# SENSE CHECKING FOR I/O FORMATS
if not input_type in ["DIRECT", "TERMINAL", "CONSOLE"]:
err("input type")
if not output_format in ["TEXT", "PNG"]:
err("output format")
if not output_method in ["PRINT", "SPLASH", "NONE"]:
err("output method")
if not save_method in ["SINGLE", "LIBRARY", "NONE"]:
err("save method")
if output_method == "PRINT" and not output_format == "TEXT":
err("incompatible output")
if len(tuning) != len(order):
err(17)
if len(stringstarts) < len(tuning):
err(18)
# CHANGE TUNING TO IMAGINED NECK, FOR STRINGS STARTING HIGHER THAN 0TH FRET
for i in range(len(tuning)):
tuning[i] = (tuning[i] - stringstarts[i]) % 12
# MAKE GRAPHICAL PARAMETERS DICTIONARY - for output functions
kwgrargs = {
"height" : height,
"margin" : margin,
"head" : head,
"string" : string,
"press" : press,
"muted" : muted,
"left" : left,
"top" : top,
"stringstarts" : stringstarts,
}
# MAKE INPUT/OUTPUT PARAMETERS DICTIONARY - for output functions
kwioargs = {
"output_method" : output_method,
"save_method" : save_method,
"save_loc" : os.path.expanduser(save_loc)
}
# MAKE COMPLETE SETTINGS DICTIONARY - for use in main file
settings = {
"tuning" : tuning,
"nfrets" : nfrets,
"nmute" : nmute,
"important" : important,
"order" : order,
"left" : left,
"stringstarts" : stringstarts,
"ranks" : ranks,
"input_type" : input_type,
"output_format" : output_format,
"output_method" : output_method,
"save_method" : save_method,
"save_loc" : save_loc}
return settings, kwgrargs, kwioargs