From 099ba71bb02e0891aa48ee51d03bb18c72991f6c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 29 Feb 2012 12:49:42 +0900 Subject: [PATCH] fix updating --- anki/importing/csvfile.py | 3 ++- anki/importing/noteimp.py | 18 ++++++++---------- tests/test_importing.py | 10 ++++++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/anki/importing/csvfile.py b/anki/importing/csvfile.py index 4ea330207..6a5c5fafb 100644 --- a/anki/importing/csvfile.py +++ b/anki/importing/csvfile.py @@ -17,6 +17,7 @@ class TextImporter(NoteImporter): self.lines = None self.fileobj = None self.delimiter = None + self.tagsToAdd = [] def foreignNotes(self): self.sniff() @@ -131,5 +132,5 @@ class TextImporter(NoteImporter): def noteFromFields(self, fields): note = ForeignNote() note.fields.extend([x.strip() for x in fields]) - print "fixme - add tagsToAdd to note tags" + note.tags.extend(self.tagsToAdd) return note diff --git a/anki/importing/noteimp.py b/anki/importing/noteimp.py index 76d5699a8..235227ec0 100644 --- a/anki/importing/noteimp.py +++ b/anki/importing/noteimp.py @@ -5,10 +5,9 @@ import time from anki.lang import _ from anki.utils import fieldChecksum, ids2str, guid64, timestampID, \ - joinFields, intTime + joinFields, intTime, splitFields from anki.errors import * from anki.importing.base import Importer -#from anki.deck import NEW_CARDS_RANDOM # Stores a list of fields, tags and deck ###################################################################### @@ -20,12 +19,9 @@ class ForeignNote(object): self.tags = [] 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: # ['Expression', 'Reading', '_tags', None] # - None means that the input should be discarded @@ -47,7 +43,6 @@ class NoteImporter(Importer): def run(self): "Import." - print "fixme: randomize" assert self.mapping c = self.foreignNotes() self.importNotes(c) @@ -103,9 +98,12 @@ class NoteImporter(Importer): "select flds from notes where id = ?", id) if fld0 == splitFields(flds)[0]: # duplicate - data = self.updateData(n, id) - if data: - updates.append(data) + if self.update: + data = self.updateData(n, id) + if data: + updates.append(data) + # note that we've seen this note once already + csums[fieldChecksum(n.fields[0])] = -1 break # newly add else: diff --git a/tests/test_importing.py b/tests/test_importing.py index 7f9143087..ccf23e5ce 100644 --- a/tests/test_importing.py +++ b/tests/test_importing.py @@ -82,12 +82,18 @@ def test_csv(): i = TextImporter(deck, file) i.mapping = ['Front', 'Back'] i.run() - print i.log # four problems - too many & too few fields, a missing front, and a # duplicate entry assert len(i.log) == 4 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() def test_csv_tags():