fix updating

This commit is contained in:
Damien Elmes 2012-02-29 12:49:42 +09:00
parent 7189e57e80
commit 099ba71bb0
3 changed files with 18 additions and 13 deletions

View file

@ -17,6 +17,7 @@ class TextImporter(NoteImporter):
self.lines = None self.lines = None
self.fileobj = None self.fileobj = None
self.delimiter = None self.delimiter = None
self.tagsToAdd = []
def foreignNotes(self): def foreignNotes(self):
self.sniff() self.sniff()
@ -131,5 +132,5 @@ class TextImporter(NoteImporter):
def noteFromFields(self, fields): def noteFromFields(self, fields):
note = ForeignNote() note = ForeignNote()
note.fields.extend([x.strip() for x in fields]) note.fields.extend([x.strip() for x in fields])
print "fixme - add tagsToAdd to note tags" note.tags.extend(self.tagsToAdd)
return note return note

View file

@ -5,10 +5,9 @@
import time import time
from anki.lang import _ from anki.lang import _
from anki.utils import fieldChecksum, ids2str, guid64, timestampID, \ from anki.utils import fieldChecksum, ids2str, guid64, timestampID, \
joinFields, intTime joinFields, intTime, splitFields
from anki.errors import * from anki.errors import *
from anki.importing.base import Importer from anki.importing.base import Importer
#from anki.deck import NEW_CARDS_RANDOM
# Stores a list of fields, tags and deck # Stores a list of fields, tags and deck
###################################################################### ######################################################################
@ -20,12 +19,9 @@ class ForeignNote(object):
self.tags = [] self.tags = []
self.deck = None self.deck = None
# Base class for csv/supermemo/etc importers # Base class for CSV and similar text-based imports
###################################################################### ######################################################################
# - instead of specifying an update key, do it by default using first field
# The mapping is list of input fields, like: # The mapping is list of input fields, like:
# ['Expression', 'Reading', '_tags', None] # ['Expression', 'Reading', '_tags', None]
# - None means that the input should be discarded # - None means that the input should be discarded
@ -47,7 +43,6 @@ class NoteImporter(Importer):
def run(self): def run(self):
"Import." "Import."
print "fixme: randomize"
assert self.mapping assert self.mapping
c = self.foreignNotes() c = self.foreignNotes()
self.importNotes(c) self.importNotes(c)
@ -103,9 +98,12 @@ class NoteImporter(Importer):
"select flds from notes where id = ?", id) "select flds from notes where id = ?", id)
if fld0 == splitFields(flds)[0]: if fld0 == splitFields(flds)[0]:
# duplicate # duplicate
data = self.updateData(n, id) if self.update:
if data: data = self.updateData(n, id)
updates.append(data) if data:
updates.append(data)
# note that we've seen this note once already
csums[fieldChecksum(n.fields[0])] = -1
break break
# newly add # newly add
else: else:

View file

@ -82,12 +82,18 @@ def test_csv():
i = TextImporter(deck, file) i = TextImporter(deck, file)
i.mapping = ['Front', 'Back'] i.mapping = ['Front', 'Back']
i.run() i.run()
print i.log
# four problems - too many & too few fields, a missing front, and a # four problems - too many & too few fields, a missing front, and a
# duplicate entry # duplicate entry
assert len(i.log) == 4 assert len(i.log) == 4
assert i.total == 5 assert i.total == 5
print deck.db.all("select * from notes") # if we run the import again, it should update instead
i.run()
assert len(i.log) == 4
assert i.total == 5
# if updating is disabled, count will be 0
i.update = False
i.run()
assert i.total == 0
deck.close() deck.close()
def test_csv_tags(): def test_csv_tags():