Lint
This commit is contained in:
54
lib/words.py
54
lib/words.py
@@ -1,40 +1,46 @@
|
||||
import importlib
|
||||
import pkgutil
|
||||
import json
|
||||
from typing import Any, TypedDict, cast
|
||||
import pkgutil
|
||||
from types import ModuleType
|
||||
from typing import Any, Iterable, TypedDict, cast
|
||||
|
||||
from PyQt6.QtCore import (
|
||||
Qt,
|
||||
pyqtSlot,
|
||||
)
|
||||
from PyQt6.QtCore import Qt, pyqtSlot
|
||||
from PyQt6.QtSql import QSqlQuery
|
||||
from PyQt6.QtWidgets import QScrollArea
|
||||
|
||||
from lib.utils import query_error
|
||||
from lib.sounds import SoundOff
|
||||
from lib.definition import Definition, Line
|
||||
from trycast import trycast
|
||||
|
||||
import plugins
|
||||
def find_plugins(ns_pkg):
|
||||
return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + '.')
|
||||
from lib.definition import Definition, Line
|
||||
from lib.sounds import SoundOff
|
||||
from lib.utils import query_error
|
||||
|
||||
|
||||
def find_plugins(ns_pkg: ModuleType) -> Iterable[pkgutil.ModuleInfo]:
|
||||
return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + ".")
|
||||
|
||||
|
||||
discovered_plugins = {
|
||||
# finder, name, ispkg
|
||||
importlib.import_module(name).registration['source']: importlib.import_module(name) for _, name, _ in find_plugins(plugins)
|
||||
importlib.import_module(name).registration[
|
||||
"source"
|
||||
]: importlib.import_module(name)
|
||||
for _, name, _ in find_plugins(plugins)
|
||||
}
|
||||
|
||||
API = "https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
|
||||
|
||||
|
||||
class WordType(TypedDict):
|
||||
word: str
|
||||
source: str
|
||||
definition: str
|
||||
|
||||
|
||||
|
||||
class Word:
|
||||
"""All processing of a dictionary word."""
|
||||
|
||||
_words: dict[str, WordType] = {}
|
||||
|
||||
|
||||
def __init__(self, word: str) -> None:
|
||||
#
|
||||
# Have we already retrieved this word?
|
||||
@@ -79,30 +85,38 @@ class Word:
|
||||
|
||||
@pyqtSlot()
|
||||
def playSound(self) -> None:
|
||||
url = discovered_plugins[self.current['source']].getFirstSound(self.current['definition'])
|
||||
url = discovered_plugins[self.current["source"]].getFirstSound(
|
||||
self.current["definition"]
|
||||
)
|
||||
if url.isValid():
|
||||
snd = SoundOff()
|
||||
snd.playSound(url)
|
||||
return
|
||||
|
||||
def playPRS(self) -> None:
|
||||
return
|
||||
|
||||
def getWord(self) -> str:
|
||||
return cast(str, self.current["word"])
|
||||
return self.current["word"]
|
||||
|
||||
def get_html(self) -> str | None:
|
||||
src = self.current['source']
|
||||
src = self.current["source"]
|
||||
try:
|
||||
return discovered_plugins[src].getHtml(self.current)
|
||||
return cast(str, discovered_plugins[src].getHtml(self.current))
|
||||
except KeyError:
|
||||
raise Exception(f"Unknown source: {src}")
|
||||
|
||||
def get_def(self) -> list[Line]:
|
||||
src = self.current['source']
|
||||
src = self.current["source"]
|
||||
try:
|
||||
lines = discovered_plugins[src].getDef(self.current["definition"])
|
||||
lines = trycast(list[Line], lines)
|
||||
assert lines is not None
|
||||
return lines
|
||||
except KeyError:
|
||||
raise Exception(f"Unknown source: {self.current['source']}")
|
||||
|
||||
|
||||
class DefinitionArea(QScrollArea):
|
||||
def __init__(self, w: Word, *args: Any, **kwargs: Any) -> None:
|
||||
super(DefinitionArea, self).__init__(*args, *kwargs)
|
||||
|
||||
Reference in New Issue
Block a user