Expose less from keyutils publicly

This commit is contained in:
Florian Bruhin 2018-02-27 22:21:35 +01:00
parent 880da2d143
commit 19512e988b
6 changed files with 22 additions and 23 deletions

View File

@ -125,7 +125,7 @@ class BaseKeyParser(QObject):
A QKeySequence match. A QKeySequence match.
""" """
key = e.key() 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)) self._debug_log("Got key: 0x{:x} / text: '{}'".format(key, txt))
if not txt: if not txt:

View File

@ -30,7 +30,7 @@ from PyQt5.QtGui import QKeySequence, QKeyEvent
from qutebrowser.utils import utils from qutebrowser.utils import utils
def key_to_string(key): def _key_to_string(key):
"""Convert a Qt::Key member to a meaningful name. """Convert a Qt::Key member to a meaningful name.
Args: Args:
@ -127,11 +127,6 @@ def key_to_string(key):
return name return name
def keyevent_to_string(e):
"""Convert a QKeyEvent to a meaningful name."""
return str(KeyInfo(e.key(), e.modifiers()))
class KeyParseError(Exception): class KeyParseError(Exception):
"""Raised by _parse_single_key/parse_keystring on parse errors.""" """Raised by _parse_single_key/parse_keystring on parse errors."""
@ -150,7 +145,7 @@ def _parse_keystring(keystr):
for c in keystr: for c in keystr:
if c == '>': if c == '>':
assert special assert special
yield normalize_keystr(key) yield _normalize_keystr(key)
key = '' key = ''
special = False special = False
elif c == '<': elif c == '<':
@ -165,7 +160,7 @@ def _parse_keystring(keystr):
yield 'Shift+' + c if c.isupper() else c 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. """Normalize a keystring like Ctrl-Q to a keystring like Ctrl+Q.
Args: Args:
@ -201,6 +196,10 @@ class KeyInfo:
key = attr.ib() key = attr.ib()
modifiers = attr.ib() modifiers = attr.ib()
@classmethod
def from_event(cls, e):
return cls(e.key(), e.modifiers())
def __str__(self): def __str__(self):
"""Convert this KeyInfo to a meaningful name. """Convert this KeyInfo to a meaningful name.
@ -240,7 +239,7 @@ class KeyInfo:
if self.modifiers & mask and s not in parts: if self.modifiers & mask and s not in parts:
parts.append(s) parts.append(s)
key_string = key_to_string(self.key) key_string = _key_to_string(self.key)
if len(key_string) == 1: if len(key_string) == 1:
category = unicodedata.category(key_string) category = unicodedata.category(key_string)

View File

@ -282,7 +282,7 @@ class RegisterKeyParser(keyparser.CommandKeyParser):
key = e.text() 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 # this is not a proper register key, let it pass and keep going
# FIXME can we simplify this when we refactor keyutils.py? # FIXME can we simplify this when we refactor keyutils.py?
return QKeySequence.NoMatch return QKeySequence.NoMatch

View File

@ -41,7 +41,7 @@ class KeyWidget(QWidget):
def keyPressEvent(self, e): def keyPressEvent(self, e):
"""Show pressed keys.""" """Show pressed keys."""
lines = [ lines = [
str(keyutils.keyevent_to_string(e)), str(keyutils.KeyInfo.from_event(e)),
'', '',
'key: 0x{:x}'.format(int(e.key())), 'key: 0x{:x}'.format(int(e.key())),
'modifiers: 0x{:x}'.format(int(e.modifiers())), 'modifiers: 0x{:x}'.format(int(e.modifiers())),

View File

@ -172,8 +172,7 @@ class TestSpecialKeys:
assert not keyparser.execute.called assert not keyparser.execute.called
def test_no_binding(self, monkeypatch, fake_keyevent_factory, keyparser): def test_no_binding(self, monkeypatch, fake_keyevent_factory, keyparser):
monkeypatch.setattr(keyutils, 'keyevent_to_string', monkeypatch.setattr(keyutils.KeyInfo, '__str__', lambda _self: '')
lambda binding: None)
keyparser.handle(fake_keyevent_factory(Qt.Key_A, Qt.NoModifier)) keyparser.handle(fake_keyevent_factory(Qt.Key_A, Qt.NoModifier))
assert not keyparser.execute.called assert not keyparser.execute.called

View File

@ -38,14 +38,14 @@ class TestKeyToString:
]) ])
def test_normal(self, key, expected): def test_normal(self, key, expected):
"""Test a special key where QKeyEvent::toString works incorrectly.""" """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): def test_missing(self, monkeypatch):
"""Test with a missing key.""" """Test with a missing key."""
monkeypatch.delattr(keyutils.Qt, 'Key_Blue') monkeypatch.delattr(keyutils.Qt, 'Key_Blue')
# We don't want to test the key which is actually missing - we only # We don't want to test the key which is actually missing - we only
# want to know if the mapping still behaves properly. # 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): def test_all(self):
"""Make sure there's some sensible output for all keys.""" """Make sure there's some sensible output for all keys."""
@ -53,7 +53,7 @@ class TestKeyToString:
if not isinstance(value, Qt.Key): if not isinstance(value, Qt.Key):
continue continue
print(name) print(name)
string = keyutils.key_to_string(value) string = keyutils._key_to_string(value)
assert string assert string
string.encode('utf-8') # make sure it's encodable string.encode('utf-8') # make sure it's encodable
@ -66,37 +66,38 @@ class TestKeyEventToString:
"""Test keyeevent when only control is pressed.""" """Test keyeevent when only control is pressed."""
evt = fake_keyevent_factory(key=Qt.Key_Control, evt = fake_keyevent_factory(key=Qt.Key_Control,
modifiers=Qt.ControlModifier) 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): def test_only_hyper_l(self, fake_keyevent_factory):
"""Test keyeevent when only Hyper_L is pressed.""" """Test keyeevent when only Hyper_L is pressed."""
evt = fake_keyevent_factory(key=Qt.Key_Hyper_L, evt = fake_keyevent_factory(key=Qt.Key_Hyper_L,
modifiers=Qt.MetaModifier) 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): def test_only_key(self, fake_keyevent_factory):
"""Test with a simple key pressed.""" """Test with a simple key pressed."""
evt = fake_keyevent_factory(key=Qt.Key_A) 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): def test_key_and_modifier(self, fake_keyevent_factory):
"""Test with key and modifier pressed.""" """Test with key and modifier pressed."""
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier) evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier)
expected = '<Meta+a>' if utils.is_mac else '<Ctrl+a>' 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): def test_key_and_modifiers(self, fake_keyevent_factory):
"""Test with key and multiple modifiers pressed.""" """Test with key and multiple modifiers pressed."""
evt = fake_keyevent_factory( evt = fake_keyevent_factory(
key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier | key=Qt.Key_A, modifiers=(Qt.ControlModifier | Qt.AltModifier |
Qt.MetaModifier | Qt.ShiftModifier)) 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') @pytest.mark.fake_os('mac')
def test_mac(self, fake_keyevent_factory): def test_mac(self, fake_keyevent_factory):
"""Test with a simulated mac.""" """Test with a simulated mac."""
evt = fake_keyevent_factory(key=Qt.Key_A, modifiers=Qt.ControlModifier) 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', [ @pytest.mark.parametrize('keystr, expected', [