uses _path where possible

This commit is contained in:
Arthur Milchior 2020-04-06 22:16:49 +02:00
parent 5d55c4cda2
commit 266c2022b5
6 changed files with 18 additions and 13 deletions

View file

@ -126,7 +126,7 @@ class DeckManager:
# child of an existing deck then it needs to be renamed # child of an existing deck then it needs to be renamed
deck = self.get(did) deck = self.get(did)
if "::" in deck["name"]: if "::" in deck["name"]:
base = deck["name"].split("::")[-1] base = self._path(deck["name"])[-1]
suffix = "" suffix = ""
while True: while True:
# find an unused name # find an unused name
@ -469,14 +469,14 @@ class DeckManager:
self.save(deck) self.save(deck)
# ensure no sections are blank # 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"]) self.col.log("fix deck with missing sections", deck["name"])
deck["name"] = "recovered%d" % intTime(1000) deck["name"] = "recovered%d" % intTime(1000)
self.save(deck) self.save(deck)
# immediate parent must exist # immediate parent must exist
if "::" in deck["name"]: if "::" in deck["name"]:
immediateParent = "::".join(deck["name"].split("::")[:-1]) immediateParent = "::".join(self._path(deck["name"])[:-1])
if immediateParent not in names: if immediateParent not in names:
self.col.log("fix deck with missing parent", deck["name"]) self.col.log("fix deck with missing parent", deck["name"])
self._ensureParents(deck["name"]) self._ensureParents(deck["name"])
@ -579,7 +579,7 @@ class DeckManager:
childMap[deck["id"]] = node childMap[deck["id"]] = node
# add note to immediate parent # add note to immediate parent
parts = deck["name"].split("::") parts = self._path(deck["name"])
if len(parts) > 1: if len(parts) > 1:
immediateParent = "::".join(parts[:-1]) immediateParent = "::".join(parts[:-1])
pid = nameMap[immediateParent]["id"] pid = nameMap[immediateParent]["id"]
@ -591,7 +591,7 @@ class DeckManager:
"All parents of did." "All parents of did."
# get parent and grandparent names # get parent and grandparent names
parents: List[str] = [] 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: if not parents:
parents.append(part) parents.append(part)
else: else:
@ -609,7 +609,7 @@ class DeckManager:
"All existing parents of name" "All existing parents of name"
if "::" not in name: if "::" not in name:
return [] return []
names = name.split("::")[:-1] names = self._path(name)[:-1]
head = [] head = []
parents = [] parents = []

View file

@ -7,6 +7,7 @@ from typing import Any, Dict, List, Optional, Tuple
from anki.collection import _Collection from anki.collection import _Collection
from anki.consts import * from anki.consts import *
from anki.decks import DeckManager
from anki.importing.base import Importer from anki.importing.base import Importer
from anki.lang import _ from anki.lang import _
from anki.storage import Collection from anki.storage import Collection
@ -257,13 +258,13 @@ class Anki2Importer(Importer):
name = g["name"] name = g["name"]
# if there's a prefix, replace the top level deck # if there's a prefix, replace the top level deck
if self.deckPrefix: if self.deckPrefix:
tmpname = "::".join(name.split("::")[1:]) tmpname = "::".join(DeckManager._path(name)[1:])
name = self.deckPrefix name = self.deckPrefix
if tmpname: if tmpname:
name += "::" + tmpname name += "::" + tmpname
# manually create any parents so we can pull in descriptions # manually create any parents so we can pull in descriptions
head = "" head = ""
for parent in name.split("::")[:-1]: for parent in DeckManager._path(name)[:-1]:
if head: if head:
head += "::" head += "::"
head += parent head += parent

View file

@ -14,6 +14,7 @@ import anki
from anki import hooks from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.consts import * from anki.consts import *
from anki.decks import DeckManager
from anki.schedv2 import Scheduler as V2 from anki.schedv2 import Scheduler as V2
from anki.utils import ids2str, intTime from anki.utils import ids2str, intTime
@ -153,7 +154,7 @@ class Scheduler(V2):
data = [] data = []
def parent(name): def parent(name):
parts = name.split("::") parts = DeckManager._path(name)
if len(parts) < 2: if len(parts) < 2:
return None return None
parts = parts[:-1] parts = parts[:-1]

View file

@ -16,6 +16,7 @@ import anki # pylint: disable=unused-import
from anki import hooks from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.consts import * from anki.consts import *
from anki.decks import DeckManager
from anki.lang import _ from anki.lang import _
from anki.rsbackend import FormatTimeSpanContext, SchedTimingToday from anki.rsbackend import FormatTimeSpanContext, SchedTimingToday
from anki.utils import ids2str, intTime from anki.utils import ids2str, intTime
@ -240,7 +241,7 @@ order by due"""
data = [] data = []
def parent(name): def parent(name):
parts = name.split("::") parts = DeckManager._path(name)
if len(parts) < 2: if len(parts) < 2:
return None return None
parts = parts[:-1] parts = parts[:-1]
@ -279,7 +280,7 @@ order by due"""
def _groupChildren(self, grps: List[List[Any]]) -> Any: def _groupChildren(self, grps: List[List[Any]]) -> Any:
# first, split the group names into components # first, split the group names into components
for g in grps: for g in grps:
g[0] = g[0].split("::") g[0] = DeckManager._path(g[0])
# and sort based on those components # and sort based on those components
grps.sort(key=itemgetter(0)) grps.sort(key=itemgetter(0))
# then run main function # then run main function

View file

@ -34,6 +34,7 @@ from typing import Any, Dict, List, Optional, Tuple
import anki import anki
from anki import hooks from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.decks import DeckManager
from anki.models import NoteType from anki.models import NoteType
from anki.notes import Note from anki.notes import Note
from anki.rsbackend import TemplateReplacementList from anki.rsbackend import TemplateReplacementList
@ -153,7 +154,7 @@ def fields_for_rendering(
fields["Tags"] = note.stringTags().strip() fields["Tags"] = note.stringTags().strip()
fields["Type"] = card.note_type()["name"] fields["Type"] = card.note_type()["name"]
fields["Deck"] = col.decks.name(card.odid or card.did) 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"] fields["Card"] = card.template()["name"]
flag = card.userFlag() flag = card.userFlag()
fields["CardFlag"] = flag and f"flag{flag}" or "" fields["CardFlag"] = flag and f"flag{flag}" or ""

View file

@ -20,6 +20,7 @@ from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.collection import _Collection from anki.collection import _Collection
from anki.consts import * from anki.consts import *
from anki.decks import DeckManager
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.models import NoteType from anki.models import NoteType
from anki.notes import Note from anki.notes import Note
@ -1302,7 +1303,7 @@ QTableView {{ gridline-color: {grid} }}
def addDecks(parent, decks): def addDecks(parent, decks):
for head, did, rev, lrn, new, children in decks: for head, did, rev, lrn, new, children in decks:
name = self.mw.col.decks.get(did)["name"] name = self.mw.col.decks.get(did)["name"]
shortname = name.split("::")[-1] shortname = DeckManager._path(name)[-1]
if children: if children:
subm = parent.addMenu(shortname) subm = parent.addMenu(shortname)
subm.addItem(_("Filter"), self._filterFunc("deck", name)) subm.addItem(_("Filter"), self._filterFunc("deck", name))