Пример #1
0
def test_log_translation():

    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.enable_translation_logging(True)
    log.translation(['a', 'b'], ['c', 'd'], None)
    assert FakeHandler.outputs == {sf: ['*a', '*b', 'c', 'd']}
Пример #2
0
def test_log_translation():

    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.enable_translation_logging(True)
    log.translation(['a', 'b'], ['c', 'd'], None)
    assert FakeHandler.outputs == {sf: ['*a', '*b', 'c', 'd']}
Пример #3
0
def fake_file_log(monkeypatch):
    monkeypatch.setattr('plover.log.FileHandler', FakeHandler)
    yield
    FakeHandler.outputs.clear()
    # Reset logger state.
    log.set_stroke_filename(None)
    log.enable_stroke_logging(False)
    log.enable_translation_logging(False)
Пример #4
0
def fake_file_log(monkeypatch):
    monkeypatch.setattr('plover.log.FileHandler', FakeHandler)
    yield
    FakeHandler.outputs.clear()
    # Reset logger state.
    log.set_stroke_filename(None)
    log.enable_stroke_logging(False)
    log.enable_translation_logging(False)
Пример #5
0
def test_enable_stroke_logging():
    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.stroke(Stroke(('S-', )))
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('T-', )))
    log.enable_stroke_logging(False)
    log.stroke(Stroke(('K-', )))
    assert FakeHandler.outputs == {sf: ["Stroke(T : ['T-'])"]}
Пример #6
0
def test_enable_stroke_logging():
    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.stroke(Stroke(('S-',)))
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('T-',)))
    log.enable_stroke_logging(False)
    log.stroke(Stroke(('K-',)))
    assert FakeHandler.outputs == {sf: ["Stroke(T : ['T-'])"]}
Пример #7
0
def test_stroke():
    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('S-', '-T', 'T-')))
    log.stroke(Stroke(('#', 'S-', '-T')))
    assert FakeHandler.outputs == {
        sf:
        ["Stroke(ST-T : ['S-', 'T-', '-T'])", "Stroke(1-9 : ['1-', '-9'])"],
    }
Пример #8
0
def test_stroke():
    sf = stroke_filename('/fn')
    log.set_stroke_filename(sf)
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('S-', '-T', 'T-')))
    log.stroke(Stroke(('#', 'S-', '-T')))
    assert FakeHandler.outputs == {
        sf: ["Stroke(ST-T : ['S-', 'T-', '-T'])",
             "Stroke(1-9 : ['1-', '-9'])"],
    }
Пример #9
0
 def set_log_file_name(self, config):
     """Set the file name for log output."""
     filename = config.get_log_file_name()
     if not filename:
         return
     if os.path.realpath(filename) == 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)
         filename = conf.DEFAULT_LOG_FILE
         config.set_log_file_name(filename)
         with open(config.target_file, 'wb') as f:
             config.save(f)
     log.set_stroke_filename(filename)
Пример #10
0
 def set_log_file_name(self, config):
     """Set the file name for log output."""
     filename = config.get_log_file_name()
     if not filename:
         return
     if os.path.realpath(filename) == 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)
         filename = conf.DEFAULT_LOG_FILE
         config.set_log_file_name(filename)
         with open(config.target_file, 'wb') as f:
             config.save(f)
     log.set_stroke_filename(filename)
Пример #11
0
def test_set_filename():
    sf1 = stroke_filename('/fn1')
    log.set_stroke_filename('/fn1')
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('S-', )))
    sf2 = stroke_filename('/fn2')
    log.set_stroke_filename('/fn2')
    log.stroke(Stroke(('-T', )))
    log.set_stroke_filename(None)
    log.stroke(Stroke(('P-', )))
    assert FakeHandler.outputs == {
        sf1: ["Stroke(S : ['S-'])"],
        sf2: ["Stroke(-T : ['-T'])"],
    }
Пример #12
0
def test_set_filename():
    sf1 = stroke_filename('/fn1')
    log.set_stroke_filename('/fn1')
    log.enable_stroke_logging(True)
    log.stroke(Stroke(('S-',)))
    sf2 = stroke_filename('/fn2')
    log.set_stroke_filename('/fn2')
    log.stroke(Stroke(('-T',)))
    log.set_stroke_filename(None)
    log.stroke(Stroke(('P-',)))
    assert FakeHandler.outputs == {
        sf1: ["Stroke(S : ['S-'])"],
        sf2: ["Stroke(-T : ['-T'])"],
    }
