From a99101d95ce4057d81830c347809c6ccfd1ca24a Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 29 Feb 2012 13:33:55 +0900 Subject: [PATCH] initMapping(); csv tweaks --- anki/importing/csvfile.py | 21 ++++++--------------- anki/importing/noteimp.py | 15 +++++++++++---- tests/test_importing.py | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/anki/importing/csvfile.py b/anki/importing/csvfile.py index 2c0018621..0ddc3cbab 100644 --- a/anki/importing/csvfile.py +++ b/anki/importing/csvfile.py @@ -20,7 +20,7 @@ class TextImporter(NoteImporter): self.tagsToAdd = [] def foreignNotes(self): - self.sniff() + self.open() # process all lines log = [] notes = [] @@ -31,12 +31,7 @@ class TextImporter(NoteImporter): else: reader = csv.reader(self.data, self.dialect, doublequote=True) for row in reader: - try: - row = [unicode(x, "utf-8") for x in row] - except UnicodeDecodeError, e: - raise ImportFormatError( - type="encodingError", - info=_("Please save in UTF-8 format. Click help for info.")) + row = [unicode(x, "utf-8") for x in row] if len(row) != self.numFields: log.append(_( "'%(row)s' had %(num1)d fields, " @@ -54,7 +49,7 @@ class TextImporter(NoteImporter): self.fileobj.close() return notes - def sniff(self): + def open(self): "Parse the top line and determine the pattern and number of fields." # load & look for the right pattern self.cacheFile() @@ -79,15 +74,11 @@ class TextImporter(NoteImporter): del self.data[0] self.updateDelimiter() if not self.dialect and not self.delimiter: - raise ImportFormatError( - type="encodingError", - info=_("Couldn't determine format of file.")) + raise Exception("unknownFormat") def updateDelimiter(self): def err(): - raise ImportFormatError( - type="encodingError", - info=_("File is not encoded in UTF-8.")) + raise Exception("unknownFormat") self.dialect = None sniffer = csv.Sniffer() delims = [',', '\t', ';', ':'] @@ -123,7 +114,7 @@ class TextImporter(NoteImporter): def fields(self): "Number of fields." - self.sniff() + self.open() return self.numFields def noteFromFields(self, fields): diff --git a/anki/importing/noteimp.py b/anki/importing/noteimp.py index 235227ec0..603af1373 100644 --- a/anki/importing/noteimp.py +++ b/anki/importing/noteimp.py @@ -51,15 +51,22 @@ class NoteImporter(Importer): "The number of fields." return 0 - def maybeChecksum(self, data, unique): - if not unique: - return "" - return fieldChecksum(data) + def initMapping(self): + flds = [f['name'] for f in self.model['flds']] + # truncate to provided count + flds = flds[0:self.fields()] + # if provided count is greater, pad + flds = flds + [None] * (self.fields() - len(flds)) + self.mapping = flds def foreignNotes(self): "Return a list of foreign notes for importing." assert 0 + def open(self): + "Open file and ensure it's in the right format." + assert 0 + def importNotes(self, notes): "Convert each card into a note, apply attributes and add to col." # gather checks for duplicate comparison diff --git a/tests/test_importing.py b/tests/test_importing.py index ccf23e5ce..d4834fb31 100644 --- a/tests/test_importing.py +++ b/tests/test_importing.py @@ -80,7 +80,7 @@ def test_csv(): deck = getEmptyDeck() file = unicode(os.path.join(testDir, "support/text-2fields.txt")) i = TextImporter(deck, file) - i.mapping = ['Front', 'Back'] + i.initMapping() i.run() # four problems - too many & too few fields, a missing front, and a # duplicate entry