fix some of the style warnings and errors
This commit is contained in:
parent
766a94a539
commit
351420310d
@ -25,7 +25,6 @@ import functools
|
|||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import string
|
|
||||||
|
|
||||||
from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QEvent, Qt, QUrl,
|
from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QEvent, Qt, QUrl,
|
||||||
QTimer)
|
QTimer)
|
||||||
@ -51,6 +50,7 @@ Target = usertypes.enum('Target', ['normal', 'tab', 'tab_fg', 'tab_bg',
|
|||||||
'fill', 'hover', 'download', 'userscript',
|
'fill', 'hover', 'download', 'userscript',
|
||||||
'spawn'])
|
'spawn'])
|
||||||
|
|
||||||
|
|
||||||
@pyqtSlot(usertypes.KeyMode)
|
@pyqtSlot(usertypes.KeyMode)
|
||||||
def on_mode_entered(mode, win_id):
|
def on_mode_entered(mode, win_id):
|
||||||
"""Stop hinting when insert mode was entered."""
|
"""Stop hinting when insert mode was entered."""
|
||||||
@ -164,10 +164,12 @@ class HintManager(QObject):
|
|||||||
hints = set()
|
hints = set()
|
||||||
lines = (line.rstrip().lower() for line in wordfile)
|
lines = (line.rstrip().lower() for line in wordfile)
|
||||||
for word in lines:
|
for word in lines:
|
||||||
if not set(word) <= alphabet: continue
|
if not set(word) <= alphabet:
|
||||||
if not len(word) <= 4: continue
|
continue
|
||||||
|
if not len(word) <= 4:
|
||||||
|
continue
|
||||||
for i in range(len(word)):
|
for i in range(len(word)):
|
||||||
hints.discard(word[:i+1])
|
hints.discard(word[:i + 1])
|
||||||
hints.add(word)
|
hints.add(word)
|
||||||
self._words.update(hints)
|
self._words.update(hints)
|
||||||
return self._words
|
return self._words
|
||||||
@ -222,8 +224,8 @@ class HintManager(QObject):
|
|||||||
"""
|
"""
|
||||||
if config.get('hints', 'mode') == 'words':
|
if config.get('hints', 'mode') == 'words':
|
||||||
try:
|
try:
|
||||||
self._initialize_word_hints()
|
words = iter(self._initialize_word_hints())
|
||||||
return self._hint_words(elems)
|
return self._hint_words(words, elems)
|
||||||
except IOError:
|
except IOError:
|
||||||
message.error(self._win_id, "Word hints require a dictionary" +
|
message.error(self._win_id, "Word hints require a dictionary" +
|
||||||
" at /usr/share/dict/words.", immediately=True)
|
" at /usr/share/dict/words.", immediately=True)
|
||||||
@ -238,8 +240,10 @@ class HintManager(QObject):
|
|||||||
else:
|
else:
|
||||||
return self._hint_linear(min_chars, chars, elems)
|
return self._hint_linear(min_chars, chars, elems)
|
||||||
|
|
||||||
def _hint_words(self, elems):
|
def _hint_words(self, words, elems):
|
||||||
"""Produce hint words based on the link text and random words
|
"""Produce hint labels based on the html tags.
|
||||||
|
|
||||||
|
Produce hint words based on the link text and random words
|
||||||
from the words arg as fallback.
|
from the words arg as fallback.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -249,7 +253,6 @@ class HintManager(QObject):
|
|||||||
Return:
|
Return:
|
||||||
A list of hint strings, in the same order as the elements.
|
A list of hint strings, in the same order as the elements.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def html_elem_to_hints(elem):
|
def html_elem_to_hints(elem):
|
||||||
candidates = []
|
candidates = []
|
||||||
if elem.tagName() == "IMG":
|
if elem.tagName() == "IMG":
|
||||||
@ -263,32 +266,30 @@ class HintManager(QObject):
|
|||||||
elif elem.tagName() == "INPUT":
|
elif elem.tagName() == "INPUT":
|
||||||
"name" in elem and candidates.append(elem["name"])
|
"name" in elem and candidates.append(elem["name"])
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
if not candidate: continue
|
if not candidate:
|
||||||
|
continue
|
||||||
match = self.FIRST_ALPHABETIC.search(candidate)
|
match = self.FIRST_ALPHABETIC.search(candidate)
|
||||||
if not match: continue
|
if not match:
|
||||||
|
continue
|
||||||
yield candidate[match.start():match.end()].lower()
|
yield candidate[match.start():match.end()].lower()
|
||||||
|
|
||||||
def any_prefix(hint, existing):
|
def any_prefix(hint, existing):
|
||||||
return any(hint.startswith(e) or e.startswith(hint) for e in existing)
|
return any(hint.startswith(e) or e.startswith(hint) for e in existing)
|
||||||
|
|
||||||
def first_good_hint(new, existing):
|
def first_good_hint(new, existing):
|
||||||
for hint in new:
|
new = filter(bool, new)
|
||||||
# some none's
|
new = filter(lambda h: len(h) > 4, new)
|
||||||
if not hint: continue
|
new = filter(lambda h: not any_prefix(h, existing), new)
|
||||||
if len(hint) < 3: continue
|
return next(hint, None) # either the first good, or None
|
||||||
if any_prefix(hint, existing): continue
|
|
||||||
return hint
|
|
||||||
|
|
||||||
hints = []
|
hints = []
|
||||||
used_hints = set()
|
used_hints = set()
|
||||||
words = iter(self._initialize_word_hints())
|
|
||||||
for elem in elems:
|
for elem in elems:
|
||||||
hint = first_good_hint(html_elem_to_hints(elem), used_hints) or next(words)
|
hint = first_good_hint(html_elem_to_hints(elem), used_hints) or next(words)
|
||||||
used_hints.add(hint)
|
used_hints.add(hint)
|
||||||
hints.append(hint)
|
hints.append(hint)
|
||||||
return hints
|
return hints
|
||||||
|
|
||||||
|
|
||||||
def _hint_scattered(self, min_chars, chars, elems):
|
def _hint_scattered(self, min_chars, chars, elems):
|
||||||
"""Produce scattered hint labels with variable length (like Vimium).
|
"""Produce scattered hint labels with variable length (like Vimium).
|
||||||
|
|
||||||
@ -1040,4 +1041,3 @@ class HintManager(QObject):
|
|||||||
# hinting.
|
# hinting.
|
||||||
return
|
return
|
||||||
self._cleanup()
|
self._cleanup()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user