예제 #1
0
파일: app.py 프로젝트: KoiOates/plover
 def _translate_stroke(self, s):
     stroke = steno.Stroke(s)
     self.translator.translate(stroke)
     for listener in self.stroke_listeners:
         listener(stroke)
예제 #2
0
파일: app.py 프로젝트: gcr/plover
    def __init__(self, engine_command_callback):
        """Creates and configures a single steno pipeline."""
        self.subscribers = []
        self.is_running = False
        self.machine = None
        self.machine_init = {}
        self.translator = None
        self.formatter = None
        self.output = None

        # Check and use configuration
        self.config = conf.get_config()
        config_errors, config_values = check_steno_config(self.config)
        for error in config_errors:
            # This will raise one of the configuration errors.
            raise error

        machine_type, user_dictionary = config_values

        # Set the machine module and any initialization variables.
        self.machine_module = conf.import_named_module(
            machine_type, SUPPORTED_MACHINES_DICT)
        if self.machine_module is None:
            raise InvalidConfigurationError(
                'Invalid configuration value for %s: %s' %
                (conf.MACHINE_TYPE_OPTION, machine_type))

        if issubclass(self.machine_module.Stenotype,
                      plover.machine.base.SerialStenotypeBase):
            serial_params = conf.get_serial_params(machine_type, self.config)
            self.machine_init.update(serial_params.__dict__)

        # Initialize the logger.
        log_file = join(
            conf.CONFIG_DIR,
            self.config.get(conf.LOGGING_CONFIG_SECTION, conf.LOG_FILE_OPTION))
        self.logger = logging.getLogger(conf.LOGGER_NAME)
        self.logger.setLevel(logging.DEBUG)
        handler = RotatingFileHandler(
            log_file,
            maxBytes=conf.LOG_MAX_BYTES,
            backupCount=conf.LOG_COUNT,
        )
        handler.setFormatter(logging.Formatter(conf.LOG_FORMAT))
        self.logger.addHandler(handler)

        # Construct the stenography capture-translate-format-display pipeline.
        self.machine = self.machine_module.Stenotype(**self.machine_init)
        self.translator = translation.Translator()
        self.translator.set_dictionary(user_dictionary)
        self.formatter = formatting.Formatter()

        # Add hooks for logging. Do this first so logs appear in order.
        if self.config.getboolean(conf.LOGGING_CONFIG_SECTION,
                                  conf.ENABLE_STROKE_LOGGING_OPTION):
            self.machine.add_callback(self._log_stroke)
        if self.config.getboolean(conf.LOGGING_CONFIG_SECTION,
                                  conf.ENABLE_TRANSLATION_LOGGING_OPTION):
            self.translator.add_listener(self._log_translation)

        self.machine.add_callback(
            lambda x: self.translator.translate(steno.Stroke(x)))
        self.translator.add_listener(self.formatter.format)
        # This seems like a reasonable number. If this becomes a problem it can
        # be parameterized.
        self.translator.set_min_undo_length(10)
        keyboard_control = keyboardcontrol.KeyboardEmulation()
        bag = SimpleNamespace()
        bag.send_backspaces = keyboard_control.send_backspaces
        bag.send_string = keyboard_control.send_string
        bag.send_key_combination = keyboard_control.send_key_combination
        bag.send_engine_command = engine_command_callback
        self.full_output = bag
        bag = SimpleNamespace()
        bag.send_engine_command = engine_command_callback
        self.command_only_output = bag
        self.running_state = self.translator.get_state()

        auto_start = self.config.getboolean(conf.MACHINE_CONFIG_SECTION,
                                            conf.MACHINE_AUTO_START_OPTION)
        self.set_is_running(auto_start)

        # Start the machine monitoring for steno strokes.
        self.machine.start_capture()
예제 #3
0
 def _translator_machine_callback(self, s):
     stroke = steno.Stroke(s)
     self.translator.translate(stroke)
     for listener in self.stroke_listeners:
         listener(stroke)