1
+ import logging
1
2
import os .path
3
+ import sys
2
4
3
5
import numpy as np
4
6
import yaml
10
12
from yaml import CLoader as Loader , CDumper as Dumper
11
13
except ImportError :
12
14
from yaml import Loader , Dumper
15
+
13
16
try :
14
17
import dynamic
15
18
except ImportError :
16
19
print ("You don't seem to have dynamic.py near its main.py and preprocessing.py. correct your installation" )
17
- exit (1 )
20
+ sys .exit (1 )
21
+ try :
22
+ import sanity
23
+ except ImportError :
24
+ print ("You don't seem to have sanity.py near its main.py and preprocessing.py. correct your installation" )
25
+ sys .exit (1 )
18
26
19
27
ACTION_CFG = "actions.yaml"
20
28
ACTION_CFG_NAME = "actions"
@@ -53,27 +61,17 @@ def __init__(self, index, name, icon, cmd_type, label=None, dataref=None, datare
53
61
cmd_on = None , cmd_off = None , cmd_on_mul = None , cmd_off_mul = None ,
54
62
gauge = None , display = None , special_label = None ):
55
63
# Constants
56
- self .index = index
57
- if self .index is None :
58
- print ("ERROR: button with name {} has no set index, quitting..." .format (name ))
59
- quit (1 )
60
64
61
- self .name = name
62
- if self .name is None or self .name == "" :
63
- print ("ERROR: button with index {} has no set name, quitting..." .format (index ))
64
- quit (1 )
65
+ sanity .vital_check (index , name )
65
66
67
+ self .index = index
68
+ self .name = name
66
69
self .icon = icon
67
70
68
- self .cmd_type = cmd_type
69
- if cmd_type is None or cmd_type == "" :
70
- print ("WARN: {} has no set type, setting none as default (no press action)" .format (name ))
71
- self .cmd_type = "none"
71
+ self .cmd_type = sanity .cmd_check (index , name , cmd_type )
72
72
73
73
# verify string type
74
- if label :
75
- label = str (label )
76
- self .label = label
74
+ self .label = sanity .label_check (index , name , label )
77
75
78
76
self .dataref = dataref
79
77
if dataref_multiplier is None :
@@ -101,6 +99,10 @@ def __init__(self, index, name, icon, cmd_type, label=None, dataref=None, datare
101
99
else :
102
100
self .auto_switch = auto_switch
103
101
102
+ # generate warnings for weird commands
103
+ sanity .cmd2_check (index , name , cmd_type , cmd , cmd_mul , cmd_release , cmd_release_mul ,
104
+ cmd_on , cmd_off , cmd_on_mul , cmd_off_mul )
105
+
104
106
self .cmd = cmd
105
107
self .cmd_mul = cmd_mul
106
108
self .cmd_release = cmd_release
@@ -118,10 +120,16 @@ def __init__(self, index, name, icon, cmd_type, label=None, dataref=None, datare
118
120
self .display = None
119
121
self .special_label = None
120
122
if file_names is not None :
123
+ # sanity check file_names corresponding to dataref_states
124
+ sanity .file_names_check (index , name , file_names , self .dataref_states )
125
+
121
126
self .file_names = np .empty (len (file_names ), dtype = object )
122
127
for i , fn in enumerate (file_names ):
123
128
self .file_names [i ] = get_filename_button_static_png (fn )
124
129
elif gauge :
130
+ # sanity check for gauge (if it contains everything)
131
+ sanity .gauge_check (index , name , gauge )
132
+
125
133
# overwrite default dataref_states
126
134
self .dataref_states = dynamic .get_dataref_states (gauge )
127
135
# special case - gauge with needles :)
@@ -134,6 +142,9 @@ def __init__(self, index, name, icon, cmd_type, label=None, dataref=None, datare
134
142
# so we pregenerate them artificial names for the use in main global images dict
135
143
self .file_names = dynamic .create_dynamic_filenames (self .gauge ["name" ], self .dataref_states )
136
144
elif display :
145
+ # sanity check for display (if it contains everything)
146
+ sanity .display_check (index , name , display )
147
+
137
148
# overwrite default dataref_states
138
149
self .dataref_states = dynamic .get_dataref_states (display )
139
150
# special case - display of number values
@@ -150,14 +161,21 @@ def __init__(self, index, name, icon, cmd_type, label=None, dataref=None, datare
150
161
# todo
151
162
pass
152
163
elif self .dataref_states is not None :
164
+ if icon is None :
165
+ logging .error ("#{} {} is trying to set dataref_states without the 'icon' parameter, quitting..."
166
+ .format (index , name ))
167
+ sys .exit (1 )
168
+
153
169
self .file_names = np .empty (len (self .dataref_states ), dtype = object )
154
170
for i , state in enumerate (self .dataref_states ):
155
171
self .file_names [i ] = get_filename_button_dataref_png (icon , state )
156
172
else :
157
- self .file_names = np .empty (1 , dtype = object )
158
173
if icon is None :
159
- print ("static icon is not present on {} button" .format (name ))
160
- exit (1 )
174
+ logging .error ("#{} {} is trying to set static icon without the 'icon' parameter, quitting..."
175
+ .format (index , name ))
176
+ sys .exit (1 )
177
+
178
+ self .file_names = np .empty (1 , dtype = object )
161
179
self .file_names [0 ] = get_filename_button_static_png (icon )
162
180
163
181
@@ -167,7 +185,7 @@ def load_preset(target_dir, yaml_keyset, deck_key_count, preload_labels=False):
167
185
preset_cfg = safe_load (stream )
168
186
except yaml .YAMLError as err :
169
187
print ("cannot load {}, ensure you have proper syntax config {}" .format (yaml_keyset , err ))
170
- exit (1 )
188
+ sys . exit (1 )
171
189
172
190
keys = preset_cfg ["actions" ]
173
191
# key_count = len(keys)
@@ -178,6 +196,12 @@ def load_preset(target_dir, yaml_keyset, deck_key_count, preload_labels=False):
178
196
for _ , key in enumerate (keys ):
179
197
index = key .get ("index" )
180
198
name = key .get ("name" )
199
+ # try to convert to int because it is used as array index
200
+ try :
201
+ index = int (index )
202
+ except ValueError :
203
+ logging .error ("button with name {} has index non-convertable to integer, quitting..." .format (name ))
204
+ sys .exit (1 )
181
205
cmd_type = key .get ("type" )
182
206
preset [index ] = Button (
183
207
index ,
0 commit comments