update to latest black

This commit is contained in:
Damien Elmes 2020-08-31 13:29:28 +10:00
parent 603210149c
commit a517accee3
24 changed files with 395 additions and 320 deletions

View file

@ -129,9 +129,11 @@ class Card:
self, reload: bool = False, browser: bool = False self, reload: bool = False, browser: bool = False
) -> anki.template.TemplateRenderOutput: ) -> anki.template.TemplateRenderOutput:
if not self._render_output or reload: if not self._render_output or reload:
self._render_output = anki.template.TemplateRenderContext.from_existing_card( self._render_output = (
anki.template.TemplateRenderContext.from_existing_card(
self, browser self, browser
).render() ).render()
)
return self._render_output return self._render_output
def set_render_output(self, output: anki.template.TemplateRenderOutput) -> None: def set_render_output(self, output: anki.template.TemplateRenderOutput) -> None:

View file

@ -104,7 +104,12 @@ class DeckManager:
# Deck save/load # Deck save/load
############################################################# #############################################################
def id(self, name: str, create: bool = True, type: int = 0,) -> Optional[int]: def id(
self,
name: str,
create: bool = True,
type: int = 0,
) -> Optional[int]:
"Add a deck with NAME. Reuse deck if already exists. Return id as int." "Add a deck with NAME. Reuse deck if already exists. Return id as int."
id = self.id_for_name(name) id = self.id_for_name(name)
if id: if id:

View file

