def _compute_kaldi_rules_activity(self, phrase_start=True): window_info = {} if phrase_start: fg_window = Window.get_foreground() window_info = { "executable": fg_window.executable, "title": fg_window.title, "handle": fg_window.handle, } for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically( ): grammar_wrapper.phrase_start_callback(**window_info) self.prepare_for_recognition() self._active_kaldi_rules = set() self._kaldi_rules_activity = [False] * self._compiler.num_kaldi_rules for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically(): if grammar_wrapper.active and (not self._any_exclusive_grammars or grammar_wrapper.exclusive): for kaldi_rule in grammar_wrapper.kaldi_rule_by_rule_dict.values( ): if kaldi_rule.active: self._active_kaldi_rules.add(kaldi_rule) self._kaldi_rules_activity[kaldi_rule.id] = True self._log.debug("active kaldi_rules (from window %s): %s", window_info, [kr.name for kr in self._active_kaldi_rules]) return self._kaldi_rules_activity
def matches(self, executable, title, handle): executable = executable.lower() title = title.lower() if self._executable: found = False for match in self._executable: if executable.find(match) != -1: found = True break if self._exclude == found: self._log_match.debug("%s: No match, executable doesn't match." % self) return False if self._title: found = False for match in self._title: if title.find(match) != -1: found = True break if self._exclude == found: self._log_match.debug("%s: No match, title doesn't match." % self) return False if self._kwargs: # Import locally to avoid import cycles. from dragonfly.windows import Window window = Window.get_window(handle) found = False for attr, expected_values in self._kwargs.items(): try: # Get the window attribute. attr_value = getattr(window, attr) except AttributeError: self._log_match.warning("%s: Skipped missing window" " attribute '%s'" % (self, attr)) continue # Check if the window attribute matched. for match in expected_values: is_string = isinstance(attr_value, string_types) found = ( not is_string and match == attr_value or is_string and attr_value.lower().find(match) != -1 ) if found: break if self._exclude == found: self._log_match.debug("%s: No match, not all extra" " attributes match." % self) return False if self._log_match: self._log_match.debug("%s: Match." % self) return True
def require_hardware_events(self): """ Return `True` if the current context requires hardware emulation. """ # Always use hardware_events for non-Windows platforms. if not sys.platform.startswith("win") or self._use_hardware: return True # Otherwise check if hardware events should be used with the current # foreground window. from dragonfly.windows import Window foreground_executable = basename( Window.get_foreground().executable.lower()) return ((not UNICODE_KEYBOARD) or (foreground_executable in HARDWARE_APPS))
def _compute_kaldi_rules_activity(self, phrase_start=True): if phrase_start: fg_window = Window.get_foreground() for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically(): grammar_wrapper.phrase_start_callback(fg_window) self.prepare_for_recognition() self._active_kaldi_rules = set() self._kaldi_rules_activity = [False] * self._compiler.num_kaldi_rules for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically(): if grammar_wrapper.active and (not self._any_exclusive_grammars or (self._any_exclusive_grammars and grammar_wrapper.exclusive)): for kaldi_rule in grammar_wrapper.kaldi_rule_by_rule_dict.values(): if kaldi_rule.active: self._active_kaldi_rules.add(kaldi_rule) self._kaldi_rules_activity[kaldi_rule.id] = True self._log.debug("active kaldi_rules: %s", [kr.name for kr in self._active_kaldi_rules]) return self._kaldi_rules_activity
def require_hardware_events(self): """ Return `True` if the current context requires hardware emulation. """ if self._use_hardware: return True # Load the keyboard configuration, if necessary. global _CONFIG_LOADED if not _CONFIG_LOADED: load_configuration() # Otherwise check if hardware events should be used with the current # foreground window. from dragonfly.windows import Window foreground_executable = basename( Window.get_foreground().executable.lower()) return ((not UNICODE_KEYBOARD) or (foreground_executable in HARDWARE_APPS))