Пример #13
0
 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 system.
     system_name = config['system_name']
     if system.NAME != system_name:
         log.info('loading system: %s', system_name)
         system.setup(system_name)
     # Update machine.
     update_keymap = False
     start_machine = False
     machine_params = MachineParams(config['machine_type'],
                                    config['machine_specific_options'],
                                    config['system_keymap'])
     # Do not reset if only the keymap changed.
     if self._machine_params is None or \
        self._machine_params.type != machine_params.type or \
        self._machine_params.options != machine_params.options:
         reset_machine = True
     if reset_machine:
         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 = registry.get_plugin('machine', machine_type).obj
         except Exception as e:
             raise InvalidConfigurationError(str(e))
         log.info('setting machine: %s', machine_type)
         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 running extensions.
     enabled_extensions = config['enabled_extensions']
     running_extensions = set(self._running_extensions)
     self._stop_extensions(running_extensions - enabled_extensions)
     self._start_extensions(enabled_extensions - running_extensions)
     # Trigger `config_changed` hook.
     if config_update:
         self._trigger_hook('config_changed', config_update)
     # Update dictionaries.
     config_dictionaries = OrderedDict(
         (d.path, d)
         for d in config['dictionaries']
     )
     copy_default_dictionaries(config_dictionaries.keys())
     # Start by unloading outdated dictionaries.
     self._dictionaries_manager.unload_outdated()
     self._set_dictionaries([
         d for d in self._dictionaries.dicts
         if d.path in config_dictionaries and \
            d.path in self._dictionaries_manager
     ])
     # And then (re)load all dictionaries.
     dictionaries = []
     for result in self._dictionaries_manager.load(config_dictionaries.keys()):
         if isinstance(result, DictionaryLoaderException):
             d = ErroredDictionary(result.path, result.exception)
             # Only show an error if it's new.
             if d != self._dictionaries.get(result.path):
                 log.error('loading dictionary `%s` failed: %s',
                           shorten_path(result.path), str(result.exception))
         else:
             d = result
         d.enabled = config_dictionaries[d.path].enabled
         dictionaries.append(d)
     self._set_dictionaries(dictionaries)
Пример #14
0
 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)
Пример #15
0
 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]
         }
     # 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 system.
     system_name = config['system_name']
     if system.NAME != system_name:
         log.info('loading system: %s', system_name)
         system.setup(system_name)
     # Update machine.
     update_keymap = False
     start_machine = False
     machine_params = MachineParams(config['machine_type'],
                                    config['machine_specific_options'],
                                    config['system_keymap'])
     # Do not reset if only the keymap changed.
     if self._machine_params is None or \
        self._machine_params.type != machine_params.type or \
        self._machine_params.options != machine_params.options:
         reset_machine = True
     if reset_machine:
         if self._machine is not None:
             self._machine.stop_capture()
             self._machine = None
         machine_type = config['machine_type']
         machine_options = config['machine_specific_options']
         machine_class = registry.get_plugin('machine', machine_type).obj
         log.info('setting machine: %s', machine_type)
         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 running extensions.
     enabled_extensions = config['enabled_extensions']
     running_extensions = set(self._running_extensions)
     self._stop_extensions(running_extensions - enabled_extensions)
     self._start_extensions(enabled_extensions - running_extensions)
     # Trigger `config_changed` hook.
     if config_update:
         self._trigger_hook('config_changed', config_update)
     # Update dictionaries.
     config_dictionaries = OrderedDict(
         (d.path, d) for d in config['dictionaries'])
     copy_default_dictionaries(config_dictionaries.keys())
     # Start by unloading outdated dictionaries.
     self._dictionaries_manager.unload_outdated()
     self._set_dictionaries([
         d for d in self._dictionaries.dicts
         if d.path in config_dictionaries and \
            d.path in self._dictionaries_manager
     ])
     # And then (re)load all dictionaries.
     dictionaries = []
     for result in self._dictionaries_manager.load(
             config_dictionaries.keys()):
         if isinstance(result, DictionaryLoaderException):
             d = ErroredDictionary(result.path, result.exception)
             # Only show an error if it's new.
             if d != self._dictionaries.get(result.path):
                 log.error('loading dictionary `%s` failed: %s',
                           shorten_path(result.path), str(result.exception))
         else:
             d = result
         d.enabled = config_dictionaries[d.path].enabled
         dictionaries.append(d)
     self._set_dictionaries(dictionaries)
Пример #16
0
 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 system.
     system_name = config["system_name"]
     if system.NAME != system_name:
         log.info("loading system: %s", system_name)
         system.setup(system_name)
     # 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 = registry.get_plugin("machine", machine_type).resolve()
         except Exception as e:
             raise InvalidConfigurationError(str(e))
         log.info("setting machine: %s", machine_type)
         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"]
     copy_default_dictionaries(dictionaries_files)
     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)
Пример #17
0
 def set_log_file_name(self, filename):
     """Set the file name for log output."""
     log.set_stroke_filename(filename)
Пример #18
0
 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)
Пример #19
0
 def set_log_file_name(self, filename):
     """Set the file name for log output."""
     log.set_stroke_filename(filename)