@ -437,7 +437,15 @@ and notes.mid = ? and cards.ord = ?""",
for c in range(nfields): for c in range(nfields):
flds.append(newflds.get(c, "")) flds.append(newflds.get(c, ""))
flds = joinFields(flds) flds = joinFields(flds)
d.append((flds, newModel["id"], intTime(), self.col.usn(), nid,)) d.append(
(
flds,
newModel["id"],
intTime(),
self.col.usn(),
nid,
)
)
self.col.db.executemany( self.col.db.executemany(
"update notes set flds=?,mid=?,mod=?,usn=? where id = ?", d "update notes set flds=?,mid=?,mod=?,usn=? where id = ?", d
) )

View file

@ -209,7 +209,9 @@ class RustBackend(RustBackendGenerated):
langs = [anki.lang.currentLang] langs = [anki.lang.currentLang]
init_msg = pb.BackendInit( init_msg = pb.BackendInit(
locale_folder_path=ftl_folder, preferred_langs=langs, server=server, locale_folder_path=ftl_folder,
preferred_langs=langs,
server=server,
) )
self._backend = ankirspy.open_backend(init_msg.SerializeToString()) self._backend = ankirspy.open_backend(init_msg.SerializeToString())

View file

@ -668,7 +668,10 @@ did = ? and queue = {QUEUE_TYPE_DAY_LEARN_RELEARN} and due <= ? limit ?""",
return tot + tod * 1000 return tot + tod * 1000
def _leftToday( def _leftToday(
self, delays: List[int], left: int, now: Optional[int] = None, self,
delays: List[int],
left: int,
now: Optional[int] = None,
) -> int: ) -> int:
"The number of steps that can be completed by the day cutoff." "The number of steps that can be completed by the day cutoff."
if not now: if not now:
@ -1603,7 +1606,16 @@ and (queue={QUEUE_TYPE_NEW} or (queue={QUEUE_TYPE_REV} and due<=?))""",
mod = intTime() mod = intTime()
for id in ids: for id in ids:
r = random.randint(imin, imax) r = random.randint(imin, imax)
d.append((max(1, r), r + t, self.col.usn(), mod, STARTING_FACTOR, id,)) d.append(
(
max(1, r),
r + t,
self.col.usn(),
mod,
STARTING_FACTOR,
id,
)
)
self.remFromDyn(ids) self.remFromDyn(ids)
self.col.db.executemany( self.col.db.executemany(
f""" f"""

View file

@ -243,7 +243,9 @@ from revlog where id > ? """
def _dueInfo(self, tot: int, num: int) -> str: def _dueInfo(self, tot: int, num: int) -> str:
i: List[str] = [] i: List[str] = []
self._line( self._line(
i, _("Total"), self.col.tr(TR.STATISTICS_REVIEWS, reviews=tot), i,
_("Total"),
self.col.tr(TR.STATISTICS_REVIEWS, reviews=tot),
) )
self._line(i, _("Average"), self._avgDay(tot, num, _("reviews"))) self._line(i, _("Average"), self._avgDay(tot, num, _("reviews")))
tomorrow = self.col.db.scalar( tomorrow = self.col.db.scalar(
@ -436,7 +438,9 @@ group by day order by day"""
return self._lineTbl(i), int(tot) return self._lineTbl(i), int(tot)
def _splitRepData( def _splitRepData(
self, data: List[Tuple[Any, ...]], spec: Sequence[Tuple[int, str, str]], self,
data: List[Tuple[Any, ...]],
spec: Sequence[Tuple[int, str, str]],
) -> Tuple[List[Dict[str, Any]], List[Tuple[Any, Any]]]: ) -> Tuple[List[Dict[str, Any]], List[Tuple[Any, Any]]]:
sep: Dict[int, Any] = {} sep: Dict[int, Any] = {}
totcnt = {} totcnt = {}

View file

@ -1,6 +1,6 @@
wheel wheel
mypy mypy
mypy_protobuf>=1.21 mypy_protobuf>=1.21
black black>=20.1b0
pytest>=6.0.1 pytest>=6.0.1
stringcase==1.2.0 stringcase==1.2.0

View file

@ -93,10 +93,14 @@ hooks = [
doc="Obsolete, do not use.", doc="Obsolete, do not use.",
), ),
Hook( Hook(
name="sync_stage_did_change", args=["stage: str"], doc="Obsolete, do not use.", name="sync_stage_did_change",
args=["stage: str"],
doc="Obsolete, do not use.",
), ),
Hook( Hook(
name="sync_progress_did_change", args=["msg: str"], doc="Obsolete, do not use.", name="sync_progress_did_change",
args=["msg: str"],
doc="Obsolete, do not use.",
), ),
] ]

View file

@ -760,7 +760,9 @@ class AddonsDialog(QDialog):
def should_grey(self, addon: AddonMeta) -> bool: def should_grey(self, addon: AddonMeta) -> bool:
return not addon.enabled or not addon.compatible() return not addon.enabled or not addon.compatible()
def redrawAddons(self,) -> None: def redrawAddons(
self,
) -> None:
addonList = self.form.addonList addonList = self.form.addonList
mgr = self.mgr mgr = self.mgr
@ -1321,7 +1323,11 @@ class ConfigEditor(QDialog):
def updateText(self, conf: Dict[str, Any]) -> None: def updateText(self, conf: Dict[str, Any]) -> None:
text = json.dumps( text = json.dumps(
conf, ensure_ascii=False, sort_keys=True, indent=4, separators=(",", ": "), conf,
ensure_ascii=False,
sort_keys=True,
indent=4,
separators=(",", ": "),
) )
text = gui_hooks.addon_config_editor_will_display_json(text) text = gui_hooks.addon_config_editor_will_display_json(text)
self.form.editor.setPlainText(text) self.form.editor.setPlainText(text)

View file

@ -166,13 +166,19 @@ class CardLayout(QDialog):
self.tform.back_button.setToolTip(shortcut("Ctrl+2")) self.tform.back_button.setToolTip(shortcut("Ctrl+2"))
self.tform.style_button.setToolTip(shortcut("Ctrl+3")) self.tform.style_button.setToolTip(shortcut("Ctrl+3"))
QShortcut( # type: ignore QShortcut( # type: ignore
QKeySequence("Ctrl+1"), self, activated=self.tform.front_button.click, QKeySequence("Ctrl+1"),
self,
activated=self.tform.front_button.click,
) )
QShortcut( # type: ignore QShortcut( # type: ignore
QKeySequence("Ctrl+2"), self, activated=self.tform.back_button.click, QKeySequence("Ctrl+2"),
self,
activated=self.tform.back_button.click,
) )
QShortcut( # type: ignore QShortcut( # type: ignore
QKeySequence("Ctrl+3"), self, activated=self.tform.style_button.click, QKeySequence("Ctrl+3"),
self,
activated=self.tform.style_button.click,
) )
# Main area setup # Main area setup
@ -303,7 +309,10 @@ class CardLayout(QDialog):
"reviewer.js", "reviewer.js",
] ]
self.preview_web.stdHtml( self.preview_web.stdHtml(
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self, self.mw.reviewer.revHtml(),
css=["reviewer.css"],
js=jsinc,
context=self,
) )
self.preview_web.set_bridge_command(self._on_bridge_cmd, self) self.preview_web.set_bridge_command(self._on_bridge_cmd, self)
@ -764,7 +773,9 @@ Enter deck to place new %s cards in, or leave blank:"""
row = form.fields.currentIndex().row() row = form.fields.currentIndex().row()
if row >= 0: if row >= 0:
self._addField( self._addField(
fields[row], form.font.currentFont().family(), form.size.value(), fields[row],
form.font.currentFont().family(),
form.size.value(),
) )
def _addField(self, field, font, size): def _addField(self, field, font, size):

View file

@ -118,7 +118,8 @@ class DeckBrowser:
def __renderPage(self, offset): def __renderPage(self, offset):
content = DeckBrowserContent( content = DeckBrowserContent(
tree=self._renderDeckTree(self._dueTree), stats=self._renderStats(), tree=self._renderDeckTree(self._dueTree),
stats=self._renderStats(),
) )
gui_hooks.deck_browser_will_render_content(self, content) gui_hooks.deck_browser_will_render_content(self, content)
self.web.stdHtml( self.web.stdHtml(

View file

@ -233,7 +233,9 @@ class Editor:
self._links[cmd] = func self._links[cmd] = func
if keys: if keys:
QShortcut( # type: ignore QShortcut( # type: ignore
QKeySequence(keys), self.widget, activated=lambda s=self: func(s), QKeySequence(keys),
self.widget,
activated=lambda s=self: func(s),
) )
btn = self._addButton( btn = self._addButton(
icon, icon,

View file

@ -120,7 +120,12 @@ class ExportDialog(QDialog):
key_str = self.exporter.key key_str = self.exporter.key
while 1: while 1:
file = getSaveFile( file = getSaveFile(
self, _("Export"), "export", key_str, self.exporter.ext, fname=filename, self,
_("Export"),
"export",
key_str,
self.exporter.ext,
fname=filename,
) )
if not file: if not file:
return return
@ -175,14 +180,18 @@ class ExportDialog(QDialog):
if self.isTextNote: if self.isTextNote:
msg = ( msg = (
ngettext( ngettext(
"%d note exported.", "%d notes exported.", self.exporter.count, "%d note exported.",
"%d notes exported.",
self.exporter.count,
) )
% self.exporter.count % self.exporter.count
) )
else: else:
msg = ( msg = (
ngettext( ngettext(
"%d card exported.", "%d cards exported.", self.exporter.count, "%d card exported.",
"%d cards exported.",
self.exporter.count,
) )
% self.exporter.count % self.exporter.count
) )

View file

@ -960,7 +960,7 @@ class _DeckBrowserWillRenderContentHook:
def on_deck_browser_will_render_content(deck_browser, content): def on_deck_browser_will_render_content(deck_browser, content):
content.stats += " content.stats += "
<div>my html</div>" <div>my html</div>"
""" """
_hooks: List[ _hooks: List[
@ -1912,7 +1912,7 @@ class _OverviewWillRenderContentHook:
def on_overview_will_render_content(overview, content): def on_overview_will_render_content(overview, content):
content.table += " content.table += "
<div>my html</div>" <div>my html</div>"
""" """
_hooks: List[ _hooks: List[

View file

@ -92,7 +92,10 @@ def allroutes(pathin):
try: try:
directory, path = _redirectWebExports(pathin) directory, path = _redirectWebExports(pathin)
except TypeError: except TypeError:
return flask.make_response(f"Invalid path: {pathin}", HTTPStatus.FORBIDDEN,) return flask.make_response(
f"Invalid path: {pathin}",
HTTPStatus.FORBIDDEN,
)
try: try:
isdir = os.path.isdir(os.path.join(directory, path)) isdir = os.path.isdir(os.path.join(directory, path))
@ -153,7 +156,10 @@ def allroutes(pathin):
return flask.send_file(fullpath, mimetype=mimetype, conditional=True) return flask.send_file(fullpath, mimetype=mimetype, conditional=True)
else: else:
print(f"Not found: {ascii(pathin)}") print(f"Not found: {ascii(pathin)}")
return flask.make_response(f"Invalid path: {pathin}", HTTPStatus.NOT_FOUND,) return flask.make_response(
f"Invalid path: {pathin}",
HTTPStatus.NOT_FOUND,
)
except Exception as error: except Exception as error:
if devMode: if devMode:
@ -165,7 +171,10 @@ def allroutes(pathin):
# swallow it - user likely surfed away from # swallow it - user likely surfed away from
# review screen before an image had finished # review screen before an image had finished
# downloading # downloading
return flask.make_response(str(error), HTTPStatus.INTERNAL_SERVER_ERROR,) return flask.make_response(
str(error),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
def _redirectWebExports(path): def _redirectWebExports(path):

View file

@ -116,8 +116,7 @@ class MPVBase:
# Process # Process
# #
def _prepare_process(self): def _prepare_process(self):
"""Prepare the argument list for the mpv process. """Prepare the argument list for the mpv process."""
"""
self.argv = [self.executable] self.argv = [self.executable]
self.argv += self.default_argv self.argv += self.default_argv
self.argv += ["--input-ipc-server=" + self._sock_filename] self.argv += ["--input-ipc-server=" + self._sock_filename]
@ -125,13 +124,11 @@ class MPVBase:
self.argv += ["--wid=" + str(self.window_id)] self.argv += ["--wid=" + str(self.window_id)]
def _start_process(self): def _start_process(self):
"""Start the mpv process. """Start the mpv process."""
"""
self._proc = subprocess.Popen(self.argv, env=self.popenEnv) self._proc = subprocess.Popen(self.argv, env=self.popenEnv)
def _stop_process(self): def _stop_process(self):
"""Stop the mpv process. """Stop the mpv process."""
"""
if hasattr(self, "_proc"): if hasattr(self, "_proc"):
try: try:
self._proc.terminate() self._proc.terminate()
@ -197,8 +194,7 @@ class MPVBase:
raise MPVProcessError("unable to start process") raise MPVProcessError("unable to start process")
def _stop_socket(self): def _stop_socket(self):
"""Clean up the socket. """Clean up the socket."""
"""
if hasattr(self, "_sock"): if hasattr(self, "_sock"):
self._sock.close() self._sock.close()
if hasattr(self, "_sock_filename"): if hasattr(self, "_sock_filename"):
@ -208,23 +204,20 @@ class MPVBase:
pass pass
def _prepare_thread(self): def _prepare_thread(self):
"""Set up the queues for the communication threads. """Set up the queues for the communication threads."""
"""
self._request_queue = Queue(1) self._request_queue = Queue(1)
self._response_queues = {} self._response_queues = {}
self._event_queue = Queue() self._event_queue = Queue()
self._stop_event = threading.Event() self._stop_event = threading.Event()
def _start_thread(self): def _start_thread(self):
"""Start up the communication threads. """Start up the communication threads."""
"""
self._thread = threading.Thread(target=self._reader) self._thread = threading.Thread(target=self._reader)
self._thread.daemon = True self._thread.daemon = True
self._thread.start() self._thread.start()
def _stop_thread(self): def _stop_thread(self):
"""Stop the communication threads. """Stop the communication threads."""
"""
if hasattr(self, "_stop_event"): if hasattr(self, "_stop_event"):
self._stop_event.set() self._stop_event.set()
if hasattr(self, "_thread"): if hasattr(self, "_thread"):
@ -276,15 +269,13 @@ class MPVBase:
# Message handling # Message handling
# #
def _compose_message(self, message): def _compose_message(self, message):
"""Return a json representation from a message dictionary. """Return a json representation from a message dictionary."""
"""
# XXX may be strict is too strict ;-) # XXX may be strict is too strict ;-)
data = json.dumps(message) data = json.dumps(message)
return data.encode("utf8", "strict") + b"\n" return data.encode("utf8", "strict") + b"\n"
def _parse_message(self, data): def _parse_message(self, data):
"""Return a message dictionary from a json representation. """Return a message dictionary from a json representation."""
"""
# XXX may be strict is too strict ;-) # XXX may be strict is too strict ;-)
data = data.decode("utf8", "strict") data = data.decode("utf8", "strict")
return json.loads(data) return json.loads(data)
@ -374,8 +365,7 @@ class MPVBase:
return None return None
def _send_request(self, message, timeout=None, _retry=1): def _send_request(self, message, timeout=None, _retry=1):
"""Send a command to the mpv process and collect the result. """Send a command to the mpv process and collect the result."""
"""
self.ensure_running() self.ensure_running()
try: try:
self._send_message(message, timeout) self._send_message(message, timeout)
@ -399,8 +389,7 @@ class MPVBase:
# Public API # Public API
# #
def is_running(self): def is_running(self):
"""Return True if the mpv process is still active. """Return True if the mpv process is still active."""
"""
return self._proc.poll() is None return self._proc.poll() is None
def ensure_running(self): def ensure_running(self):
@ -417,8 +406,7 @@ class MPVBase:
self._register_callbacks() self._register_callbacks()
def close(self): def close(self):
"""Shutdown the mpv process and our communication setup. """Shutdown the mpv process and our communication setup."""
"""
if self.is_running(): if self.is_running():
self._send_request({"command": ["quit"]}, timeout=1) self._send_request({"command": ["quit"]}, timeout=1)
self._stop_process() self._stop_process()
@ -498,8 +486,7 @@ class MPV(MPVBase):
# Socket communication # Socket communication
# #
def _start_thread(self): def _start_thread(self):
"""Start up the communication threads. """Start up the communication threads."""
"""
super()._start_thread() super()._start_thread()
if not hasattr(self, "_event_thread"): if not hasattr(self, "_event_thread"):
self._event_thread = threading.Thread(target=self._event_reader) self._event_thread = threading.Thread(target=self._event_reader)
@ -510,8 +497,7 @@ class MPV(MPVBase):
# Event/callback API # Event/callback API
# #
def _event_reader(self): def _event_reader(self):
"""Collect incoming event messages and call the event handler. """Collect incoming event messages and call the event handler."""
"""
while True: while True:
message = self._get_event(timeout=1) message = self._get_event(timeout=1)
if message is None: if message is None:
@ -520,8 +506,7 @@ class MPV(MPVBase):
self._handle_event(message) self._handle_event(message)
def _handle_event(self, message): def _handle_event(self, message):
"""Lookup and call the callbacks for a particular event message. """Lookup and call the callbacks for a particular event message."""
"""
if not self._callbacks_initialized: if not self._callbacks_initialized:
self._callbacks_queue.put(message) self._callbacks_queue.put(message)
return return
@ -538,8 +523,7 @@ class MPV(MPVBase):
callback() callback()
def register_callback(self, name, callback): def register_callback(self, name, callback):
"""Register a function `callback` for the event `name`. """Register a function `callback` for the event `name`."""
"""
try: try:
self.command("enable_event", name) self.command("enable_event", name)
except MPVCommandError: except MPVCommandError:
@ -606,18 +590,15 @@ class MPV(MPVBase):
# Public API # Public API
# #
def command(self, *args, timeout=1): def command(self, *args, timeout=1):
"""Execute a single command on the mpv process and return the result. """Execute a single command on the mpv process and return the result."""
"""
return self._send_request({"command": list(args)}, timeout=timeout) return self._send_request({"command": list(args)}, timeout=timeout)
def get_property(self, name): def get_property(self, name):
"""Return the value of property `name`. """Return the value of property `name`."""
"""
return self.command("get_property", name) return self.command("get_property", name)
def set_property(self, name, value): def set_property(self, name, value):
"""Set the value of property `name`. """Set the value of property `name`."""
"""
return self.command("set_property", name, value) return self.command("set_property", name, value)

View file

@ -119,7 +119,10 @@ class Previewer(QDialog):
"reviewer.js", "reviewer.js",
] ]
self._web.stdHtml( self._web.stdHtml(
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self, self.mw.reviewer.revHtml(),
css=["reviewer.css"],
js=jsinc,
context=self,
) )
self._web.set_bridge_command(self._on_bridge_cmd, self) self._web.set_bridge_command(self._on_bridge_cmd, self)

View file

@ -146,7 +146,9 @@ class ProfileManager:
app = QtWidgets.QApplication([]) app = QtWidgets.QApplication([])
icon = QtGui.QIcon() icon = QtGui.QIcon()
icon.addPixmap( icon.addPixmap(
QtGui.QPixmap(":/icons/anki.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off, QtGui.QPixmap(":/icons/anki.png"),
QtGui.QIcon.Normal,
QtGui.QIcon.Off,
) )
window_title = "Data Folder Migration" window_title = "Data Folder Migration"
migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}" migration_directories = f"\n\n {oldBase}\n\nto\n\n {self.base}"

View file

@ -77,7 +77,8 @@ def on_normal_sync_timer(mw: aqt.main.AnkiQt) -> None:
assert isinstance(progress.val, NormalSyncProgress) assert isinstance(progress.val, NormalSyncProgress)
mw.progress.update( mw.progress.update(
label=f"{progress.val.added}\n{progress.val.removed}", process=False, label=f"{progress.val.added}\n{progress.val.removed}",
process=False,
) )
mw.progress.set_title(progress.val.stage) mw.progress.set_title(progress.val.stage)

View file

@ -412,7 +412,10 @@ hooks = [
), ),
Hook( Hook(
name="webview_will_set_content", name="webview_will_set_content",
args=["web_content: aqt.webview.WebContent", "context: Optional[Any]",], args=[
"web_content: aqt.webview.WebContent",
"context: Optional[Any]",
],
doc="""Used to modify web content before it is rendered. doc="""Used to modify web content before it is rendered.
Web_content contains the HTML, JS, and CSS the web view will be Web_content contains the HTML, JS, and CSS the web view will be
@ -526,7 +529,8 @@ hooks = [
doc="""Executed when the top toolbar is redrawn""", doc="""Executed when the top toolbar is redrawn""",
), ),
Hook( Hook(
name="media_sync_did_progress", args=["entry: aqt.mediasync.LogEntryWithTime"], name="media_sync_did_progress",
args=["entry: aqt.mediasync.LogEntryWithTime"],
), ),
Hook(name="media_sync_did_start_or_stop", args=["running: bool"]), Hook(name="media_sync_did_start_or_stop", args=["running: bool"]),
Hook( Hook(
@ -541,7 +545,10 @@ hooks = [
args=["addcards: aqt.addcards.AddCards", "menu: QMenu"], args=["addcards: aqt.addcards.AddCards", "menu: QMenu"],
legacy_hook="AddCards.onHistory", legacy_hook="AddCards.onHistory",
), ),
Hook(name="add_cards_did_init", args=["addcards: aqt.addcards.AddCards"],), Hook(
name="add_cards_did_init",
args=["addcards: aqt.addcards.AddCards"],
),
Hook( Hook(
name="add_cards_did_add_note", name="add_cards_did_add_note",
args=["note: anki.notes.Note"], args=["note: anki.notes.Note"],
@ -623,7 +630,10 @@ hooks = [
name="editor_web_view_did_init", name="editor_web_view_did_init",
args=["editor_web_view: aqt.editor.EditorWebView"], args=["editor_web_view: aqt.editor.EditorWebView"],
), ),
Hook(name="editor_did_init", args=["editor: aqt.editor.Editor"],), Hook(
name="editor_did_init",
args=["editor: aqt.editor.Editor"],
),
Hook( Hook(
name="editor_will_load_note", name="editor_will_load_note",
args=["js: str", "note: anki.notes.Note", "editor: aqt.editor.Editor"], args=["js: str", "note: anki.notes.Note", "editor: aqt.editor.Editor"],
@ -675,7 +685,10 @@ hooks = [
), ),
# Model # Model
################### ###################
Hook(name="models_advanced_will_show", args=["advanced: QDialog"],), Hook(
name="models_advanced_will_show",
args=["advanced: QDialog"],
),
# Stats # Stats
################### ###################
Hook( Hook(