diff options
-rw-r--r-- | qutebrowser/keyinput/basekeyparser.py | 2 | ||||
-rw-r--r-- | qutebrowser/keyinput/keyutils.py | 17 | ||||
-rw-r--r-- | qutebrowser/keyinput/modeparsers.py | 2 | ||||
-rw-r--r-- | scripts/keytester.py | 2 | ||||
-rw-r--r-- | tests/unit/keyinput/test_basekeyparser.py | 3 | ||||
-rw-r--r-- | tests/unit/keyinput/test_keyutils.py | 19 |
6 files changed, 22 insertions, 23 deletions
diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index 27d760d9a..27f671dcd 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -125,7 +125,7 @@ class BaseKeyParser(QObject): A QKeySequence match. """ key = e.key() - txt = keyutils.keyevent_to_string(e) + txt = str(keyutils.KeyInfo.from_event(e)) self._debug_log("Got key: 0x{:x} / text: '{}'".format(key, txt)) if not txt: diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index d2c32e1b5..2cf151348 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -30,7 +30,7 @@ from PyQt5.QtGui import QKeySequence, QKeyEvent from qutebrowser.utils import utils -def key_to_string(key): +def _key_to_string(key): """Convert a Qt::Key member to a meaningful name. Args: @@ -127,11 +127,6 @@ def key_to_string(key): return name -def keyevent_to_string(e): - """Convert a QKeyEvent to a meaningful name.""" - return str(KeyInfo(e.key(), e.modifiers())) - - class KeyParseError(Exception): """Raised by _parse_single_key/parse_keystring on parse errors.""" @@ -150,7 +145,7 @@ def _parse_keystring(keystr): for c in keystr: if c == '>': assert special - yield normalize_keystr(key) + yield _normalize_keystr(key) key = '' special = False elif c == '<': @@ -165,7 +160,7 @@ def _parse_keystring(keystr): yield 'Shift+' + c if c.isupper() else c -def normalize_keystr(keystr): +def _normalize_keystr(keystr): """Normalize a keystring like Ctrl-Q to a keystring like Ctrl+Q. Args: @@ -201,6 +196,10 @@ class KeyInfo: key = attr.ib() modifiers = attr.ib() + @classmethod + def from_event(cls, e): + return cls(e.key(), e.modifiers()) + def __str__(self): """Convert this KeyInfo to a meaningful name. @@ -240,7 +239,7 @@ class KeyInfo: if self.modifiers & mask and s not in parts: parts.append(s) - key_string = key_to_string(self.key) + key_string = _key_to_string(self.key) if len(key_string) == 1: category = unicodedata.category(key_string) diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index f397b9fd5..42eeb53f8 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -282,7 +282,7 @@ class RegisterKeyParser(keyparser.CommandKeyParser): key = e.text() - if key == '' or keyutils.keyevent_to_string(e) is None: + if key == '' or not str(keyutils.KeyInfo.from_event(e)): # this is not a proper register key, let it pass and keep going # FIXME can we simplify this when we refactor keyutils.py? return QKeySequence.NoMatch diff --git a/scripts/keytester.py b/scripts/keytester.py index 4d27a3dd1..ee5eb347c 100644 --- a/scripts/keytester.py +++ b/scripts/keytester.py @@ -41,7 +41,7 @@ class KeyWidget(QWidget): def keyPressEvent(self, e): """Show pressed keys.""" lines = [ - str(keyutils.keyevent_to_string(e)), + str(keyutils.KeyInfo.from_event(e)), '', 'key: 0x{:x}'.format(int(e.key())), 'modifiers: 0x{:x}'.format(int(e.modifiers())), diff --git a/tests/unit/keyinput/test_basekeyparser.py b/tests/unit/keyinput/test_basekeyparser.py index f99c6f4fd..a32009390 100644 --- a/tests/unit/keyinput/test_basekeyparser.py +++ b/tests/unit/keyinput/test_basekeyparser.py @@ -172,8 +172,7 @@ class TestSpecialKeys: assert not keyparser.execute.called def test_no_binding(self, monkeypatch, fake_keyevent_factory, keyparser): - monkeypatch.setattr(keyutils, 'keyevent_to_string', - lambda binding: None) + monkeypatch.setattr(keyutils.KeyInfo, '__str__', lambda _self: '') keyparser.handle(fake_keyevent_factory(Qt.Key_A, Qt.NoModifier)) assert not keyparser.execute.called diff --git a/tests/unit/keyinput/test_keyutils.py b/tests/unit/keyinput/test_keyutils.py index 760b58e19..db6855d23 100644 --- a/tests/unit/keyinput/test_keyutils.py +++ b/tests/unit/keyinput/test_keyutils.py @@ -38,14 +38,14 @@ class TestKeyToString: ]) def test_normal(self, key, expected): """Test a special key where QKeyEvent::toString works incorrectly.""" - assert keyutils.key_to_string(key) == expected + assert keyutils._key_to_string(key) == expected def test_missing(self, monkeypatch): """Test with a missing key.""" monkeypatch.delattr(keyutils.Qt, 'Key_Blue') # We don't want to test the key which is actually missing - we only # want to know if the mapping still behaves properly. - assert keyutils.key_to_string(Qt.Key_A) == 'A' + assert keyutils._key_to_string(Qt.Key_A) == 'A' def test_all(self): """Make sure there's some sensible output for all keys.""" @@ -53,7 +53,7 @@ class TestKeyToString: if not isinstance(value, Qt.Key): continue print(name) - string = keyutils.key_to_string(value) + string = keyutils._key_to_string(value) assert string string.encode('utf-8') # make sure it's encodable @@ -66,37 +66,38 @@ class TestKeyEventToString: """Test keyeevent when only control is pressed.""" evt = fake_keyevent_factory(key=Qt.Key_Control, modifiers=Qt.ControlModifier) - assert not keyutils.keyevent_to_string(evt) + assert not str(keyutils.KeyInfo.from_event(evt)) def test_only_hyper_l(self, fake_keyevent_factory): """Test keyeevent when only Hyper_L is pressed.""" evt = fake_keyevent_factory(key=Qt.Key_Hyper_L, modifiers=Qt.MetaModifier) - assert not keyutils.keyevent_to_string(evt) + assert not str(keyutils.KeyInfo.from_event(evt)) def test_only_key(self, fake_keyevent_factory): """Test with a simple key pressed.""" evt = fake_keyevent_factory(key=Qt.Key_A) - assert keyutils.keyevent_to_string(evt) == 'a' + assert str(keyutils.KeyInfo.from_event(evt)) == 'a' def test_key_and_modifier(self, fake_keyevent_factory): """Test with key and modifier pressed.""" evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier) expected = '<Meta+a>' if utils.is_mac else '<Ctrl+a>' - assert keyutils.keyevent_to_string(evt) == expected + assert str(keyutils.KeyInfo.from_event(evt)) == expected def test_key_and_modifiers(self, fake_keyevent_factory): """Test with key and multiple modifiers pressed.""" evt = fake_keyevent_factory( key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier | Qt.ShiftModifier)) - assert keyutils.keyevent_to_string(evt) == '<Ctrl+Alt+Meta+Shift+a>' + s = str(keyutils.KeyInfo.from_event(evt)) + assert s == '<Ctrl+Alt+Meta+Shift+a>' @pytest.mark.fake_os('mac') def test_mac(self, fake_keyevent_factory): """Test with a simulated mac.""" evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier) - assert keyutils.keyevent_to_string(evt) == '<Meta+a>' + assert str(keyutils.KeyInfo.from_event(evt)) == '<Meta+a>' @pytest.mark.parametrize('keystr, expected', [ |