NF: add types to noteimp.py

This commit is contained in:
Arthur Milchior 2021-03-23 12:53:07 +01:00 committed by Damien Elmes
parent 4b5944f181
commit 23083c3eb4

View file

@ -19,6 +19,11 @@ from anki.utils import (
timestampID, timestampID,
) )
type_tagsMapped = Tuple[int, int, str, str, int, str, str]
type_tagsModified = Tuple[int, int, str, str, int, str]
type_tagsElse = Tuple[int, int, str, int, str]
type_udpates = Union[type_tagsMapped, type_tagsModified, type_tagsElse]
# Stores a list of fields, tags and deck # Stores a list of fields, tags and deck
###################################################################### ######################################################################
@ -135,7 +140,7 @@ class NoteImporter(Importer):
self._fmap = self.col.models.fieldMap(self.model) self._fmap = self.col.models.fieldMap(self.model)
self._nextID = timestampID(self.col.db, "notes") self._nextID = timestampID(self.col.db, "notes")
# loop through the notes # loop through the notes
updates = [] updates: List[type_udpates] = []
updateLog = [] updateLog = []
new = [] new = []
self._ids: List[int] = [] self._ids: List[int] = []
@ -203,9 +208,9 @@ class NoteImporter(Importer):
found = False found = False
# newly add # newly add
if not found: if not found:
data = self.newData(n) new_data = self.newData(n)
if data: if new_data:
new.append(data) new.append(new_data)
# note that we've seen this note once already # note that we've seen this note once already
firsts[fld0] = True firsts[fld0] = True
self.addNew(new) self.addNew(new)
@ -235,7 +240,9 @@ class NoteImporter(Importer):
self.log.extend(updateLog) self.log.extend(updateLog)
self.total = len(self._ids) self.total = len(self._ids)
def newData(self, n: ForeignNote) -> Optional[list]: def newData(
self, n: ForeignNote
) -> Tuple[int, str, int, int, int, str, str, str, int, int, str]:
id = self._nextID id = self._nextID
self._nextID += 1 self._nextID += 1
self._ids.append(id) self._ids.append(id)
@ -243,7 +250,7 @@ class NoteImporter(Importer):
# note id for card updates later # note id for card updates later
for ord, c in list(n.cards.items()): for ord, c in list(n.cards.items()):
self._cards.append((id, ord, c)) self._cards.append((id, ord, c))
return [ return (
id, id,
guid64(), guid64(),
self.model["id"], self.model["id"],
@ -255,28 +262,33 @@ class NoteImporter(Importer):
0, 0,
0, 0,
"", "",
] )
def addNew(self, rows: List[List[Union[int, str]]]) -> None: def addNew(
self,
rows: List[Tuple[int, str, int, int, int, str, str, str, int, int, str]],
) -> None:
self.col.db.executemany( self.col.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)", rows "insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)", rows
) )
def updateData(self, n: ForeignNote, id: int, sflds: List[str]) -> Optional[list]: def updateData(
self, n: ForeignNote, id: int, sflds: List[str]
) -> Optional[type_udpates]:
self._ids.append(id) self._ids.append(id)
self.processFields(n, sflds) self.processFields(n, sflds)
if self._tagsMapped: if self._tagsMapped:
tags = self.col.tags.join(n.tags) tags = self.col.tags.join(n.tags)
return [intTime(), self.col.usn(), n.fieldsStr, tags, id, n.fieldsStr, tags] return (intTime(), self.col.usn(), n.fieldsStr, tags, id, n.fieldsStr, tags)
elif self.tagModified: elif self.tagModified:
tags = self.col.db.scalar("select tags from notes where id = ?", id) tags = self.col.db.scalar("select tags from notes where id = ?", id)
tagList = self.col.tags.split(tags) + self.tagModified.split() tagList = self.col.tags.split(tags) + self.tagModified.split()
tags = self.col.tags.join(tagList) tags = self.col.tags.join(tagList)
return [intTime(), self.col.usn(), n.fieldsStr, tags, id, n.fieldsStr] return (intTime(), self.col.usn(), n.fieldsStr, tags, id, n.fieldsStr)
else: else:
return [intTime(), self.col.usn(), n.fieldsStr, id, n.fieldsStr] return (intTime(), self.col.usn(), n.fieldsStr, id, n.fieldsStr)
def addUpdates(self, rows: List[List[Union[int, str]]]) -> None: def addUpdates(self, rows: List[type_udpates]) -> None:
changes = self.col.db.scalar("select total_changes()") changes = self.col.db.scalar("select total_changes()")
if self._tagsMapped: if self._tagsMapped:
self.col.db.executemany( self.col.db.executemany(