def get_machine_type(self): machine_type = self._get(MACHINE_CONFIG_SECTION, MACHINE_TYPE_OPTION, None) if machine_type is not None: try: machine_registry.get(machine_type) except NoSuchMachineException: log.error("invalid machine type: %s", machine_type) self.set_machine_type(DEFAULT_MACHINE_TYPE) machine_type = None if machine_type is None: machine_type = DEFAULT_MACHINE_TYPE return machine_type
def update_engine(engine, old, new): """Modify a StenoEngine using a before and after config object. Using the before and after allows this function to not make unnecessary changes. """ machine_type = new.get_machine_type() machine_options = new.get_machine_specific_options(machine_type) if (old.get_machine_type() != machine_type or old.get_machine_specific_options(machine_type) != machine_options): try: machine_class = machine_registry.get(machine_type) except NoSuchMachineException as e: raise InvalidConfigurationError(unicode(e)) engine.set_machine(machine_class(machine_options)) dictionary_file_names = new.get_dictionary_file_names() if old.get_dictionary_file_names() != dictionary_file_names: try: dicts = dict_manager.load(dictionary_file_names) except DictionaryLoaderException as e: raise InvalidConfigurationError(unicode(e)) engine.get_dictionary().set_dicts(dicts) log_file_name = new.get_log_file_name() if old.get_log_file_name() != log_file_name: engine.set_log_file_name(log_file_name) enable_stroke_logging = new.get_enable_stroke_logging() if old.get_enable_stroke_logging() != enable_stroke_logging: engine.enable_stroke_logging(enable_stroke_logging) enable_translation_logging = new.get_enable_translation_logging() if old.get_enable_translation_logging() != enable_translation_logging: engine.enable_translation_logging(enable_translation_logging)
def get_system_keymap(self, machine_type=None): if machine_type is None: machine_type = self.get_machine_type() try: machine_class = machine_registry.get(machine_type) except: log.error("invalid machine type: %s", machine_type, exc_info=True) return None section = SYSTEM_CONFIG_SECTION % DEFAULT_SYSTEM option = SYSTEM_KEYMAP_OPTION % machine_type mappings = self._get(section, option, None) if mappings is None: mappings = system.KEYMAPS.get(machine_type) else: try: mappings = dict(json.loads(mappings)) except ValueError as e: log.error("invalid machine keymap, resetting to default", exc_info=True) mappings = system.KEYMAPS.get(machine_type) self.set_system_keymap(mappings, machine_type) keymap = Keymap(machine_class.get_keys(), system.KEYS + machine_class.get_actions()) keymap.set_mappings(mappings) return keymap
def reset_machine(engine, config): """Set the machine on the engine based on config.""" machine_type = config.get_machine_type() machine_options = config.get_machine_specific_options(machine_type) try: instance = machine_registry.get(machine_type)(machine_options) except NoSuchMachineException as e: raise InvalidConfigurationError(unicode(e)) engine.set_machine(instance)
def get_machine_specific_options(self, machine_name): machine = machine_registry.get(machine_name) option_info = machine.get_option_info() if self._config.has_section(machine_name): options = dict((o, self._config.get(machine_name, o)) for o in self._config.options(machine_name)) return dict((k, option_info[k][1](v)) for k, v in options.items() if k in option_info) return dict((k, v[0]) for k, v in option_info.items())
def update_engine(engine, config, reset_machine=False): """Modify a StenoEngine using a before and after config object. Using the before and after allows this function to not make unnecessary changes. """ if reset_machine: engine.set_machine(None) machine_type = config.get_machine_type() try: machine_class = machine_registry.get(machine_type) except NoSuchMachineException as e: raise InvalidConfigurationError(unicode(e)) machine_options = config.get_machine_specific_options(machine_type) machine_mappings = config.get_system_keymap(machine_type) engine.set_machine(machine_class, machine_options, machine_mappings) dictionary_file_names = config.get_dictionary_file_names() engine.set_dictionaries(dictionary_file_names) log_file_name = config.get_log_file_name() if log_file_name: # Older versions would use "plover.log" for logging strokes. if os.path.realpath(log_file_name) == os.path.realpath(log.LOG_FILENAME): log.warning('stroke logging must use a different file than %s, ' 'renaming to %s', log.LOG_FILENAME, conf.DEFAULT_LOG_FILE) log_file_name = conf.DEFAULT_LOG_FILE config.set_log_file_name(log_file_name) with open(config.target_file, 'wb') as f: config.save(f) engine.set_log_file_name(log_file_name) enable_stroke_logging = config.get_enable_stroke_logging() engine.enable_stroke_logging(enable_stroke_logging) enable_translation_logging = config.get_enable_translation_logging() engine.enable_translation_logging(enable_translation_logging) space_placement = config.get_space_placement() engine.set_space_placement(space_placement) undo_levels = config.get_undo_levels() engine.set_undo_levels(undo_levels) start_capitalized = config.get_start_capitalized() start_attached = config.get_start_attached() engine.set_starting_stroke_state(attach=start_attached, capitalize=start_capitalized)
def get_machine_specific_options(self, machine_name): def convert(p, v): try: return p[1](v) except ValueError: return p[0] machine = machine_registry.get(machine_name) info = machine.get_option_info() defaults = {k: v[0] for k, v in info.items()} if self._config.has_section(machine_name): options = {o: self._config.get(machine_name, o) for o in self._config.options(machine_name) if o in info} options = {k: convert(info[k], v) for k, v in options.items()} defaults.update(options) return defaults
def update_engine(engine, config, reset_machine=False): """Modify a StenoEngine using a before and after config object. Using the before and after allows this function to not make unnecessary changes. """ machine_type = config.get_machine_type() try: machine_class = machine_registry.get(machine_type) except NoSuchMachineException as e: raise InvalidConfigurationError(str(e)) machine_options = config.get_machine_specific_options(machine_type) machine_mappings = config.get_system_keymap(machine_type) engine.set_machine(machine_class, machine_options, machine_mappings, reset_machine=reset_machine) dictionary_file_names = config.get_dictionary_file_names() engine.set_dictionaries(dictionary_file_names) log_file_name = config.get_log_file_name() if log_file_name: # Older versions would use "plover.log" for logging strokes. if os.path.realpath(log_file_name) == os.path.realpath(log.LOG_FILENAME): log.warning('stroke logging must use a different file than %s, ' 'renaming to %s', log.LOG_FILENAME, conf.DEFAULT_LOG_FILE) log_file_name = conf.DEFAULT_LOG_FILE config.set_log_file_name(log_file_name) with open(config.target_file, 'wb') as f: config.save(f) engine.set_log_file_name(log_file_name) enable_stroke_logging = config.get_enable_stroke_logging() engine.enable_stroke_logging(enable_stroke_logging) enable_translation_logging = config.get_enable_translation_logging() engine.enable_translation_logging(enable_translation_logging) space_placement = config.get_space_placement() engine.set_space_placement(space_placement) undo_levels = config.get_undo_levels() engine.set_undo_levels(undo_levels) start_capitalized = config.get_start_capitalized() start_attached = config.get_start_attached() engine.set_starting_stroke_state(attach=start_attached, capitalize=start_capitalized)
def test_sidewinder(self): self.assertEqual(machine_registery.get("NKRO Keyboard"), machine_registry.get('Microsoft Sidewinder X4'))
def test_unknown_machine(self): with self.assertRaises(NoSuchMachineException): machine_registry.get('No such machine')
def test_keyboard_as_nkro(self): self.assertEqual(machine_registry.get("Keyboard"), machine_registry.get('NKRO Keyboard'))
def test_keyboard_as_sidewinder(self): self.assertEqual(machine_registry.get("Keyboard"), machine_registry.get('Microsoft Sidewinder X4'))
def _update(self, config_update=None, full=False, reset_machine=False): original_config = self._config.as_dict() # Update configuration. if config_update is not None: self._config.update(**config_update) config = self._config.as_dict() else: config = original_config # Create configuration update. if full: config_update = config else: config_update = { option: value for option, value in config.items() if value != original_config[option] } if 'machine_type' in config_update: for opt in ( 'machine_specific_options', 'system_keymap', ): config_update[opt] = config[opt] # Update logging. log.set_stroke_filename(config['log_file_name']) log.enable_stroke_logging(config['enable_stroke_logging']) log.enable_translation_logging(config['enable_translation_logging']) # Update output. self._formatter.set_space_placement(config['space_placement']) self._formatter.start_attached = config['start_attached'] self._formatter.start_capitalized = config['start_capitalized'] self._translator.set_min_undo_length(config['undo_levels']) # Update machine. update_keymap = False start_machine = False machine_params = MachineParams(config['machine_type'], config['machine_specific_options'], config['system_keymap']) if reset_machine or machine_params != self._machine_params: if self._machine is not None: self._machine.stop_capture() self._machine = None machine_type = config['machine_type'] machine_options = config['machine_specific_options'] try: machine_class = machine_registry.get(machine_type) except NoSuchMachineException as e: raise InvalidConfigurationError(str(e)) self._machine = machine_class(machine_options) self._machine.set_suppression(self._is_running) self._machine.add_state_callback(self._machine_state_callback) self._machine.add_stroke_callback(self._machine_stroke_callback) self._machine_params = machine_params update_keymap = True start_machine = True elif self._machine is not None: update_keymap = 'system_keymap' in config_update if update_keymap: machine_keymap = config['system_keymap'] if machine_keymap is not None: self._machine.set_keymap(machine_keymap) if start_machine: self._machine.start_capture() # Update dictionaries. dictionaries_files = config['dictionary_file_names'] dictionaries = self._dictionaries_manager.load(dictionaries_files) self._dictionaries.set_dicts(dictionaries) # Trigger `config_changed` hook. if config_update: self._trigger_hook('config_changed', config_update)