aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-26 15:30:02 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-26 15:30:02 +0200
commit05d0d2ed0672aeb2e056c8af79bebde9c8b27199 (patch)
tree7841fbc4257316479759d519e0b9ea9ccca150c9
parente55b29bb38b845c7b9e65a1fbca0198882658e14 (diff)
downloadMusicSampler-05d0d2ed0672aeb2e056c8af79bebde9c8b27199.tar.gz
MusicSampler-05d0d2ed0672aeb2e056c8af79bebde9c8b27199.tar.zst
MusicSampler-05d0d2ed0672aeb2e056c8af79bebde9c8b27199.zip
Give usable errors when parsing configuration
-rw-r--r--helpers/__init__.py14
-rw-r--r--helpers/mapping.py200
2 files changed, 153 insertions, 61 deletions
diff --git a/helpers/__init__.py b/helpers/__init__.py
index f5ad848..534e168 100644
--- a/helpers/__init__.py
+++ b/helpers/__init__.py
@@ -86,7 +86,7 @@ def parse_args():
86 by Kivy. Pass \"-- --help\" to get Kivy's usage.") 86 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
87 87
88 from kivy.logger import Logger 88 from kivy.logger import Logger
89 Logger.setLevel(logging.ERROR) 89 Logger.setLevel(logging.WARN)
90 90
91 args = parser.parse_args(argv) 91 args = parser.parse_args(argv)
92 92
@@ -137,10 +137,14 @@ def gain(volume, old_volume=None):
137 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), 137 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)),
138 max(volume, 0)] 138 max(volume, 0)]
139 139
140def debug_print(message): 140def debug_print(message, with_trace=False):
141 from kivy.logger import Logger 141 from kivy.logger import Logger
142 Logger.debug('MusicSampler: ' + message) 142 Logger.debug('MusicSampler: ' + message, exc_info=with_trace)
143 143
144def error_print(message): 144def error_print(message, with_trace=False):
145 from kivy.logger import Logger 145 from kivy.logger import Logger
146 Logger.error('MusicSampler: ' + message) 146 Logger.error('MusicSampler: ' + message, exc_info=with_trace)
147
148def warn_print(message, with_trace=False):
149 from kivy.logger import Logger
150 Logger.warn('MusicSampler: ' + message, exc_info=with_trace)
diff --git a/helpers/mapping.py b/helpers/mapping.py
index ba2c340..1f63459 100644
--- a/helpers/mapping.py
+++ b/helpers/mapping.py
@@ -6,10 +6,11 @@ from kivy.clock import Clock
6import threading 6import threading
7import yaml 7import yaml
8import sys 8import sys
9from collections import defaultdict
9 10
10from .music_file import MusicFile 11from .music_file import MusicFile
11from .mixer import Mixer 12from .mixer import Mixer
12from . import Config, gain, error_print 13from . import Config, gain, error_print, warn_print
13from .action import Action 14from .action import Action
14 15
15class Mapping(RelativeLayout): 16class Mapping(RelativeLayout):
@@ -26,7 +27,8 @@ class Mapping(RelativeLayout):
26 try: 27 try:
27 self.key_config, self.open_files = self.parse_config() 28 self.key_config, self.open_files = self.parse_config()
28 except Exception as e: 29 except Exception as e:
29 error_print("Error while loading configuration: {}".format(e)) 30 error_print("Error while loading configuration: {}".format(e),
31 with_trace=True)
30 sys.exit() 32 sys.exit()
31 33
32 super(Mapping, self).__init__(**kwargs) 34 super(Mapping, self).__init__(**kwargs)
@@ -108,85 +110,171 @@ class Mapping(RelativeLayout):
108 self.running.remove((key, start_time)) 110 self.running.remove((key, start_time))
109 111
110 def parse_config(self): 112 def parse_config(self):
113 def update_alias(prop_hash, aliases, key):
114 if isinstance(aliases[key], dict):
115 prop_hash.update(aliases[key], **prop_hash)
116 else:
117 warn_print("Alias {} is not a hash, ignored".format(key))
118
119 def include_aliases(prop_hash, aliases):
120 if 'include' not in prop_hash:
121 return
122
123 included = prop_hash['include']
124 del(prop_hash['include'])
125 if isinstance(included, str):
126 update_alias(prop_hash, aliases, included)
127 elif isinstance(included, list):
128 for included_ in included:
129 if isinstance(included_, str):
130 update_alias(prop_hash, aliases, included_)
131 else:
132 warn_print("Unkown alias include type, ignored: "
133 "{} in {}".format(included_, included))
134 else:
135 warn_print("Unkown alias include type, ignored: {}"
136 .format(included))
137
138 def check_key_property(key_property, key):
139 if 'description' in key_property:
140 desc = key_property['description']
141 if not isinstance(desc, list):
142 warn_print("description in key_property '{}' is not "
143 "a list, ignored".format(key))
144 del(key_property['description'])
145 if 'color' in key_property:
146 color = key_property['color']
147 if not isinstance(color, list)\
148 or len(color) != 3\
149 or not all(isinstance(item, int) for item in color)\
150 or any(item < 0 or item > 255 for item in color):
151 warn_print("color in key_property '{}' is not "
152 "a list of 3 valid integers, ignored".format(key))
153 del(key_property['color'])
154
155 def check_key_properties(config):
156 if 'key_properties' in config:
157 if isinstance(config['key_properties'], dict):
158 return config['key_properties']
159 else:
160 warn_print("key_properties config is not a hash, ignored")
161 return {}
162 else:
163 return {}
164
165 def check_mapped_keys(config):
166 if 'keys' in config:
167 if isinstance(config['keys'], dict):
168 return config['keys']
169 else:
170 warn_print("keys config is not a hash, ignored")
171 return {}
172 else:
173 return {}
174
175 def check_mapped_key(mapped_keys, key):
176 if not isinstance(mapped_keys[key], list):
177 warn_print("key config '{}' is not an array, ignored"
178 .format(key))
179 return []
180 else:
181 return mapped_keys[key]
182
183 def check_music_property(music_property, filename):
184 if not isinstance(music_property, dict):
185 warn_print("music_property config '{}' is not a hash, ignored"
186 .format(filename))
187 return {}
188 if 'name' in music_property:
189 music_property['name'] = str(music_property['name'])
190 if 'gain' in music_property:
191 try:
192 music_property['gain'] = float(music_property['gain'])
193 except ValueError as e:
194 del(music_property['gain'])
195 warn_print("gain for music_property '{}' is not "
196 "a float, ignored".format(filename))
197 return music_property
198
111 stream = open(Config.yml_file, "r") 199 stream = open(Config.yml_file, "r")
112 try: 200 try:
113 config = yaml.load(stream) 201 config = yaml.safe_load(stream)
114 except Exception as e: 202 except Exception as e:
115 error_print("Error while loading config file: {}".format(e)) 203 error_print("Error while loading config file: {}".format(e))
116 sys.exit() 204 sys.exit()
117 stream.close() 205 stream.close()
118 206
119 aliases = config['aliases'] 207 if not isinstance(config, dict):
208 raise Exception("Top level config is supposed to be a hash")
209
210 if 'aliases' in config and isinstance(config['aliases'], dict):
211 aliases = config['aliases']
212 else:
213 aliases = defaultdict(dict)
214 if 'aliases' in config:
215 warn_print("aliases config is not a hash, ignored")
216
217 music_properties = defaultdict(dict)
218 if 'music_properties' in config and\
219 isinstance(config['music_properties'], dict):
220 music_properties.update(config['music_properties'])
221 elif 'music_properties' in config:
222 warn_print("music_properties config is not a hash, ignored")
223
120 seen_files = {} 224 seen_files = {}
121 225
122 key_properties = {} 226 key_properties = defaultdict(lambda: {
227 "actions": [],
228 "properties": {},
229 "files": []
230 })
123 231
124 for key in config['key_properties']: 232 for key in check_key_properties(config):
125 if key not in key_properties: 233 key_prop = config['key_properties'][key]
126 key_prop = config['key_properties'][key] 234
127 if 'include' in key_prop: 235 if not isinstance(key_prop, dict):
128 included = key_prop['include'] 236 warn_print("key_property '{}' is not a hash, ignored"
129 del(key_prop['include']) 237 .format(key))
238 continue
239
240 include_aliases(key_prop, aliases)
241 check_key_property(key_prop, key)
242
243 key_properties[key]["properties"] = key_prop
244
245 for mapped_key in check_mapped_keys(config):
246 for index, action in enumerate(check_mapped_key(
247 config['keys'], mapped_key)):
248 if not isinstance(action, dict) or\
249 not len(action) == 1 or\
250 not isinstance(list(action.values())[0] or {}, dict):
251 warn_print("action number {} of key '{}' is invalid, "
252 "ignored".format(index + 1, mapped_key))
253 continue
130 254
131 if isinstance(included, str):
132 key_prop.update(aliases[included], **key_prop)
133 else:
134 for included_ in included:
135 key_prop.update(aliases[included_], **key_prop)
136
137 key_properties[key] = {
138 "actions": [],
139 "properties": key_prop,
140 "files": []
141 }
142
143 for mapped_key in config['keys']:
144 if mapped_key not in key_properties:
145 key_properties[mapped_key] = {
146 "actions": [],
147 "properties": {},
148 "files": []
149 }
150 for action in config['keys'][mapped_key]:
151 action_name = list(action)[0] 255 action_name = list(action)[0]
152 action_args = {} 256 action_args = {}
153 if action[action_name] is None: 257 if action[action_name] is None:
154 action[action_name] = [] 258 action[action_name] = {}
155
156 if 'include' in action[action_name]:
157 included = action[action_name]['include']
158 del(action[action_name]['include'])
159 259
160 if isinstance(included, str): 260 include_aliases(action[action_name], aliases)
161 action[action_name].update(
162 aliases[included],
163 **action[action_name])
164 else:
165 for included_ in included:
166 action[action_name].update(
167 aliases[included_],
168 **action[action_name])
169 261
170 for argument in action[action_name]: 262 for argument in action[action_name]:
171 if argument == 'file': 263 if argument == 'file':
172 filename = action[action_name]['file'] 264 filename = str(action[action_name]['file'])
173 if filename not in seen_files: 265 if filename not in seen_files:
174 if filename in config['music_properties']: 266 music_property = check_music_property(
175 seen_files[filename] = MusicFile( 267 music_properties[filename],
176 filename, 268 filename)
177 self, 269
178 **config['music_properties'][filename]) 270 seen_files[filename] = MusicFile(
179 else: 271 filename, self, **music_property)
180 seen_files[filename] = MusicFile(
181 filename,
182 self)
183 272
184 if filename not in key_properties[mapped_key]['files']: 273 if filename not in key_properties[mapped_key]['files']:
185 key_properties[mapped_key]['files'] \ 274 key_properties[mapped_key]['files'] \
186 .append(seen_files[filename]) 275 .append(seen_files[filename])
187 276
188 action_args['music'] = seen_files[filename] 277 action_args['music'] = seen_files[filename]
189
190 else: 278 else:
191 action_args[argument] = action[action_name][argument] 279 action_args[argument] = action[action_name][argument]
192 280