def edit_name_callback(self, textinput, confirm): self.menu.pop() if confirm: preset = self.get_cursor_item() preset.name = textinput.get_text() preset.save() signals.emit('preset:changed', {'id': preset.id}) self.menu.message('Changes saved', popup=True, timeout=1)
def test_signal_reaches_page(menu): class TestPage(Page): state_events = ['test:blafoo'] menu.register_page('test', TestPage) menu.goto('test') signals.emit('test:blafoo', {'val': 'test123'})
def lock(self, message=None, goto_home=False): self._lock.acquire() if message is not None: signals.emit('state:locked', {'message': message}) try: yield finally: self._lock.release() if message is not None: signals.emit('state:unlocked', {'goto_home': goto_home})
def put(self, id): preset = get_object_or_404(Preset, Preset.id == id) data = request.get_json() errors = Preset.data_schema.validate(data) if errors: abort(400, errors=errors) preset.name = data.get('name') preset.set_data(data) preset.save() signals.emit('preset:changed', {'id': preset.id}) return self.get(id)
def delete(self, id): filepath = self.id_to_filepath(id) if not os.path.isfile(filepath): return abort(404) try: os.remove(filepath) except Exception as e: return abort(500, message=str(e)) with self.state.lock('Loading...', goto_home=True): signals.emit('sound:deleted', {'id': id}) return None, 204
def post(self): data = request.get_json() if not data: abort(400, message='Please supply a preset!') errors = Preset.data_schema.validate(data) if errors: abort(400, errors=errors) preset = Preset(name=data.get('name')) preset.set_data(data) preset.save(force_insert=True) signals.emit('preset:added', {'id': preset.id}) return preset.to_dict(), 201
def put(self): """ Does a partial update on the state, only changing things that are actually present in the submitted JSON data. Changes are not saved. """ data = request.get_json() errors = MiscSchema().validate(data) if errors: abort(400, errors=errors) self.state.from_misc_dict(data, partial=True) signals.emit('misc_config:updated') return self.get()
def post(self): """ Replaces the current state with the submitted data. If values are missing from the submitted JSON, it's set to the default value. """ data = request.get_json() errors = PresetSchema().validate(data) if errors: abort(400, errors=errors) with signals.suppress(): self.state.from_preset_dict(data, partial=False) signals.emit('active:preset:changed') return self.get()
def post(self): """ Replaces the current state with the submitted data. If values are missing from the submitted JSON, it's set to the default value. Changes are stored to the database. """ data = request.get_json() errors = MiscSchema().validate(data) if errors: abort(400, errors=errors) self.state.from_misc_dict(data, partial=False) db.save_misc_config(self.state.to_misc_dict()) signals.emit('misc_config:updated') return self.get()
def load_preset(self, preset_id, preload=False): try: preset = Preset.get(Preset.id == int(preset_id)) except Preset.DoesNotExist: log.error('Preset {} not found!'.format(preset_id)) return with self.lock(): with signals.suppress(): self.from_preset_dict(preset.get_data()) self.preset.id = preset_id if preload: signals.emit('active:preset:preload') signals.emit('active:preset:changed') self.last_preset_number = preset.number
def post(self, filename=None): filename = secure_filename(filename) if not re.search(r'\.sf[23]$', filename, re.I): abort( 400, message='Invalid file extension, please use .sf2 or .sf3 files' ) filepath = os.path.join(settings.sound_dir, filename) overwrite = os.path.isfile(filepath) tmp = tempfile.NamedTemporaryFile(delete=False, dir=settings.upload_dir) try: while True: chunk = request.stream.read(16 * 1024 * 1024) if len(chunk) == 0: break tmp.write(chunk) tmp.seek(0) try: SoundFont().parse_file(tmp) except Exception as e: raise UploadError( 'Invalid file format, is this really a SoundFont?') tmp.close() if os.path.isfile(filepath): os.unlink(filepath) shutil.move(tmp.name, filepath) except UploadError as e: try: os.unlink(tmp.name) except Exception: pass abort(400, message=str(e)) if overwrite: with self.state.lock('Loading...', goto_home=True): signals.emit('sound:changed', {'id': filename}) else: signals.emit('sound:added', {'id': filename}) return SoundFont(filepath).as_dict()
def save_preset(self, name=None, preset_id=None): if preset_id is not None: try: preset = Preset.get(Preset.id == int(preset_id)) except Preset.DoesNotExist: log.error('Preset {} not found!'.format(preset_id)) return if name is not None: preset.name = name else: preset = Preset() preset.name = name or 'Unnamed' preset.set_data(self.to_preset_dict()) preset.save() signals.emit('preset:changed', {'id': preset.id})
def post(self): raw_data = request.get_json() if not raw_data: abort(400, message='Please supply a preset!') data, errors = schema.ExportSchema().load(raw_data) if errors: abort(400, errors=errors) if opt_switch('presets') and data.get('presets', []): for preset in db.Preset.select(): signals.emit('preset:deleted', {'id': preset.id}) db.Preset.delete().execute() for preset_data in data['presets']: name = preset_data.pop('name', '') preset = db.Preset(name=name) preset.set_data(preset_data) preset.save(force_insert=True) signals.emit('preset:added', {'id': preset.id}) if opt_switch('mappings') and data.get('mappings', []): mapping_configs = mgcore.get_mapping_configs() for name in mapping_configs.keys(): db.delete_mapping_ranges(name) mgcore.reset_mapping_ranges(name) for mapping in data['mappings']: name = mapping['name'] if name in mapping_configs: db.save_mapping_ranges(name, mapping['ranges']) mgcore.set_mapping_ranges(name, mapping['ranges']) if opt_switch('calibration') and data.get('calibration', []): db.delete_key_calibration() calib = data['calibration'] if calib: db.save_key_calibration(calib) else: calib = calibration.default_keys() calibration.commit_keys(calib) if opt_switch('settings') and data.get('settings', []): misc = data['settings'] if misc: self.state.from_misc_dict(misc) db.save_misc_config(misc) return {'message': 'success'}
def update_port_states(self): if self.port_states is None: self.port_states = {} rawmidi_ports = [] for p in RawMIDI().get_ports(): # which internal MIDI port to use depends on the currently # selected UDC (USB gadget) configuration. if p.id.startswith('f_midi') and p.card_idx != self.udc_config: continue rawmidi_ports.append(p) ports = {p.id: p for p in rawmidi_ports} to_add = [pid for pid in ports if pid not in self.port_states] to_remove = [pid for pid in self.port_states if pid not in ports] for pid in to_remove: port_state = self.port_states.pop(pid, None) signals.emit('midi:port:removed', {'port_state': port_state}) for pid in to_add: port = ports[pid] port_state = MIDIPortState(port) self.port_states[pid] = port_state signals.emit('midi:port:added', {'port_state': port_state}) config = load_midi_config(port.id) if config: port_state.from_midi_dict(config) if to_add or to_remove: signals.emit('midi:changed')
def select_item(self, item): order = [item.id for item in self.items] self.state.preset.last_preset_number = 0 Preset.reorder(order) signals.emit('preset:reordered', {'order': order}) self.menu.message('Preset moved', popup=True, timeout=1)
def post(self): order = request.get_json()['order'] Preset.reorder(order) signals.emit('preset:reordered', {'order': order}) return {'order': [p.id for p in Preset.select(Preset.id)]}
def delete(self, id): preset = get_object_or_404(Preset, Preset.id == id) preset.delete_instance() signals.emit('preset:deleted', {'id': preset.id}) return None, 204
def unpreload_presets(self): signals.emit('clear:preload') self.presets_preloaded = False