From 50c17b66fc0c311daa363d1fce23fb1bd00bf900 Mon Sep 17 00:00:00 2001 From: Thomas Hess Date: Thu, 23 May 2019 16:14:47 +0200 Subject: [PATCH] AbstractAbbreviation: Type-check abbreviations. Added a function to batch-add multiple abbreviations. --- lib/autokey/model.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/autokey/model.py b/lib/autokey/model.py index c2f1dfa6..f0a3a54e 100644 --- a/lib/autokey/model.py +++ b/lib/autokey/model.py @@ -159,7 +159,22 @@ def get_word_chars(self): return self.wordChars.pattern def add_abbreviation(self, abbr): + if not isinstance(abbr, str): + raise ValueError("Abbreviations must be strings. Cannot add abbreviation '{}', having type {}.".format( + abbr, type(abbr) + )) self.abbreviations.append(abbr) + if TriggerMode.ABBREVIATION not in self.modes: + self.modes.append(TriggerMode.ABBREVIATION) + + def add_abbreviations(self, abbreviation_list: typing.Iterable[str]): + if not isinstance(abbreviation_list, list): + abbreviation_list = list(abbreviation_list) + if not all(isinstance(abbr, str) for abbr in abbreviation_list): + raise ValueError("All added Abbreviations must be strings.") + self.abbreviations += abbreviation_list + if TriggerMode.ABBREVIATION not in self.modes: + self.modes.append(TriggerMode.ABBREVIATION) def clear_abbreviations(self): self.abbreviations = [] @@ -354,8 +369,8 @@ def _should_trigger_window_title(self, window_info): class AbstractHotkey(AbstractWindowFilter): def __init__(self): - self.modifiers = [] - self.hotKey = None + self.modifiers = [] # type: typing.List[Key] + self.hotKey = None # type: typing.Optional[str] def get_serializable(self): d = { @@ -375,6 +390,7 @@ def set_hotkey(self, modifiers, key): modifiers.sort() self.modifiers = modifiers self.hotKey = key + self.modes.append(TriggerMode.HOTKEY) def check_hotkey(self, modifiers, key, windowTitle): if self.hotKey is not None and self._should_trigger_window_title(windowTitle):