Skip to content

Commit

Permalink
format files
Browse files Browse the repository at this point in the history
add parents to tooltips
  • Loading branch information
tatsumoto-ren committed Mar 17, 2024
1 parent c002927 commit a2b1f37
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
10 changes: 6 additions & 4 deletions find_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from anki.collection import Collection
from anki.collection import SearchNode
from anki.hooks import wrap
from anki.notes import Note
from anki.notes import Note, NoteId
from aqt.browser import Browser
from aqt.browser.find_duplicates import FindDuplicatesDialog
from aqt.qt import *
Expand All @@ -17,14 +17,16 @@
from .merge_notes import cfg_strip


def notes_from_search(self: Collection, field_name: str, search: str) -> Iterable[Note]:
def notes_from_search(col: Collection, field_name: str, search: str) -> Iterable[Note]:
return carefully_get_notes(
self.find_notes(self.build_search_string(search, SearchNode(field_name=field_name))), has_field=field_name
col,
col.find_notes(query=col.build_search_string(search, SearchNode(field_name=field_name))),
has_field=field_name,
)


def deep_search_duplicates(self: Collection, field_name: str, search: str) -> list[tuple[str, list]]:
vals = {}
vals: dict[str, list[NoteId]] = {}
for note in notes_from_search(self, field_name, search):
if val := cfg_strip(note[field_name]):
vals.setdefault(val, []).append(note.id)
Expand Down
32 changes: 19 additions & 13 deletions merge_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from collections.abc import Sequence

import anki.errors
from anki.collection import OpChanges
from anki.collection import OpChanges, Collection
from anki.hooks import wrap
from anki.notes import NoteId, Note
from aqt import mw
from aqt.browser.find_duplicates import FindDuplicatesDialog
from aqt.operations import CollectionOp
from aqt.qt import *
Expand All @@ -18,15 +17,15 @@
from .merge_notes import MergeNotes


def carefully_get_notes(nids: Sequence[NoteId], has_field: Optional[str] = None) -> list[Note]:
def carefully_get_notes(col: Collection, nids: Sequence[NoteId], has_field: Optional[str] = None) -> list[Note]:
"""
Returns notes constructed from nids. Skip nonexistent notes.
If "has_field" is not None, return notes that contain this field.
"""
ret = []
ret: list[Note] = []
for nid in set(nids):
try:
note: Note = mw.col.get_note(nid)
note: Note = col.get_note(nid)
if not has_field or has_field in note.keys():
ret.append(note)
except anki.errors.NotFoundError:
Expand All @@ -45,22 +44,29 @@ def op(self, dupes: list[tuple[str, list[NoteId]]]) -> OpChanges:
pos = self.col.add_custom_undo_entry(self.action_name)

for _, dupe_nids in dupes:
if len(chunk := carefully_get_notes(dupe_nids)) > 1:
if len(chunk := carefully_get_notes(self.col, dupe_nids)) > 1:
chunk.sort(key=sort_by_note_cards, reverse=config["reverse_order"])
self._merge_notes(chunk)

mw.col.update_notes(self.notes_to_update)
mw.col.remove_notes(self.nids_to_remove)
self.col.update_notes(self.notes_to_update)
self.col.remove_notes(self.nids_to_remove)
return self.col.merge_undo_entries(pos)


def merge_dupes(parent: QWidget, dupes: list[tuple[str, list[NoteId]]]) -> None:
if len(dupes) > 0:
CollectionOp(parent, lambda col: MergeDupes(col).op(dupes)).success(
lambda out: tooltip(f"Merged {len(dupes)} groups of notes.")
).run_in_background()
(
CollectionOp(
parent,
lambda col: MergeDupes(col).op(dupes),
)
.success(
lambda out: tooltip(f"Merged {len(dupes)} groups of notes.", parent=parent),
)
.run_in_background()
)
else:
tooltip("Nothing to do.")
tooltip("Nothing to do.", parent=parent)


def append_merge_duplicates_button(self: FindDuplicatesDialog, dupes: list[tuple[str, list[NoteId]]]):
Expand All @@ -69,7 +75,7 @@ def append_merge_duplicates_button(self: FindDuplicatesDialog, dupes: list[tuple
self._merge_dupes_button = b = self.form.buttonBox.addButton(
MergeDupes.action_name, QDialogButtonBox.ButtonRole.ActionRole
)
qconnect(b.clicked, lambda: merge_dupes(parent=self, dupes=self._dupes))
qconnect(b.clicked, lambda: merge_dupes(parent=self.browser, dupes=self._dupes))


def init():
Expand Down
21 changes: 15 additions & 6 deletions merge_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,28 @@ def on_merge_selected(browser: Browser) -> None:
cids = browser.selectedCards()

if len(cids) < 2:
tooltip("At least two cards must be selected.")
tooltip("At least two cards must be selected.", parent=browser)
return

sorted_cards = sorted(
(browser.col.get_card(cid) for cid in cids), key=config.ord_key, reverse=config["reverse_order"]
(browser.col.get_card(cid) for cid in cids),
key=config.ord_key,
reverse=config["reverse_order"],
)

if len(notes := notes_by_cards(sorted_cards)) > 1:
CollectionOp(browser, lambda col: MergeNotes(col).op(notes)).success(
lambda out: after_merge(browser, notes, cids)
).run_in_background()
(
CollectionOp(
parent=browser,
op=lambda col: MergeNotes(col).op(notes),
)
.success(
lambda out: after_merge(browser, notes, cids),
)
.run_in_background()
)
else:
tooltip("At least two distinct notes must be selected.")
tooltip("At least two distinct notes must be selected.", parent=browser)


######################################################################
Expand Down

0 comments on commit a2b1f37

Please sign in to comment.