diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py index 8bceb7346..263906b41 100644 --- a/pylib/anki/decks.py +++ b/pylib/anki/decks.py @@ -126,7 +126,7 @@ class DeckManager: # child of an existing deck then it needs to be renamed deck = self.get(did) if "::" in deck["name"]: - base = deck["name"].split("::")[-1] + base = self._path(deck["name"])[-1] suffix = "" while True: # find an unused name @@ -469,14 +469,14 @@ class DeckManager: self.save(deck) # ensure no sections are blank - if not all(deck["name"].split("::")): + if not all(self._path(deck["name"])): self.col.log("fix deck with missing sections", deck["name"]) deck["name"] = "recovered%d" % intTime(1000) self.save(deck) # immediate parent must exist if "::" in deck["name"]: - immediateParent = "::".join(deck["name"].split("::")[:-1]) + immediateParent = "::".join(self._path(deck["name"])[:-1]) if immediateParent not in names: self.col.log("fix deck with missing parent", deck["name"]) self._ensureParents(deck["name"]) @@ -579,7 +579,7 @@ class DeckManager: childMap[deck["id"]] = node # add note to immediate parent - parts = deck["name"].split("::") + parts = self._path(deck["name"]) if len(parts) > 1: immediateParent = "::".join(parts[:-1]) pid = nameMap[immediateParent]["id"] @@ -591,7 +591,7 @@ class DeckManager: "All parents of did." # get parent and grandparent names parents: List[str] = [] - for part in self.get(did)["name"].split("::")[:-1]: + for part in self._path(self.get(did)["name"])[:-1]: if not parents: parents.append(part) else: @@ -609,7 +609,7 @@ class DeckManager: "All existing parents of name" if "::" not in name: return [] - names = name.split("::")[:-1] + names = self._path(name)[:-1] head = [] parents = [] diff --git a/pylib/anki/importing/anki2.py b/pylib/anki/importing/anki2.py index a43e0b13f..33a92fd58 100644 --- a/pylib/anki/importing/anki2.py +++ b/pylib/anki/importing/anki2.py @@ -7,6 +7,7 @@ from typing import Any, Dict, List, Optional, Tuple from anki.collection import _Collection from anki.consts import * +from anki.decks import DeckManager from anki.importing.base import Importer from anki.lang import _ from anki.storage import Collection @@ -257,13 +258,13 @@ class Anki2Importer(Importer): name = g["name"] # if there's a prefix, replace the top level deck if self.deckPrefix: - tmpname = "::".join(name.split("::")[1:]) + tmpname = "::".join(DeckManager._path(name)[1:]) name = self.deckPrefix if tmpname: name += "::" + tmpname # manually create any parents so we can pull in descriptions head = "" - for parent in name.split("::")[:-1]: + for parent in DeckManager._path(name)[:-1]: if head: head += "::" head += parent diff --git a/pylib/anki/sched.py b/pylib/anki/sched.py index 1e187e38d..3d79e1b69 100644 --- a/pylib/anki/sched.py +++ b/pylib/anki/sched.py @@ -14,6 +14,7 @@ import anki from anki import hooks from anki.cards import Card from anki.consts import * +from anki.decks import DeckManager from anki.schedv2 import Scheduler as V2 from anki.utils import ids2str, intTime @@ -153,7 +154,7 @@ class Scheduler(V2): data = [] def parent(name): - parts = name.split("::") + parts = DeckManager._path(name) if len(parts) < 2: return None parts = parts[:-1] diff --git a/pylib/anki/schedv2.py b/pylib/anki/schedv2.py index 75ab8b5d0..f787142b0 100644 --- a/pylib/anki/schedv2.py +++ b/pylib/anki/schedv2.py @@ -16,6 +16,7 @@ import anki # pylint: disable=unused-import from anki import hooks from anki.cards import Card from anki.consts import * +from anki.decks import DeckManager from anki.lang import _ from anki.rsbackend import FormatTimeSpanContext, SchedTimingToday from anki.utils import ids2str, intTime @@ -240,7 +241,7 @@ order by due""" data = [] def parent(name): - parts = name.split("::") + parts = DeckManager._path(name) if len(parts) < 2: return None parts = parts[:-1] @@ -279,7 +280,7 @@ order by due""" def _groupChildren(self, grps: List[List[Any]]) -> Any: # first, split the group names into components for g in grps: - g[0] = g[0].split("::") + g[0] = DeckManager._path(g[0]) # and sort based on those components grps.sort(key=itemgetter(0)) # then run main function diff --git a/pylib/anki/template.py b/pylib/anki/template.py index 97fde6699..06e198844 100644 --- a/pylib/anki/template.py +++ b/pylib/anki/template.py @@ -34,6 +34,7 @@ from typing import Any, Dict, List, Optional, Tuple import anki from anki import hooks from anki.cards import Card +from anki.decks import DeckManager from anki.models import NoteType from anki.notes import Note from anki.rsbackend import TemplateReplacementList @@ -153,7 +154,7 @@ def fields_for_rendering( fields["Tags"] = note.stringTags().strip() fields["Type"] = card.note_type()["name"] fields["Deck"] = col.decks.name(card.odid or card.did) - fields["Subdeck"] = fields["Deck"].split("::")[-1] + fields["Subdeck"] = DeckManager._path(fields["Deck"])[-1] fields["Card"] = card.template()["name"] flag = card.userFlag() fields["CardFlag"] = flag and f"flag{flag}" or "" diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index d68c2078a..2dbb60aeb 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -20,6 +20,7 @@ from anki import hooks from anki.cards import Card from anki.collection import _Collection from anki.consts import * +from anki.decks import DeckManager from anki.lang import _, ngettext from anki.models import NoteType from anki.notes import Note @@ -1302,7 +1303,7 @@ QTableView {{ gridline-color: {grid} }} def addDecks(parent, decks): for head, did, rev, lrn, new, children in decks: name = self.mw.col.decks.get(did)["name"] - shortname = name.split("::")[-1] + shortname = DeckManager._path(name)[-1] if children: subm = parent.addMenu(shortname) subm.addItem(_("Filter"), self._filterFunc("deck", name))