immediate_parent_path

This commit is contained in:
Arthur Milchior 2020-04-06 22:55:28 +02:00
parent bda2935de1
commit 285e9280cf
2 changed files with 11 additions and 7 deletions

View file

@ -302,6 +302,10 @@ class DeckManager:
_basename = basename _basename = basename
@classmethod
def immediate_parent_path(cls, name: str) -> Any:
return cls._path(name)[:-1]
@classmethod @classmethod
def key(cls, deck: Dict[str, Any]) -> List[str]: def key(cls, deck: Dict[str, Any]) -> List[str]:
return cls.path(deck["name"]) return cls.path(deck["name"])
@ -481,7 +485,7 @@ class DeckManager:
# immediate parent must exist # immediate parent must exist
if "::" in deck["name"]: if "::" in deck["name"]:
immediateParent = "::".join(self.path(deck["name"])[:-1]) immediateParent = "::".join(self.immediate_parent_path(deck["name"]))
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"])
@ -584,9 +588,9 @@ class DeckManager:
childMap[deck["id"]] = node childMap[deck["id"]] = node
# add note to immediate parent # add note to immediate parent
parts = self.path(deck["name"]) immediate_parent_path = self.immediate_parent_path(deck["name"])
if len(parts) > 1: if immediate_parent_path:
immediateParent = "::".join(parts[:-1]) immediateParent = "::".join(immediate_parent_path)
pid = nameMap[immediateParent]["id"] pid = nameMap[immediateParent]["id"]
childMap[pid][deck["id"]] = node childMap[pid][deck["id"]] = node
@ -596,7 +600,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.path(self.get(did)["name"])[:-1]: for part in self.immediate_parent_path(self.get(did)["name"]):
if not parents: if not parents:
parents.append(part) parents.append(part)
else: else:
@ -614,7 +618,7 @@ class DeckManager:
"All existing parents of name" "All existing parents of name"
if "::" not in name: if "::" not in name:
return [] return []
names = self.path(name)[:-1] names = self.immediate_parent_path(name)
head = [] head = []
parents = [] parents = []

View file

@ -264,7 +264,7 @@ class Anki2Importer(Importer):
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 DeckManager.path(name)[:-1]: for parent in DeckManager.immediate_parent_path(name):
if head: if head:
head += "::" head += "::"
head += parent head += parent