Merge pull request #372 from glutanimate/addon-type-hints

Add type hints for a few more methods commonly accessed by add-ons
This commit is contained in:
Damien Elmes 2019-12-22 12:28:07 +10:00 committed by GitHub
commit 0581e29f88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View file

@ -389,7 +389,7 @@ and have been disabled: %(found)s") % dict(name=self.addonName(dir), found=addon
# Add-on Config API # Add-on Config API
###################################################################### ######################################################################
def getConfig(self, module): def getConfig(self, module: str) -> Optional[dict]:
addon = self.addonFromModule(module) addon = self.addonFromModule(module)
# get default config # get default config
config = self.addonConfigDefaults(addon) config = self.addonConfigDefaults(addon)
@ -401,15 +401,15 @@ and have been disabled: %(found)s") % dict(name=self.addonName(dir), found=addon
config.update(userConf) config.update(userConf)
return config return config
def setConfigAction(self, module, fn): def setConfigAction(self, module: str, fn: Callable[[], Optional[bool]]):
addon = self.addonFromModule(module) addon = self.addonFromModule(module)
self._configButtonActions[addon] = fn self._configButtonActions[addon] = fn
def setConfigUpdatedAction(self, module, fn): def setConfigUpdatedAction(self, module: str, fn: Callable[[Any], None]):
addon = self.addonFromModule(module) addon = self.addonFromModule(module)
self._configUpdatedActions[addon] = fn self._configUpdatedActions[addon] = fn
def writeConfig(self, module, conf): def writeConfig(self, module: str, conf: dict):
addon = self.addonFromModule(module) addon = self.addonFromModule(module)
meta = self.addonMeta(addon) meta = self.addonMeta(addon)
meta['config'] = conf meta['config'] = conf
@ -442,7 +442,7 @@ and have been disabled: %(found)s") % dict(name=self.addonName(dir), found=addon
_webExports: Dict[str, str] = {} _webExports: Dict[str, str] = {}
def setWebExports(self, module, pattern): def setWebExports(self, module: str, pattern: str):
addon = self.addonFromModule(module) addon = self.addonFromModule(module)
self._webExports[addon] = pattern self._webExports[addon] = pattern

View file

@ -11,6 +11,7 @@ import urllib.error
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import warnings import warnings
from typing import Optional, Callable, List, Tuple
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -74,7 +75,7 @@ class Editor:
self.web.onBridgeCmd = self.onBridgeCmd self.web.onBridgeCmd = self.onBridgeCmd
self.outerLayout.addWidget(self.web, 1) self.outerLayout.addWidget(self.web, 1)
righttopbtns = list() righttopbtns: List[str] = list()
righttopbtns.append(self._addButton('text_bold', 'bold', _("Bold text (Ctrl+B)"), id='bold')) righttopbtns.append(self._addButton('text_bold', 'bold', _("Bold text (Ctrl+B)"), id='bold'))
righttopbtns.append(self._addButton('text_italic', 'italic', _("Italic text (Ctrl+I)"), id='italic')) righttopbtns.append(self._addButton('text_italic', 'italic', _("Italic text (Ctrl+I)"), id='italic'))
righttopbtns.append(self._addButton('text_under', 'underline', _("Underline text (Ctrl+U)"), id='underline')) righttopbtns.append(self._addButton('text_under', 'underline', _("Underline text (Ctrl+U)"), id='underline'))
@ -131,20 +132,23 @@ class Editor:
return 'data:%s;base64,%s' % (mime, data64.decode('ascii')) return 'data:%s;base64,%s' % (mime, data64.decode('ascii'))
def addButton(self, icon, cmd, func, tip="", label="", def addButton(self, icon: str, cmd: str, func: Callable[["Editor"], None],
id=None, toggleable=False, keys=None, disables=True): tip: str = "", label: str = "", id: str = None,
toggleable: bool = False, keys: str = None,
disables: bool = True):
"""Assign func to bridge cmd, register shortcut, return button""" """Assign func to bridge cmd, register shortcut, return button"""
if cmd not in self._links: if cmd not in self._links:
self._links[cmd] = func self._links[cmd] = func
if keys: if keys:
QShortcut(QKeySequence(keys), self.widget, QShortcut(QKeySequence(keys), self.widget,
activated = lambda s=self: func(s)) activated=lambda s=self: func(s))
btn = self._addButton(icon, cmd, tip=tip, label=label, btn = self._addButton(icon, cmd, tip=tip, label=label,
id=id, toggleable=toggleable, disables=disables) id=id, toggleable=toggleable, disables=disables)
return btn return btn
def _addButton(self, icon, cmd, tip="", label="", id=None, toggleable=False, def _addButton(self, icon: str, cmd: str, tip: str = "", label: str = "",
disables=True): id: Optional[str] = None, toggleable: bool = False,
disables: bool = True):
if icon: if icon:
if icon.startswith("qrc:/"): if icon.startswith("qrc:/"):
iconstr = icon iconstr = icon
@ -180,7 +184,7 @@ class Editor:
def setupShortcuts(self): def setupShortcuts(self):
# if a third element is provided, enable shortcut even when no field selected # if a third element is provided, enable shortcut even when no field selected
cuts = [ cuts: List[Tuple] = [
("Ctrl+L", self.onCardLayout, True), ("Ctrl+L", self.onCardLayout, True),
("Ctrl+B", self.toggleBold), ("Ctrl+B", self.toggleBold),
("Ctrl+I", self.toggleItalic), ("Ctrl+I", self.toggleItalic),